NCR5380.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NCR 5380 generic driver routines. These should make it *trivial*
  4. * to implement 5380 SCSI drivers under Linux with a non-trantor
  5. * architecture.
  6. *
  7. * Note that these routines also work with NR53c400 family chips.
  8. *
  9. * Copyright 1993, Drew Eckhardt
  10. * Visionary Computing
  11. * (Unix and Linux consulting and custom programming)
  12. * drew@colorado.edu
  13. * +1 (303) 666-5836
  14. *
  15. * For more information, please consult
  16. *
  17. * NCR 5380 Family
  18. * SCSI Protocol Controller
  19. * Databook
  20. *
  21. * NCR Microelectronics
  22. * 1635 Aeroplaza Drive
  23. * Colorado Springs, CO 80916
  24. * 1+ (719) 578-3400
  25. * 1+ (800) 334-5454
  26. */
  27. /*
  28. * With contributions from Ray Van Tassle, Ingmar Baumgart,
  29. * Ronald van Cuijlenborg, Alan Cox and others.
  30. */
  31. /* Ported to Atari by Roman Hodek and others. */
  32. /* Adapted for the Sun 3 by Sam Creasey. */
  33. /*
  34. * Design
  35. *
  36. * This is a generic 5380 driver. To use it on a different platform,
  37. * one simply writes appropriate system specific macros (ie, data
  38. * transfer - some PC's will use the I/O bus, 68K's must use
  39. * memory mapped) and drops this file in their 'C' wrapper.
  40. *
  41. * As far as command queueing, two queues are maintained for
  42. * each 5380 in the system - commands that haven't been issued yet,
  43. * and commands that are currently executing. This means that an
  44. * unlimited number of commands may be queued, letting
  45. * more commands propagate from the higher driver levels giving higher
  46. * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
  47. * allowing multiple commands to propagate all the way to a SCSI-II device
  48. * while a command is already executing.
  49. *
  50. *
  51. * Issues specific to the NCR5380 :
  52. *
  53. * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
  54. * piece of hardware that requires you to sit in a loop polling for
  55. * the REQ signal as long as you are connected. Some devices are
  56. * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
  57. * while doing long seek operations. [...] These
  58. * broken devices are the exception rather than the rule and I'd rather
  59. * spend my time optimizing for the normal case.
  60. *
  61. * Architecture :
  62. *
  63. * At the heart of the design is a coroutine, NCR5380_main,
  64. * which is started from a workqueue for each NCR5380 host in the
  65. * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
  66. * removing the commands from the issue queue and calling
  67. * NCR5380_select() if a nexus is not established.
  68. *
  69. * Once a nexus is established, the NCR5380_information_transfer()
  70. * phase goes through the various phases as instructed by the target.
  71. * if the target goes into MSG IN and sends a DISCONNECT message,
  72. * the command structure is placed into the per instance disconnected
  73. * queue, and NCR5380_main tries to find more work. If the target is
  74. * idle for too long, the system will try to sleep.
  75. *
  76. * If a command has disconnected, eventually an interrupt will trigger,
  77. * calling NCR5380_intr() which will in turn call NCR5380_reselect
  78. * to reestablish a nexus. This will run main if necessary.
  79. *
  80. * On command termination, the done function will be called as
  81. * appropriate.
  82. *
  83. * The command data pointer is initialized after the command is connected
  84. * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
  85. * Note that in violation of the standard, an implicit SAVE POINTERS operation
  86. * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
  87. */
  88. /*
  89. * Using this file :
  90. * This file a skeleton Linux SCSI driver for the NCR 5380 series
  91. * of chips. To use it, you write an architecture specific functions
  92. * and macros and include this file in your driver.
  93. *
  94. * These macros MUST be defined :
  95. *
  96. * NCR5380_read(register) - read from the specified register
  97. *
  98. * NCR5380_write(register, value) - write to the specific register
  99. *
  100. * NCR5380_implementation_fields - additional fields needed for this
  101. * specific implementation of the NCR5380
  102. *
  103. * Either real DMA *or* pseudo DMA may be implemented
  104. *
  105. * NCR5380_dma_xfer_len - determine size of DMA/PDMA transfer
  106. * NCR5380_dma_send_setup - execute DMA/PDMA from memory to 5380
  107. * NCR5380_dma_recv_setup - execute DMA/PDMA from 5380 to memory
  108. * NCR5380_dma_residual - residual byte count
  109. *
  110. * The generic driver is initialized by calling NCR5380_init(instance),
  111. * after setting the appropriate host specific fields and ID.
  112. */
  113. #ifndef NCR5380_io_delay
  114. #define NCR5380_io_delay(x)
  115. #endif
  116. #ifndef NCR5380_acquire_dma_irq
  117. #define NCR5380_acquire_dma_irq(x) (1)
  118. #endif
  119. #ifndef NCR5380_release_dma_irq
  120. #define NCR5380_release_dma_irq(x)
  121. #endif
  122. static unsigned int disconnect_mask = ~0;
  123. module_param(disconnect_mask, int, 0444);
  124. static int do_abort(struct Scsi_Host *, unsigned int);
  125. static void do_reset(struct Scsi_Host *);
  126. static void bus_reset_cleanup(struct Scsi_Host *);
  127. /**
  128. * initialize_SCp - init the scsi pointer field
  129. * @cmd: command block to set up
  130. *
  131. * Set up the internal fields in the SCSI command.
  132. */
  133. static inline void initialize_SCp(struct scsi_cmnd *cmd)
  134. {
  135. struct NCR5380_cmd *ncmd = NCR5380_to_ncmd(cmd);
  136. if (scsi_bufflen(cmd)) {
  137. ncmd->buffer = scsi_sglist(cmd);
  138. ncmd->ptr = sg_virt(ncmd->buffer);
  139. ncmd->this_residual = ncmd->buffer->length;
  140. } else {
  141. ncmd->buffer = NULL;
  142. ncmd->ptr = NULL;
  143. ncmd->this_residual = 0;
  144. }
  145. ncmd->status = 0;
  146. }
  147. static inline void advance_sg_buffer(struct NCR5380_cmd *ncmd)
  148. {
  149. struct scatterlist *s = ncmd->buffer;
  150. if (!ncmd->this_residual && s && !sg_is_last(s)) {
  151. ncmd->buffer = sg_next(s);
  152. ncmd->ptr = sg_virt(ncmd->buffer);
  153. ncmd->this_residual = ncmd->buffer->length;
  154. }
  155. }
  156. static inline void set_resid_from_SCp(struct scsi_cmnd *cmd)
  157. {
  158. struct NCR5380_cmd *ncmd = NCR5380_to_ncmd(cmd);
  159. int resid = ncmd->this_residual;
  160. struct scatterlist *s = ncmd->buffer;
  161. if (s)
  162. while (!sg_is_last(s)) {
  163. s = sg_next(s);
  164. resid += s->length;
  165. }
  166. scsi_set_resid(cmd, resid);
  167. }
  168. /**
  169. * NCR5380_poll_politely2 - wait for two chip register values
  170. * @hostdata: host private data
  171. * @reg1: 5380 register to poll
  172. * @bit1: Bitmask to check
  173. * @val1: Expected value
  174. * @reg2: Second 5380 register to poll
  175. * @bit2: Second bitmask to check
  176. * @val2: Second expected value
  177. * @wait: Time-out in jiffies, 0 if sleeping is not allowed
  178. *
  179. * Polls the chip in a reasonably efficient manner waiting for an
  180. * event to occur. After a short quick poll we begin to yield the CPU
  181. * (if possible). In irq contexts the time-out is arbitrarily limited.
  182. *
  183. * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
  184. */
  185. static int NCR5380_poll_politely2(struct NCR5380_hostdata *hostdata,
  186. unsigned int reg1, u8 bit1, u8 val1,
  187. unsigned int reg2, u8 bit2, u8 val2,
  188. unsigned long wait)
  189. {
  190. unsigned long n = hostdata->poll_loops;
  191. unsigned long deadline = jiffies + wait;
  192. do {
  193. if ((NCR5380_read(reg1) & bit1) == val1)
  194. return 0;
  195. if ((NCR5380_read(reg2) & bit2) == val2)
  196. return 0;
  197. cpu_relax();
  198. } while (n--);
  199. if (!wait)
  200. return -ETIMEDOUT;
  201. /* Repeatedly sleep for 1 ms until deadline */
  202. while (time_is_after_jiffies(deadline)) {
  203. schedule_timeout_uninterruptible(1);
  204. if ((NCR5380_read(reg1) & bit1) == val1)
  205. return 0;
  206. if ((NCR5380_read(reg2) & bit2) == val2)
  207. return 0;
  208. }
  209. return -ETIMEDOUT;
  210. }
  211. #if NDEBUG
  212. static struct {
  213. unsigned char mask;
  214. const char *name;
  215. } signals[] = {
  216. {SR_DBP, "PARITY"},
  217. {SR_RST, "RST"},
  218. {SR_BSY, "BSY"},
  219. {SR_REQ, "REQ"},
  220. {SR_MSG, "MSG"},
  221. {SR_CD, "CD"},
  222. {SR_IO, "IO"},
  223. {SR_SEL, "SEL"},
  224. {0, NULL}
  225. },
  226. basrs[] = {
  227. {BASR_END_DMA_TRANSFER, "END OF DMA"},
  228. {BASR_DRQ, "DRQ"},
  229. {BASR_PARITY_ERROR, "PARITY ERROR"},
  230. {BASR_IRQ, "IRQ"},
  231. {BASR_PHASE_MATCH, "PHASE MATCH"},
  232. {BASR_BUSY_ERROR, "BUSY ERROR"},
  233. {BASR_ATN, "ATN"},
  234. {BASR_ACK, "ACK"},
  235. {0, NULL}
  236. },
  237. icrs[] = {
  238. {ICR_ASSERT_RST, "ASSERT RST"},
  239. {ICR_ARBITRATION_PROGRESS, "ARB. IN PROGRESS"},
  240. {ICR_ARBITRATION_LOST, "LOST ARB."},
  241. {ICR_ASSERT_ACK, "ASSERT ACK"},
  242. {ICR_ASSERT_BSY, "ASSERT BSY"},
  243. {ICR_ASSERT_SEL, "ASSERT SEL"},
  244. {ICR_ASSERT_ATN, "ASSERT ATN"},
  245. {ICR_ASSERT_DATA, "ASSERT DATA"},
  246. {0, NULL}
  247. },
  248. mrs[] = {
  249. {MR_BLOCK_DMA_MODE, "BLOCK DMA MODE"},
  250. {MR_TARGET, "TARGET"},
  251. {MR_ENABLE_PAR_CHECK, "PARITY CHECK"},
  252. {MR_ENABLE_PAR_INTR, "PARITY INTR"},
  253. {MR_ENABLE_EOP_INTR, "EOP INTR"},
  254. {MR_MONITOR_BSY, "MONITOR BSY"},
  255. {MR_DMA_MODE, "DMA MODE"},
  256. {MR_ARBITRATE, "ARBITRATE"},
  257. {0, NULL}
  258. };
  259. /**
  260. * NCR5380_print - print scsi bus signals
  261. * @instance: adapter state to dump
  262. *
  263. * Print the SCSI bus signals for debugging purposes
  264. */
  265. static void NCR5380_print(struct Scsi_Host *instance)
  266. {
  267. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  268. unsigned char status, basr, mr, icr, i;
  269. status = NCR5380_read(STATUS_REG);
  270. mr = NCR5380_read(MODE_REG);
  271. icr = NCR5380_read(INITIATOR_COMMAND_REG);
  272. basr = NCR5380_read(BUS_AND_STATUS_REG);
  273. printk(KERN_DEBUG "SR = 0x%02x : ", status);
  274. for (i = 0; signals[i].mask; ++i)
  275. if (status & signals[i].mask)
  276. printk(KERN_CONT "%s, ", signals[i].name);
  277. printk(KERN_CONT "\nBASR = 0x%02x : ", basr);
  278. for (i = 0; basrs[i].mask; ++i)
  279. if (basr & basrs[i].mask)
  280. printk(KERN_CONT "%s, ", basrs[i].name);
  281. printk(KERN_CONT "\nICR = 0x%02x : ", icr);
  282. for (i = 0; icrs[i].mask; ++i)
  283. if (icr & icrs[i].mask)
  284. printk(KERN_CONT "%s, ", icrs[i].name);
  285. printk(KERN_CONT "\nMR = 0x%02x : ", mr);
  286. for (i = 0; mrs[i].mask; ++i)
  287. if (mr & mrs[i].mask)
  288. printk(KERN_CONT "%s, ", mrs[i].name);
  289. printk(KERN_CONT "\n");
  290. }
  291. static struct {
  292. unsigned char value;
  293. const char *name;
  294. } phases[] = {
  295. {PHASE_DATAOUT, "DATAOUT"},
  296. {PHASE_DATAIN, "DATAIN"},
  297. {PHASE_CMDOUT, "CMDOUT"},
  298. {PHASE_STATIN, "STATIN"},
  299. {PHASE_MSGOUT, "MSGOUT"},
  300. {PHASE_MSGIN, "MSGIN"},
  301. {PHASE_UNKNOWN, "UNKNOWN"}
  302. };
  303. /**
  304. * NCR5380_print_phase - show SCSI phase
  305. * @instance: adapter to dump
  306. *
  307. * Print the current SCSI phase for debugging purposes
  308. */
  309. static void NCR5380_print_phase(struct Scsi_Host *instance)
  310. {
  311. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  312. unsigned char status;
  313. int i;
  314. status = NCR5380_read(STATUS_REG);
  315. if (!(status & SR_REQ))
  316. shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
  317. else {
  318. for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
  319. (phases[i].value != (status & PHASE_MASK)); ++i)
  320. ;
  321. shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
  322. }
  323. }
  324. #endif
  325. /**
  326. * NCR5380_info - report driver and host information
  327. * @instance: relevant scsi host instance
  328. *
  329. * For use as the host template info() handler.
  330. */
  331. static const char *NCR5380_info(struct Scsi_Host *instance)
  332. {
  333. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  334. return hostdata->info;
  335. }
  336. /**
  337. * NCR5380_init - initialise an NCR5380
  338. * @instance: adapter to configure
  339. * @flags: control flags
  340. *
  341. * Initializes *instance and corresponding 5380 chip,
  342. * with flags OR'd into the initial flags value.
  343. *
  344. * Notes : I assume that the host, hostno, and id bits have been
  345. * set correctly. I don't care about the irq and other fields.
  346. *
  347. * Returns 0 for success
  348. */
  349. static int NCR5380_init(struct Scsi_Host *instance, int flags)
  350. {
  351. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  352. int i;
  353. unsigned long deadline;
  354. unsigned long accesses_per_ms;
  355. instance->max_lun = 7;
  356. hostdata->host = instance;
  357. hostdata->id_mask = 1 << instance->this_id;
  358. hostdata->id_higher_mask = 0;
  359. for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
  360. if (i > hostdata->id_mask)
  361. hostdata->id_higher_mask |= i;
  362. for (i = 0; i < 8; ++i)
  363. hostdata->busy[i] = 0;
  364. hostdata->dma_len = 0;
  365. spin_lock_init(&hostdata->lock);
  366. hostdata->connected = NULL;
  367. hostdata->sensing = NULL;
  368. INIT_LIST_HEAD(&hostdata->autosense);
  369. INIT_LIST_HEAD(&hostdata->unissued);
  370. INIT_LIST_HEAD(&hostdata->disconnected);
  371. hostdata->flags = flags;
  372. INIT_WORK(&hostdata->main_task, NCR5380_main);
  373. hostdata->work_q = alloc_workqueue("ncr5380_%d",
  374. WQ_UNBOUND | WQ_MEM_RECLAIM,
  375. 0, instance->host_no);
  376. if (!hostdata->work_q)
  377. return -ENOMEM;
  378. snprintf(hostdata->info, sizeof(hostdata->info),
  379. "%s, irq %d, io_port 0x%lx, base 0x%lx, can_queue %d, cmd_per_lun %d, sg_tablesize %d, this_id %d, flags { %s%s%s}",
  380. instance->hostt->name, instance->irq, hostdata->io_port,
  381. hostdata->base, instance->can_queue, instance->cmd_per_lun,
  382. instance->sg_tablesize, instance->this_id,
  383. hostdata->flags & FLAG_DMA_FIXUP ? "DMA_FIXUP " : "",
  384. hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
  385. hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "");
  386. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  387. NCR5380_write(MODE_REG, MR_BASE);
  388. NCR5380_write(TARGET_COMMAND_REG, 0);
  389. NCR5380_write(SELECT_ENABLE_REG, 0);
  390. /* Calibrate register polling loop */
  391. i = 0;
  392. deadline = jiffies + 1;
  393. do {
  394. cpu_relax();
  395. } while (time_is_after_jiffies(deadline));
  396. deadline += msecs_to_jiffies(256);
  397. do {
  398. NCR5380_read(STATUS_REG);
  399. ++i;
  400. cpu_relax();
  401. } while (time_is_after_jiffies(deadline));
  402. accesses_per_ms = i / 256;
  403. hostdata->poll_loops = NCR5380_REG_POLL_TIME * accesses_per_ms / 2;
  404. return 0;
  405. }
  406. /**
  407. * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
  408. * @instance: adapter to check
  409. *
  410. * If the system crashed, it may have crashed with a connected target and
  411. * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
  412. * currently established nexus, which we know nothing about. Failing that
  413. * do a bus reset.
  414. *
  415. * Note that a bus reset will cause the chip to assert IRQ.
  416. *
  417. * Returns 0 if successful, otherwise -ENXIO.
  418. */
  419. static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
  420. {
  421. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  422. int pass;
  423. for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
  424. switch (pass) {
  425. case 1:
  426. case 3:
  427. case 5:
  428. shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
  429. NCR5380_poll_politely(hostdata,
  430. STATUS_REG, SR_BSY, 0, 5 * HZ);
  431. break;
  432. case 2:
  433. shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
  434. do_abort(instance, 1);
  435. break;
  436. case 4:
  437. shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
  438. do_reset(instance);
  439. /* Wait after a reset; the SCSI standard calls for
  440. * 250ms, we wait 500ms to be on the safe side.
  441. * But some Toshiba CD-ROMs need ten times that.
  442. */
  443. if (hostdata->flags & FLAG_TOSHIBA_DELAY)
  444. msleep(2500);
  445. else
  446. msleep(500);
  447. break;
  448. case 6:
  449. shost_printk(KERN_ERR, instance, "bus locked solid\n");
  450. return -ENXIO;
  451. }
  452. }
  453. return 0;
  454. }
  455. /**
  456. * NCR5380_exit - remove an NCR5380
  457. * @instance: adapter to remove
  458. *
  459. * Assumes that no more work can be queued (e.g. by NCR5380_intr).
  460. */
  461. static void NCR5380_exit(struct Scsi_Host *instance)
  462. {
  463. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  464. cancel_work_sync(&hostdata->main_task);
  465. destroy_workqueue(hostdata->work_q);
  466. }
  467. /**
  468. * complete_cmd - finish processing a command and return it to the SCSI ML
  469. * @instance: the host instance
  470. * @cmd: command to complete
  471. */
  472. static void complete_cmd(struct Scsi_Host *instance,
  473. struct scsi_cmnd *cmd)
  474. {
  475. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  476. dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
  477. if (hostdata->sensing == cmd) {
  478. /* Autosense processing ends here */
  479. if (get_status_byte(cmd) != SAM_STAT_GOOD) {
  480. scsi_eh_restore_cmnd(cmd, &hostdata->ses);
  481. } else {
  482. scsi_eh_restore_cmnd(cmd, &hostdata->ses);
  483. set_status_byte(cmd, SAM_STAT_CHECK_CONDITION);
  484. }
  485. hostdata->sensing = NULL;
  486. }
  487. scsi_done(cmd);
  488. }
  489. /**
  490. * NCR5380_queue_command - queue a command
  491. * @instance: the relevant SCSI adapter
  492. * @cmd: SCSI command
  493. *
  494. * cmd is added to the per-instance issue queue, with minor
  495. * twiddling done to the host specific fields of cmd. If the
  496. * main coroutine is not running, it is restarted.
  497. */
  498. static int NCR5380_queue_command(struct Scsi_Host *instance,
  499. struct scsi_cmnd *cmd)
  500. {
  501. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  502. struct NCR5380_cmd *ncmd = NCR5380_to_ncmd(cmd);
  503. unsigned long flags;
  504. #if (NDEBUG & NDEBUG_NO_WRITE)
  505. switch (cmd->cmnd[0]) {
  506. case WRITE_6:
  507. case WRITE_10:
  508. shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
  509. cmd->result = (DID_ERROR << 16);
  510. scsi_done(cmd);
  511. return 0;
  512. }
  513. #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
  514. cmd->result = 0;
  515. spin_lock_irqsave(&hostdata->lock, flags);
  516. if (!NCR5380_acquire_dma_irq(instance)) {
  517. spin_unlock_irqrestore(&hostdata->lock, flags);
  518. return SCSI_MLQUEUE_HOST_BUSY;
  519. }
  520. /*
  521. * Insert the cmd into the issue queue. Note that REQUEST SENSE
  522. * commands are added to the head of the queue since any command will
  523. * clear the contingent allegiance condition that exists and the
  524. * sense data is only guaranteed to be valid while the condition exists.
  525. */
  526. if (cmd->cmnd[0] == REQUEST_SENSE)
  527. list_add(&ncmd->list, &hostdata->unissued);
  528. else
  529. list_add_tail(&ncmd->list, &hostdata->unissued);
  530. spin_unlock_irqrestore(&hostdata->lock, flags);
  531. dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
  532. cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
  533. /* Kick off command processing */
  534. queue_work(hostdata->work_q, &hostdata->main_task);
  535. return 0;
  536. }
  537. static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
  538. {
  539. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  540. /* Caller does the locking needed to set & test these data atomically */
  541. if (list_empty(&hostdata->disconnected) &&
  542. list_empty(&hostdata->unissued) &&
  543. list_empty(&hostdata->autosense) &&
  544. !hostdata->connected &&
  545. !hostdata->selecting) {
  546. NCR5380_release_dma_irq(instance);
  547. }
  548. }
  549. /**
  550. * dequeue_next_cmd - dequeue a command for processing
  551. * @instance: the scsi host instance
  552. *
  553. * Priority is given to commands on the autosense queue. These commands
  554. * need autosense because of a CHECK CONDITION result.
  555. *
  556. * Returns a command pointer if a command is found for a target that is
  557. * not already busy. Otherwise returns NULL.
  558. */
  559. static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
  560. {
  561. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  562. struct NCR5380_cmd *ncmd;
  563. struct scsi_cmnd *cmd;
  564. if (hostdata->sensing || list_empty(&hostdata->autosense)) {
  565. list_for_each_entry(ncmd, &hostdata->unissued, list) {
  566. cmd = NCR5380_to_scmd(ncmd);
  567. dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
  568. cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
  569. if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
  570. list_del(&ncmd->list);
  571. dsprintk(NDEBUG_QUEUES, instance,
  572. "dequeue: removed %p from issue queue\n", cmd);
  573. return cmd;
  574. }
  575. }
  576. } else {
  577. /* Autosense processing begins here */
  578. ncmd = list_first_entry(&hostdata->autosense,
  579. struct NCR5380_cmd, list);
  580. list_del(&ncmd->list);
  581. cmd = NCR5380_to_scmd(ncmd);
  582. dsprintk(NDEBUG_QUEUES, instance,
  583. "dequeue: removed %p from autosense queue\n", cmd);
  584. scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
  585. hostdata->sensing = cmd;
  586. return cmd;
  587. }
  588. return NULL;
  589. }
  590. static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
  591. {
  592. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  593. struct NCR5380_cmd *ncmd = NCR5380_to_ncmd(cmd);
  594. if (hostdata->sensing == cmd) {
  595. scsi_eh_restore_cmnd(cmd, &hostdata->ses);
  596. list_add(&ncmd->list, &hostdata->autosense);
  597. hostdata->sensing = NULL;
  598. } else
  599. list_add(&ncmd->list, &hostdata->unissued);
  600. }
  601. /**
  602. * NCR5380_main - NCR state machines
  603. *
  604. * NCR5380_main is a coroutine that runs as long as more work can
  605. * be done on the NCR5380 host adapters in a system. Both
  606. * NCR5380_queue_command() and NCR5380_intr() will try to start it
  607. * in case it is not running.
  608. */
  609. static void NCR5380_main(struct work_struct *work)
  610. {
  611. struct NCR5380_hostdata *hostdata =
  612. container_of(work, struct NCR5380_hostdata, main_task);
  613. struct Scsi_Host *instance = hostdata->host;
  614. int done;
  615. do {
  616. done = 1;
  617. spin_lock_irq(&hostdata->lock);
  618. while (!hostdata->connected && !hostdata->selecting) {
  619. struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
  620. if (!cmd)
  621. break;
  622. dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
  623. /*
  624. * Attempt to establish an I_T_L nexus here.
  625. * On success, instance->hostdata->connected is set.
  626. * On failure, we must add the command back to the
  627. * issue queue so we can keep trying.
  628. */
  629. /*
  630. * REQUEST SENSE commands are issued without tagged
  631. * queueing, even on SCSI-II devices because the
  632. * contingent allegiance condition exists for the
  633. * entire unit.
  634. */
  635. if (!NCR5380_select(instance, cmd)) {
  636. dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
  637. } else {
  638. dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
  639. "main: select failed, returning %p to queue\n", cmd);
  640. requeue_cmd(instance, cmd);
  641. }
  642. }
  643. if (hostdata->connected && !hostdata->dma_len) {
  644. dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
  645. NCR5380_information_transfer(instance);
  646. done = 0;
  647. }
  648. if (!hostdata->connected) {
  649. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  650. maybe_release_dma_irq(instance);
  651. }
  652. spin_unlock_irq(&hostdata->lock);
  653. if (!done)
  654. cond_resched();
  655. } while (!done);
  656. }
  657. /*
  658. * NCR5380_dma_complete - finish DMA transfer
  659. * @instance: the scsi host instance
  660. *
  661. * Called by the interrupt handler when DMA finishes or a phase
  662. * mismatch occurs (which would end the DMA transfer).
  663. */
  664. static void NCR5380_dma_complete(struct Scsi_Host *instance)
  665. {
  666. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  667. struct NCR5380_cmd *ncmd = NCR5380_to_ncmd(hostdata->connected);
  668. int transferred;
  669. unsigned char **data;
  670. int *count;
  671. int saved_data = 0, overrun = 0;
  672. unsigned char p;
  673. if (hostdata->read_overruns) {
  674. p = ncmd->phase;
  675. if (p & SR_IO) {
  676. udelay(10);
  677. if ((NCR5380_read(BUS_AND_STATUS_REG) &
  678. (BASR_PHASE_MATCH | BASR_ACK)) ==
  679. (BASR_PHASE_MATCH | BASR_ACK)) {
  680. saved_data = NCR5380_read(INPUT_DATA_REG);
  681. overrun = 1;
  682. dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
  683. }
  684. }
  685. }
  686. #ifdef CONFIG_SUN3
  687. if (sun3scsi_dma_finish(hostdata->connected->sc_data_direction)) {
  688. pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
  689. instance->host_no);
  690. BUG();
  691. }
  692. if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
  693. (BASR_PHASE_MATCH | BASR_ACK)) {
  694. pr_err("scsi%d: BASR %02x\n", instance->host_no,
  695. NCR5380_read(BUS_AND_STATUS_REG));
  696. pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
  697. instance->host_no);
  698. BUG();
  699. }
  700. #endif
  701. NCR5380_write(MODE_REG, MR_BASE);
  702. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  703. NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  704. transferred = hostdata->dma_len - NCR5380_dma_residual(hostdata);
  705. hostdata->dma_len = 0;
  706. data = (unsigned char **)&ncmd->ptr;
  707. count = &ncmd->this_residual;
  708. *data += transferred;
  709. *count -= transferred;
  710. if (hostdata->read_overruns) {
  711. int cnt, toPIO;
  712. if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
  713. cnt = toPIO = hostdata->read_overruns;
  714. if (overrun) {
  715. dsprintk(NDEBUG_DMA, instance,
  716. "Got an input overrun, using saved byte\n");
  717. *(*data)++ = saved_data;
  718. (*count)--;
  719. cnt--;
  720. toPIO--;
  721. }
  722. if (toPIO > 0) {
  723. dsprintk(NDEBUG_DMA, instance,
  724. "Doing %d byte PIO to 0x%p\n", cnt, *data);
  725. NCR5380_transfer_pio(instance, &p, &cnt, data, 0);
  726. *count -= toPIO - cnt;
  727. }
  728. }
  729. }
  730. }
  731. /**
  732. * NCR5380_intr - generic NCR5380 irq handler
  733. * @irq: interrupt number
  734. * @dev_id: device info
  735. *
  736. * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
  737. * from the disconnected queue, and restarting NCR5380_main()
  738. * as required.
  739. *
  740. * The chip can assert IRQ in any of six different conditions. The IRQ flag
  741. * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
  742. * Three of these six conditions are latched in the Bus and Status Register:
  743. * - End of DMA (cleared by ending DMA Mode)
  744. * - Parity error (cleared by reading RPIR)
  745. * - Loss of BSY (cleared by reading RPIR)
  746. * Two conditions have flag bits that are not latched:
  747. * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
  748. * - Bus reset (non-maskable)
  749. * The remaining condition has no flag bit at all:
  750. * - Selection/reselection
  751. *
  752. * Hence, establishing the cause(s) of any interrupt is partly guesswork.
  753. * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
  754. * claimed that "the design of the [DP8490] interrupt logic ensures
  755. * interrupts will not be lost (they can be on the DP5380)."
  756. * The L5380/53C80 datasheet from LOGIC Devices has more details.
  757. *
  758. * Checking for bus reset by reading RST is futile because of interrupt
  759. * latency, but a bus reset will reset chip logic. Checking for parity error
  760. * is unnecessary because that interrupt is never enabled. A Loss of BSY
  761. * condition will clear DMA Mode. We can tell when this occurs because the
  762. * Busy Monitor interrupt is enabled together with DMA Mode.
  763. */
  764. static irqreturn_t __maybe_unused NCR5380_intr(int irq, void *dev_id)
  765. {
  766. struct Scsi_Host *instance = dev_id;
  767. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  768. int handled = 0;
  769. unsigned char basr;
  770. unsigned long flags;
  771. spin_lock_irqsave(&hostdata->lock, flags);
  772. basr = NCR5380_read(BUS_AND_STATUS_REG);
  773. if (basr & BASR_IRQ) {
  774. unsigned char mr = NCR5380_read(MODE_REG);
  775. unsigned char sr = NCR5380_read(STATUS_REG);
  776. dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
  777. irq, basr, sr, mr);
  778. if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
  779. /* Probably End of DMA, Phase Mismatch or Loss of BSY.
  780. * We ack IRQ after clearing Mode Register. Workarounds
  781. * for End of DMA errata need to happen in DMA Mode.
  782. */
  783. dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
  784. if (hostdata->connected) {
  785. NCR5380_dma_complete(instance);
  786. queue_work(hostdata->work_q, &hostdata->main_task);
  787. } else {
  788. NCR5380_write(MODE_REG, MR_BASE);
  789. NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  790. }
  791. } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
  792. (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
  793. /* Probably reselected */
  794. NCR5380_write(SELECT_ENABLE_REG, 0);
  795. NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  796. dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
  797. if (!hostdata->connected) {
  798. NCR5380_reselect(instance);
  799. queue_work(hostdata->work_q, &hostdata->main_task);
  800. }
  801. if (!hostdata->connected)
  802. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  803. } else {
  804. /* Probably Bus Reset */
  805. NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  806. if (sr & SR_RST) {
  807. /* Certainly Bus Reset */
  808. shost_printk(KERN_WARNING, instance,
  809. "bus reset interrupt\n");
  810. bus_reset_cleanup(instance);
  811. } else {
  812. dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
  813. }
  814. #ifdef SUN3_SCSI_VME
  815. dregs->csr |= CSR_DMA_ENABLE;
  816. #endif
  817. }
  818. handled = 1;
  819. } else {
  820. dsprintk(NDEBUG_INTR, instance, "interrupt without IRQ bit\n");
  821. #ifdef SUN3_SCSI_VME
  822. dregs->csr |= CSR_DMA_ENABLE;
  823. #endif
  824. }
  825. spin_unlock_irqrestore(&hostdata->lock, flags);
  826. return IRQ_RETVAL(handled);
  827. }
  828. /**
  829. * NCR5380_select - attempt arbitration and selection for a given command
  830. * @instance: the Scsi_Host instance
  831. * @cmd: the scsi_cmnd to execute
  832. *
  833. * This routine establishes an I_T_L nexus for a SCSI command. This involves
  834. * ARBITRATION, SELECTION and MESSAGE OUT phases and an IDENTIFY message.
  835. *
  836. * Returns true if the operation should be retried.
  837. * Returns false if it should not be retried.
  838. *
  839. * Side effects :
  840. * If bus busy, arbitration failed, etc, NCR5380_select() will exit
  841. * with registers as they should have been on entry - ie
  842. * SELECT_ENABLE will be set appropriately, the NCR5380
  843. * will cease to drive any SCSI bus signals.
  844. *
  845. * If successful : the I_T_L nexus will be established, and
  846. * hostdata->connected will be set to cmd.
  847. * SELECT interrupt will be disabled.
  848. *
  849. * If failed (no target) : scsi_done() will be called, and the
  850. * cmd->result host byte set to DID_BAD_TARGET.
  851. */
  852. static bool NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
  853. __releases(&hostdata->lock) __acquires(&hostdata->lock)
  854. {
  855. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  856. unsigned char tmp[3], phase;
  857. unsigned char *data;
  858. int len;
  859. int err;
  860. bool ret = true;
  861. bool can_disconnect = instance->irq != NO_IRQ &&
  862. cmd->cmnd[0] != REQUEST_SENSE &&
  863. (disconnect_mask & BIT(scmd_id(cmd)));
  864. NCR5380_dprint(NDEBUG_ARBITRATION, instance);
  865. dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
  866. instance->this_id);
  867. /*
  868. * Arbitration and selection phases are slow and involve dropping the
  869. * lock, so we have to watch out for EH. An exception handler may
  870. * change 'selecting' to NULL. This function will then return false
  871. * so that the caller will forget about 'cmd'. (During information
  872. * transfer phases, EH may change 'connected' to NULL.)
  873. */
  874. hostdata->selecting = cmd;
  875. /*
  876. * Set the phase bits to 0, otherwise the NCR5380 won't drive the
  877. * data bus during SELECTION.
  878. */
  879. NCR5380_write(TARGET_COMMAND_REG, 0);
  880. /*
  881. * Start arbitration.
  882. */
  883. NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
  884. NCR5380_write(MODE_REG, MR_ARBITRATE);
  885. /* The chip now waits for BUS FREE phase. Then after the 800 ns
  886. * Bus Free Delay, arbitration will begin.
  887. */
  888. spin_unlock_irq(&hostdata->lock);
  889. err = NCR5380_poll_politely2(hostdata, MODE_REG, MR_ARBITRATE, 0,
  890. INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
  891. ICR_ARBITRATION_PROGRESS, HZ);
  892. spin_lock_irq(&hostdata->lock);
  893. if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
  894. /* Reselection interrupt */
  895. goto out;
  896. }
  897. if (!hostdata->selecting) {
  898. /* Command was aborted */
  899. NCR5380_write(MODE_REG, MR_BASE);
  900. return false;
  901. }
  902. if (err < 0) {
  903. NCR5380_write(MODE_REG, MR_BASE);
  904. shost_printk(KERN_ERR, instance,
  905. "select: arbitration timeout\n");
  906. goto out;
  907. }
  908. spin_unlock_irq(&hostdata->lock);
  909. /* The SCSI-2 arbitration delay is 2.4 us */
  910. udelay(3);
  911. /* Check for lost arbitration */
  912. if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
  913. (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
  914. (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
  915. NCR5380_write(MODE_REG, MR_BASE);
  916. dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
  917. spin_lock_irq(&hostdata->lock);
  918. goto out;
  919. }
  920. /* After/during arbitration, BSY should be asserted.
  921. * IBM DPES-31080 Version S31Q works now
  922. * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
  923. */
  924. NCR5380_write(INITIATOR_COMMAND_REG,
  925. ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
  926. /*
  927. * Again, bus clear + bus settle time is 1.2us, however, this is
  928. * a minimum so we'll udelay ceil(1.2)
  929. */
  930. if (hostdata->flags & FLAG_TOSHIBA_DELAY)
  931. udelay(15);
  932. else
  933. udelay(2);
  934. spin_lock_irq(&hostdata->lock);
  935. /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
  936. if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
  937. goto out;
  938. if (!hostdata->selecting) {
  939. NCR5380_write(MODE_REG, MR_BASE);
  940. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  941. return false;
  942. }
  943. dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
  944. /*
  945. * Now that we have won arbitration, start Selection process, asserting
  946. * the host and target ID's on the SCSI bus.
  947. */
  948. NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
  949. /*
  950. * Raise ATN while SEL is true before BSY goes false from arbitration,
  951. * since this is the only way to guarantee that we'll get a MESSAGE OUT
  952. * phase immediately after selection.
  953. */
  954. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
  955. ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
  956. NCR5380_write(MODE_REG, MR_BASE);
  957. /*
  958. * Reselect interrupts must be turned off prior to the dropping of BSY,
  959. * otherwise we will trigger an interrupt.
  960. */
  961. NCR5380_write(SELECT_ENABLE_REG, 0);
  962. spin_unlock_irq(&hostdata->lock);
  963. /*
  964. * The initiator shall then wait at least two deskew delays and release
  965. * the BSY signal.
  966. */
  967. udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
  968. /* Reset BSY */
  969. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
  970. ICR_ASSERT_ATN | ICR_ASSERT_SEL);
  971. /*
  972. * Something weird happens when we cease to drive BSY - looks
  973. * like the board/chip is letting us do another read before the
  974. * appropriate propagation delay has expired, and we're confusing
  975. * a BSY signal from ourselves as the target's response to SELECTION.
  976. *
  977. * A small delay (the 'C++' frontend breaks the pipeline with an
  978. * unnecessary jump, making it work on my 386-33/Trantor T128, the
  979. * tighter 'C' code breaks and requires this) solves the problem -
  980. * the 1 us delay is arbitrary, and only used because this delay will
  981. * be the same on other platforms and since it works here, it should
  982. * work there.
  983. *
  984. * wingel suggests that this could be due to failing to wait
  985. * one deskew delay.
  986. */
  987. udelay(1);
  988. dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
  989. /*
  990. * The SCSI specification calls for a 250 ms timeout for the actual
  991. * selection.
  992. */
  993. err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_BSY, SR_BSY,
  994. msecs_to_jiffies(250));
  995. if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
  996. spin_lock_irq(&hostdata->lock);
  997. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  998. NCR5380_reselect(instance);
  999. shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
  1000. goto out;
  1001. }
  1002. if (err < 0) {
  1003. spin_lock_irq(&hostdata->lock);
  1004. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1005. /* Can't touch cmd if it has been reclaimed by the scsi ML */
  1006. if (!hostdata->selecting)
  1007. return false;
  1008. cmd->result = DID_BAD_TARGET << 16;
  1009. complete_cmd(instance, cmd);
  1010. dsprintk(NDEBUG_SELECTION, instance,
  1011. "target did not respond within 250ms\n");
  1012. ret = false;
  1013. goto out;
  1014. }
  1015. /*
  1016. * No less than two deskew delays after the initiator detects the
  1017. * BSY signal is true, it shall release the SEL signal and may
  1018. * change the DATA BUS. -wingel
  1019. */
  1020. udelay(1);
  1021. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1022. /*
  1023. * Since we followed the SCSI spec, and raised ATN while SEL
  1024. * was true but before BSY was false during selection, the information
  1025. * transfer phase should be a MESSAGE OUT phase so that we can send the
  1026. * IDENTIFY message.
  1027. */
  1028. /* Wait for start of REQ/ACK handshake */
  1029. err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
  1030. spin_lock_irq(&hostdata->lock);
  1031. if (err < 0) {
  1032. shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
  1033. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1034. goto out;
  1035. }
  1036. if (!hostdata->selecting) {
  1037. do_abort(instance, 0);
  1038. return false;
  1039. }
  1040. dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
  1041. scmd_id(cmd));
  1042. tmp[0] = IDENTIFY(can_disconnect, cmd->device->lun);
  1043. len = 1;
  1044. data = tmp;
  1045. phase = PHASE_MSGOUT;
  1046. NCR5380_transfer_pio(instance, &phase, &len, &data, 0);
  1047. if (len) {
  1048. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1049. cmd->result = DID_ERROR << 16;
  1050. complete_cmd(instance, cmd);
  1051. dsprintk(NDEBUG_SELECTION, instance, "IDENTIFY message transfer failed\n");
  1052. ret = false;
  1053. goto out;
  1054. }
  1055. dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
  1056. hostdata->connected = cmd;
  1057. hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
  1058. #ifdef SUN3_SCSI_VME
  1059. dregs->csr |= CSR_INTR;
  1060. #endif
  1061. initialize_SCp(cmd);
  1062. ret = false;
  1063. out:
  1064. if (!hostdata->selecting)
  1065. return false;
  1066. hostdata->selecting = NULL;
  1067. return ret;
  1068. }
  1069. /**
  1070. * NCR5380_transfer_pio() - transfers data in given phase using polled I/O
  1071. * @instance: instance of driver
  1072. * @phase: pointer to what phase is expected
  1073. * @count: pointer to number of bytes to transfer
  1074. * @data: pointer to data pointer
  1075. * @can_sleep: 1 or 0 when sleeping is permitted or not, respectively
  1076. *
  1077. * Returns: void. *phase, *count, *data are modified in place.
  1078. */
  1079. /*
  1080. * Note : this code is not as quick as it could be, however it
  1081. * IS 100% reliable, and for the actual data transfer where speed
  1082. * counts, we will always do a pseudo DMA or DMA transfer.
  1083. */
  1084. static void NCR5380_transfer_pio(struct Scsi_Host *instance,
  1085. unsigned char *phase, int *count,
  1086. unsigned char **data, unsigned int can_sleep)
  1087. {
  1088. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  1089. unsigned char p = *phase, tmp;
  1090. int c = *count;
  1091. unsigned char *d = *data;
  1092. /*
  1093. * The NCR5380 chip will only drive the SCSI bus when the
  1094. * phase specified in the appropriate bits of the TARGET COMMAND
  1095. * REGISTER match the STATUS REGISTER
  1096. */
  1097. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
  1098. do {
  1099. /*
  1100. * Wait for assertion of REQ, after which the phase bits will be
  1101. * valid
  1102. */
  1103. if (NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ | SR_BSY,
  1104. SR_REQ | SR_BSY, HZ * can_sleep) < 0)
  1105. break;
  1106. dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
  1107. /* Check for phase mismatch */
  1108. if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
  1109. dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
  1110. NCR5380_dprint_phase(NDEBUG_PIO, instance);
  1111. break;
  1112. }
  1113. /* Do actual transfer from SCSI bus to / from memory */
  1114. if (!(p & SR_IO))
  1115. NCR5380_write(OUTPUT_DATA_REG, *d);
  1116. else
  1117. *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
  1118. ++d;
  1119. /*
  1120. * The SCSI standard suggests that in MSGOUT phase, the initiator
  1121. * should drop ATN on the last byte of the message phase
  1122. * after REQ has been asserted for the handshake but before
  1123. * the initiator raises ACK.
  1124. */
  1125. if (!(p & SR_IO)) {
  1126. if (!((p & SR_MSG) && c > 1)) {
  1127. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
  1128. NCR5380_dprint(NDEBUG_PIO, instance);
  1129. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
  1130. ICR_ASSERT_DATA | ICR_ASSERT_ACK);
  1131. } else {
  1132. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
  1133. ICR_ASSERT_DATA | ICR_ASSERT_ATN);
  1134. NCR5380_dprint(NDEBUG_PIO, instance);
  1135. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
  1136. ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
  1137. }
  1138. } else {
  1139. NCR5380_dprint(NDEBUG_PIO, instance);
  1140. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
  1141. }
  1142. if (NCR5380_poll_politely(hostdata,
  1143. STATUS_REG, SR_REQ, 0, 5 * HZ * can_sleep) < 0)
  1144. break;
  1145. dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
  1146. /*
  1147. * We have several special cases to consider during REQ/ACK
  1148. * handshaking:
  1149. *
  1150. * 1. We were in MSGOUT phase, and we are on the last byte of
  1151. * the message. ATN must be dropped as ACK is dropped.
  1152. *
  1153. * 2. We are in MSGIN phase, and we are on the last byte of the
  1154. * message. We must exit with ACK asserted, so that the calling
  1155. * code may raise ATN before dropping ACK to reject the message.
  1156. *
  1157. * 3. ACK and ATN are clear & the target may proceed as normal.
  1158. */
  1159. if (!(p == PHASE_MSGIN && c == 1)) {
  1160. if (p == PHASE_MSGOUT && c > 1)
  1161. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1162. else
  1163. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1164. }
  1165. } while (--c);
  1166. dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
  1167. *count = c;
  1168. *data = d;
  1169. tmp = NCR5380_read(STATUS_REG);
  1170. /* The phase read from the bus is valid if either REQ is (already)
  1171. * asserted or if ACK hasn't been released yet. The latter applies if
  1172. * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
  1173. */
  1174. if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
  1175. *phase = tmp & PHASE_MASK;
  1176. else
  1177. *phase = PHASE_UNKNOWN;
  1178. }
  1179. /**
  1180. * do_reset - issue a reset command
  1181. * @instance: adapter to reset
  1182. *
  1183. * Issue a reset sequence to the NCR5380 and try and get the bus
  1184. * back into sane shape.
  1185. *
  1186. * This clears the reset interrupt flag because there may be no handler for
  1187. * it. When the driver is initialized, the NCR5380_intr() handler has not yet
  1188. * been installed. And when in EH we may have released the ST DMA interrupt.
  1189. */
  1190. static void do_reset(struct Scsi_Host *instance)
  1191. {
  1192. struct NCR5380_hostdata __maybe_unused *hostdata = shost_priv(instance);
  1193. unsigned long flags;
  1194. local_irq_save(flags);
  1195. NCR5380_write(TARGET_COMMAND_REG,
  1196. PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
  1197. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
  1198. udelay(50);
  1199. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1200. (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1201. local_irq_restore(flags);
  1202. }
  1203. /**
  1204. * do_abort - abort the currently established nexus by going to
  1205. * MESSAGE OUT phase and sending an ABORT message.
  1206. * @instance: relevant scsi host instance
  1207. * @can_sleep: 1 or 0 when sleeping is permitted or not, respectively
  1208. *
  1209. * Returns 0 on success, negative error code on failure.
  1210. */
  1211. static int do_abort(struct Scsi_Host *instance, unsigned int can_sleep)
  1212. {
  1213. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  1214. unsigned char *msgptr, phase, tmp;
  1215. int len;
  1216. int rc;
  1217. /* Request message out phase */
  1218. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1219. /*
  1220. * Wait for the target to indicate a valid phase by asserting
  1221. * REQ. Once this happens, we'll have either a MSGOUT phase
  1222. * and can immediately send the ABORT message, or we'll have some
  1223. * other phase and will have to source/sink data.
  1224. *
  1225. * We really don't care what value was on the bus or what value
  1226. * the target sees, so we just handshake.
  1227. */
  1228. rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ,
  1229. 10 * HZ * can_sleep);
  1230. if (rc < 0)
  1231. goto out;
  1232. tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
  1233. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
  1234. if (tmp != PHASE_MSGOUT) {
  1235. NCR5380_write(INITIATOR_COMMAND_REG,
  1236. ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
  1237. rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, 0,
  1238. 3 * HZ * can_sleep);
  1239. if (rc < 0)
  1240. goto out;
  1241. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1242. }
  1243. tmp = ABORT;
  1244. msgptr = &tmp;
  1245. len = 1;
  1246. phase = PHASE_MSGOUT;
  1247. NCR5380_transfer_pio(instance, &phase, &len, &msgptr, can_sleep);
  1248. if (len)
  1249. rc = -ENXIO;
  1250. /*
  1251. * If we got here, and the command completed successfully,
  1252. * we're about to go into bus free state.
  1253. */
  1254. out:
  1255. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1256. return rc;
  1257. }
  1258. /*
  1259. * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
  1260. * unsigned char *phase, int *count, unsigned char **data)
  1261. *
  1262. * Purpose : transfers data in given phase using either real
  1263. * or pseudo DMA.
  1264. *
  1265. * Inputs : instance - instance of driver, *phase - pointer to
  1266. * what phase is expected, *count - pointer to number of
  1267. * bytes to transfer, **data - pointer to data pointer.
  1268. *
  1269. * Returns : -1 when different phase is entered without transferring
  1270. * maximum number of bytes, 0 if all bytes or transferred or exit
  1271. * is in same phase.
  1272. *
  1273. * Also, *phase, *count, *data are modified in place.
  1274. */
  1275. static int NCR5380_transfer_dma(struct Scsi_Host *instance,
  1276. unsigned char *phase, int *count,
  1277. unsigned char **data)
  1278. {
  1279. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  1280. struct NCR5380_cmd *ncmd = NCR5380_to_ncmd(hostdata->connected);
  1281. int c = *count;
  1282. unsigned char p = *phase;
  1283. unsigned char *d = *data;
  1284. unsigned char tmp;
  1285. int result = 0;
  1286. if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
  1287. *phase = tmp;
  1288. return -1;
  1289. }
  1290. ncmd->phase = p;
  1291. if (p & SR_IO) {
  1292. if (hostdata->read_overruns)
  1293. c -= hostdata->read_overruns;
  1294. else if (hostdata->flags & FLAG_DMA_FIXUP)
  1295. --c;
  1296. }
  1297. dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
  1298. (p & SR_IO) ? "receive" : "send", c, d);
  1299. #ifdef CONFIG_SUN3
  1300. /* send start chain */
  1301. sun3scsi_dma_start(c, *data);
  1302. #endif
  1303. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
  1304. NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
  1305. MR_ENABLE_EOP_INTR);
  1306. if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
  1307. /* On the Medusa, it is a must to initialize the DMA before
  1308. * starting the NCR. This is also the cleaner way for the TT.
  1309. */
  1310. if (p & SR_IO)
  1311. result = NCR5380_dma_recv_setup(hostdata, d, c);
  1312. else
  1313. result = NCR5380_dma_send_setup(hostdata, d, c);
  1314. }
  1315. /*
  1316. * On the PAS16 at least I/O recovery delays are not needed here.
  1317. * Everyone else seems to want them.
  1318. */
  1319. if (p & SR_IO) {
  1320. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1321. NCR5380_io_delay(1);
  1322. NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
  1323. } else {
  1324. NCR5380_io_delay(1);
  1325. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
  1326. NCR5380_io_delay(1);
  1327. NCR5380_write(START_DMA_SEND_REG, 0);
  1328. NCR5380_io_delay(1);
  1329. }
  1330. #ifdef CONFIG_SUN3
  1331. #ifdef SUN3_SCSI_VME
  1332. dregs->csr |= CSR_DMA_ENABLE;
  1333. #endif
  1334. sun3_dma_active = 1;
  1335. #endif
  1336. if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
  1337. /* On the Falcon, the DMA setup must be done after the last
  1338. * NCR access, else the DMA setup gets trashed!
  1339. */
  1340. if (p & SR_IO)
  1341. result = NCR5380_dma_recv_setup(hostdata, d, c);
  1342. else
  1343. result = NCR5380_dma_send_setup(hostdata, d, c);
  1344. }
  1345. /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
  1346. if (result < 0)
  1347. return result;
  1348. /* For real DMA, result is the byte count. DMA interrupt is expected. */
  1349. if (result > 0) {
  1350. hostdata->dma_len = result;
  1351. return 0;
  1352. }
  1353. /* The result is zero iff pseudo DMA send/receive was completed. */
  1354. hostdata->dma_len = c;
  1355. /*
  1356. * A note regarding the DMA errata workarounds for early NMOS silicon.
  1357. *
  1358. * For DMA sends, we want to wait until the last byte has been
  1359. * transferred out over the bus before we turn off DMA mode. Alas, there
  1360. * seems to be no terribly good way of doing this on a 5380 under all
  1361. * conditions. For non-scatter-gather operations, we can wait until REQ
  1362. * and ACK both go false, or until a phase mismatch occurs. Gather-sends
  1363. * are nastier, since the device will be expecting more data than we
  1364. * are prepared to send it, and REQ will remain asserted. On a 53C8[01]
  1365. * we could test Last Byte Sent to assure transfer (I imagine this is
  1366. * precisely why this signal was added to the newer chips) but on the
  1367. * older 538[01] this signal does not exist. The workaround for this
  1368. * lack is a watchdog; we bail out of the wait-loop after a modest
  1369. * amount of wait-time if the usual exit conditions are not met.
  1370. * Not a terribly clean or correct solution :-%
  1371. *
  1372. * DMA receive is equally tricky due to a nasty characteristic of the
  1373. * NCR5380. If the chip is in DMA receive mode, it will respond to a
  1374. * target's REQ by latching the SCSI data into the INPUT DATA register
  1375. * and asserting ACK, even if it has _already_ been notified by the
  1376. * DMA controller that the current DMA transfer has completed! If the
  1377. * NCR5380 is then taken out of DMA mode, this already-acknowledged
  1378. * byte is lost.
  1379. *
  1380. * This is not a problem for "one DMA transfer per READ
  1381. * command", because the situation will never arise... either all of
  1382. * the data is DMA'ed properly, or the target switches to MESSAGE IN
  1383. * phase to signal a disconnection (either operation bringing the DMA
  1384. * to a clean halt). However, in order to handle scatter-receive, we
  1385. * must work around the problem. The chosen fix is to DMA fewer bytes,
  1386. * then check for the condition before taking the NCR5380 out of DMA
  1387. * mode. One or two extra bytes are transferred via PIO as necessary
  1388. * to fill out the original request.
  1389. */
  1390. if ((hostdata->flags & FLAG_DMA_FIXUP) &&
  1391. (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) {
  1392. /*
  1393. * The workaround was to transfer fewer bytes than we
  1394. * intended to with the pseudo-DMA receive function, wait for
  1395. * the chip to latch the last byte, read it, and then disable
  1396. * DMA mode.
  1397. *
  1398. * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
  1399. * REQ is deasserted when ACK is asserted, and not reasserted
  1400. * until ACK goes false. Since the NCR5380 won't lower ACK
  1401. * until DACK is asserted, which won't happen unless we twiddle
  1402. * the DMA port or we take the NCR5380 out of DMA mode, we
  1403. * can guarantee that we won't handshake another extra
  1404. * byte.
  1405. *
  1406. * If sending, wait for the last byte to be sent. If REQ is
  1407. * being asserted for the byte we're interested, we'll ACK it
  1408. * and it will go false.
  1409. */
  1410. if (!NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
  1411. BASR_DRQ, BASR_DRQ, 0)) {
  1412. if ((p & SR_IO) &&
  1413. (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) {
  1414. if (!NCR5380_poll_politely(hostdata, STATUS_REG,
  1415. SR_REQ, 0, 0)) {
  1416. d[c] = NCR5380_read(INPUT_DATA_REG);
  1417. --ncmd->this_residual;
  1418. } else {
  1419. result = -1;
  1420. scmd_printk(KERN_ERR, hostdata->connected,
  1421. "PDMA fixup: !REQ timeout\n");
  1422. }
  1423. }
  1424. } else if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH) {
  1425. result = -1;
  1426. scmd_printk(KERN_ERR, hostdata->connected,
  1427. "PDMA fixup: DRQ timeout\n");
  1428. }
  1429. }
  1430. NCR5380_dma_complete(instance);
  1431. return result;
  1432. }
  1433. /*
  1434. * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
  1435. *
  1436. * Purpose : run through the various SCSI phases and do as the target
  1437. * directs us to. Operates on the currently connected command,
  1438. * instance->connected.
  1439. *
  1440. * Inputs : instance, instance for which we are doing commands
  1441. *
  1442. * Side effects : SCSI things happen, the disconnected queue will be
  1443. * modified if a command disconnects, *instance->connected will
  1444. * change.
  1445. */
  1446. static void NCR5380_information_transfer(struct Scsi_Host *instance)
  1447. __releases(&hostdata->lock) __acquires(&hostdata->lock)
  1448. {
  1449. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  1450. unsigned char msgout = NOP;
  1451. int sink = 0;
  1452. int len;
  1453. int transfersize;
  1454. unsigned char *data;
  1455. unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
  1456. struct scsi_cmnd *cmd;
  1457. #ifdef SUN3_SCSI_VME
  1458. dregs->csr |= CSR_INTR;
  1459. #endif
  1460. while ((cmd = hostdata->connected)) {
  1461. struct NCR5380_cmd *ncmd = NCR5380_to_ncmd(cmd);
  1462. tmp = NCR5380_read(STATUS_REG);
  1463. /* We only have a valid SCSI phase when REQ is asserted */
  1464. if (tmp & SR_REQ) {
  1465. phase = (tmp & PHASE_MASK);
  1466. if (phase != old_phase) {
  1467. old_phase = phase;
  1468. NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
  1469. }
  1470. #ifdef CONFIG_SUN3
  1471. if (phase == PHASE_CMDOUT &&
  1472. sun3_dma_setup_done != cmd) {
  1473. int count;
  1474. advance_sg_buffer(ncmd);
  1475. count = sun3scsi_dma_xfer_len(hostdata, cmd);
  1476. if (count > 0) {
  1477. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  1478. sun3scsi_dma_send_setup(hostdata,
  1479. ncmd->ptr, count);
  1480. else
  1481. sun3scsi_dma_recv_setup(hostdata,
  1482. ncmd->ptr, count);
  1483. sun3_dma_setup_done = cmd;
  1484. }
  1485. #ifdef SUN3_SCSI_VME
  1486. dregs->csr |= CSR_INTR;
  1487. #endif
  1488. }
  1489. #endif /* CONFIG_SUN3 */
  1490. if (sink && (phase != PHASE_MSGOUT)) {
  1491. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
  1492. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
  1493. ICR_ASSERT_ACK);
  1494. while (NCR5380_read(STATUS_REG) & SR_REQ)
  1495. ;
  1496. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
  1497. ICR_ASSERT_ATN);
  1498. sink = 0;
  1499. continue;
  1500. }
  1501. switch (phase) {
  1502. case PHASE_DATAOUT:
  1503. #if (NDEBUG & NDEBUG_NO_DATAOUT)
  1504. shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
  1505. sink = 1;
  1506. do_abort(instance, 0);
  1507. cmd->result = DID_ERROR << 16;
  1508. complete_cmd(instance, cmd);
  1509. hostdata->connected = NULL;
  1510. hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
  1511. return;
  1512. #endif
  1513. case PHASE_DATAIN:
  1514. /*
  1515. * If there is no room left in the current buffer in the
  1516. * scatter-gather list, move onto the next one.
  1517. */
  1518. advance_sg_buffer(ncmd);
  1519. dsprintk(NDEBUG_INFORMATION, instance,
  1520. "this residual %d, sg ents %d\n",
  1521. ncmd->this_residual,
  1522. sg_nents(ncmd->buffer));
  1523. /*
  1524. * The preferred transfer method is going to be
  1525. * PSEUDO-DMA for systems that are strictly PIO,
  1526. * since we can let the hardware do the handshaking.
  1527. *
  1528. * For this to work, we need to know the transfersize
  1529. * ahead of time, since the pseudo-DMA code will sit
  1530. * in an unconditional loop.
  1531. */
  1532. transfersize = 0;
  1533. if (!cmd->device->borken)
  1534. transfersize = NCR5380_dma_xfer_len(hostdata, cmd);
  1535. if (transfersize > 0) {
  1536. len = transfersize;
  1537. if (NCR5380_transfer_dma(instance, &phase,
  1538. &len, (unsigned char **)&ncmd->ptr)) {
  1539. /*
  1540. * If the watchdog timer fires, all future
  1541. * accesses to this device will use the
  1542. * polled-IO.
  1543. */
  1544. scmd_printk(KERN_INFO, cmd,
  1545. "switching to slow handshake\n");
  1546. cmd->device->borken = 1;
  1547. do_reset(instance);
  1548. bus_reset_cleanup(instance);
  1549. }
  1550. } else {
  1551. /* Transfer a small chunk so that the
  1552. * irq mode lock is not held too long.
  1553. */
  1554. transfersize = min(ncmd->this_residual,
  1555. NCR5380_PIO_CHUNK_SIZE);
  1556. len = transfersize;
  1557. NCR5380_transfer_pio(instance, &phase, &len,
  1558. (unsigned char **)&ncmd->ptr,
  1559. 0);
  1560. ncmd->this_residual -= transfersize - len;
  1561. }
  1562. #ifdef CONFIG_SUN3
  1563. if (sun3_dma_setup_done == cmd)
  1564. sun3_dma_setup_done = NULL;
  1565. #endif
  1566. return;
  1567. case PHASE_MSGIN:
  1568. len = 1;
  1569. tmp = 0xff;
  1570. data = &tmp;
  1571. NCR5380_transfer_pio(instance, &phase, &len, &data, 0);
  1572. if (tmp == 0xff)
  1573. break;
  1574. switch (tmp) {
  1575. case ABORT:
  1576. set_host_byte(cmd, DID_ABORT);
  1577. fallthrough;
  1578. case COMMAND_COMPLETE:
  1579. /* Accept message by clearing ACK */
  1580. sink = 1;
  1581. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1582. dsprintk(NDEBUG_QUEUES, instance,
  1583. "COMMAND COMPLETE %p target %d lun %llu\n",
  1584. cmd, scmd_id(cmd), cmd->device->lun);
  1585. hostdata->connected = NULL;
  1586. hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
  1587. set_status_byte(cmd, ncmd->status);
  1588. set_resid_from_SCp(cmd);
  1589. if (cmd->cmnd[0] == REQUEST_SENSE)
  1590. complete_cmd(instance, cmd);
  1591. else {
  1592. if (ncmd->status == SAM_STAT_CHECK_CONDITION ||
  1593. ncmd->status == SAM_STAT_COMMAND_TERMINATED) {
  1594. dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
  1595. cmd);
  1596. list_add_tail(&ncmd->list,
  1597. &hostdata->autosense);
  1598. } else
  1599. complete_cmd(instance, cmd);
  1600. }
  1601. /*
  1602. * Restore phase bits to 0 so an interrupted selection,
  1603. * arbitration can resume.
  1604. */
  1605. NCR5380_write(TARGET_COMMAND_REG, 0);
  1606. return;
  1607. case MESSAGE_REJECT:
  1608. /* Accept message by clearing ACK */
  1609. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1610. switch (hostdata->last_message) {
  1611. case HEAD_OF_QUEUE_TAG:
  1612. case ORDERED_QUEUE_TAG:
  1613. case SIMPLE_QUEUE_TAG:
  1614. cmd->device->simple_tags = 0;
  1615. hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
  1616. break;
  1617. default:
  1618. break;
  1619. }
  1620. break;
  1621. case DISCONNECT:
  1622. /* Accept message by clearing ACK */
  1623. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1624. hostdata->connected = NULL;
  1625. list_add(&ncmd->list, &hostdata->disconnected);
  1626. dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
  1627. instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
  1628. cmd, scmd_id(cmd), cmd->device->lun);
  1629. /*
  1630. * Restore phase bits to 0 so an interrupted selection,
  1631. * arbitration can resume.
  1632. */
  1633. NCR5380_write(TARGET_COMMAND_REG, 0);
  1634. #ifdef SUN3_SCSI_VME
  1635. dregs->csr |= CSR_DMA_ENABLE;
  1636. #endif
  1637. return;
  1638. /*
  1639. * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
  1640. * operation, in violation of the SCSI spec so we can safely
  1641. * ignore SAVE/RESTORE pointers calls.
  1642. *
  1643. * Unfortunately, some disks violate the SCSI spec and
  1644. * don't issue the required SAVE_POINTERS message before
  1645. * disconnecting, and we have to break spec to remain
  1646. * compatible.
  1647. */
  1648. case SAVE_POINTERS:
  1649. case RESTORE_POINTERS:
  1650. /* Accept message by clearing ACK */
  1651. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1652. break;
  1653. case EXTENDED_MESSAGE:
  1654. /*
  1655. * Start the message buffer with the EXTENDED_MESSAGE
  1656. * byte, since spi_print_msg() wants the whole thing.
  1657. */
  1658. extended_msg[0] = EXTENDED_MESSAGE;
  1659. /* Accept first byte by clearing ACK */
  1660. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1661. spin_unlock_irq(&hostdata->lock);
  1662. dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
  1663. len = 2;
  1664. data = extended_msg + 1;
  1665. phase = PHASE_MSGIN;
  1666. NCR5380_transfer_pio(instance, &phase, &len, &data, 1);
  1667. dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
  1668. (int)extended_msg[1],
  1669. (int)extended_msg[2]);
  1670. if (!len && extended_msg[1] > 0 &&
  1671. extended_msg[1] <= sizeof(extended_msg) - 2) {
  1672. /* Accept third byte by clearing ACK */
  1673. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1674. len = extended_msg[1] - 1;
  1675. data = extended_msg + 3;
  1676. phase = PHASE_MSGIN;
  1677. NCR5380_transfer_pio(instance, &phase, &len, &data, 1);
  1678. dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
  1679. len);
  1680. switch (extended_msg[2]) {
  1681. case EXTENDED_SDTR:
  1682. case EXTENDED_WDTR:
  1683. tmp = 0;
  1684. }
  1685. } else if (len) {
  1686. shost_printk(KERN_ERR, instance, "error receiving extended message\n");
  1687. tmp = 0;
  1688. } else {
  1689. shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
  1690. extended_msg[2], extended_msg[1]);
  1691. tmp = 0;
  1692. }
  1693. spin_lock_irq(&hostdata->lock);
  1694. if (!hostdata->connected)
  1695. return;
  1696. /* Reject message */
  1697. fallthrough;
  1698. default:
  1699. /*
  1700. * If we get something weird that we aren't expecting,
  1701. * log it.
  1702. */
  1703. if (tmp == EXTENDED_MESSAGE)
  1704. scmd_printk(KERN_INFO, cmd,
  1705. "rejecting unknown extended message code %02x, length %d\n",
  1706. extended_msg[2], extended_msg[1]);
  1707. else if (tmp)
  1708. scmd_printk(KERN_INFO, cmd,
  1709. "rejecting unknown message code %02x\n",
  1710. tmp);
  1711. msgout = MESSAGE_REJECT;
  1712. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1713. break;
  1714. } /* switch (tmp) */
  1715. break;
  1716. case PHASE_MSGOUT:
  1717. len = 1;
  1718. data = &msgout;
  1719. hostdata->last_message = msgout;
  1720. NCR5380_transfer_pio(instance, &phase, &len, &data, 0);
  1721. if (msgout == ABORT) {
  1722. hostdata->connected = NULL;
  1723. hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
  1724. cmd->result = DID_ERROR << 16;
  1725. complete_cmd(instance, cmd);
  1726. return;
  1727. }
  1728. msgout = NOP;
  1729. break;
  1730. case PHASE_CMDOUT:
  1731. len = cmd->cmd_len;
  1732. data = cmd->cmnd;
  1733. /*
  1734. * XXX for performance reasons, on machines with a
  1735. * PSEUDO-DMA architecture we should probably
  1736. * use the dma transfer function.
  1737. */
  1738. NCR5380_transfer_pio(instance, &phase, &len, &data, 0);
  1739. break;
  1740. case PHASE_STATIN:
  1741. len = 1;
  1742. tmp = ncmd->status;
  1743. data = &tmp;
  1744. NCR5380_transfer_pio(instance, &phase, &len, &data, 0);
  1745. ncmd->status = tmp;
  1746. break;
  1747. default:
  1748. shost_printk(KERN_ERR, instance, "unknown phase\n");
  1749. NCR5380_dprint(NDEBUG_ANY, instance);
  1750. } /* switch(phase) */
  1751. } else {
  1752. int err;
  1753. spin_unlock_irq(&hostdata->lock);
  1754. err = NCR5380_poll_politely(hostdata, STATUS_REG,
  1755. SR_REQ, SR_REQ, HZ);
  1756. spin_lock_irq(&hostdata->lock);
  1757. if (err < 0 && hostdata->connected &&
  1758. !(NCR5380_read(STATUS_REG) & SR_BSY)) {
  1759. scmd_printk(KERN_ERR, hostdata->connected,
  1760. "BSY signal lost\n");
  1761. do_reset(instance);
  1762. bus_reset_cleanup(instance);
  1763. }
  1764. }
  1765. }
  1766. }
  1767. /*
  1768. * Function : void NCR5380_reselect (struct Scsi_Host *instance)
  1769. *
  1770. * Purpose : does reselection, initializing the instance->connected
  1771. * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
  1772. * nexus has been reestablished,
  1773. *
  1774. * Inputs : instance - this instance of the NCR5380.
  1775. */
  1776. static void NCR5380_reselect(struct Scsi_Host *instance)
  1777. {
  1778. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  1779. unsigned char target_mask;
  1780. unsigned char lun;
  1781. unsigned char msg[3];
  1782. struct NCR5380_cmd *ncmd;
  1783. struct scsi_cmnd *tmp;
  1784. /*
  1785. * Disable arbitration, etc. since the host adapter obviously
  1786. * lost, and tell an interrupted NCR5380_select() to restart.
  1787. */
  1788. NCR5380_write(MODE_REG, MR_BASE);
  1789. target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
  1790. if (!target_mask || target_mask & (target_mask - 1)) {
  1791. shost_printk(KERN_WARNING, instance,
  1792. "reselect: bad target_mask 0x%02x\n", target_mask);
  1793. return;
  1794. }
  1795. /*
  1796. * At this point, we have detected that our SCSI ID is on the bus,
  1797. * SEL is true and BSY was false for at least one bus settle delay
  1798. * (400 ns).
  1799. *
  1800. * We must assert BSY ourselves, until the target drops the SEL
  1801. * signal.
  1802. */
  1803. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
  1804. if (NCR5380_poll_politely(hostdata,
  1805. STATUS_REG, SR_SEL, 0, 0) < 0) {
  1806. shost_printk(KERN_ERR, instance, "reselect: !SEL timeout\n");
  1807. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1808. return;
  1809. }
  1810. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1811. /*
  1812. * Wait for target to go into MSGIN.
  1813. */
  1814. if (NCR5380_poll_politely(hostdata,
  1815. STATUS_REG, SR_REQ, SR_REQ, 0) < 0) {
  1816. if ((NCR5380_read(STATUS_REG) & (SR_BSY | SR_SEL)) == 0)
  1817. /* BUS FREE phase */
  1818. return;
  1819. shost_printk(KERN_ERR, instance, "reselect: REQ timeout\n");
  1820. do_abort(instance, 0);
  1821. return;
  1822. }
  1823. #ifdef CONFIG_SUN3
  1824. /* acknowledge toggle to MSGIN */
  1825. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
  1826. /* peek at the byte without really hitting the bus */
  1827. msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
  1828. #else
  1829. {
  1830. int len = 1;
  1831. unsigned char *data = msg;
  1832. unsigned char phase = PHASE_MSGIN;
  1833. NCR5380_transfer_pio(instance, &phase, &len, &data, 0);
  1834. if (len) {
  1835. do_abort(instance, 0);
  1836. return;
  1837. }
  1838. }
  1839. #endif /* CONFIG_SUN3 */
  1840. if (!(msg[0] & 0x80)) {
  1841. shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
  1842. spi_print_msg(msg);
  1843. printk("\n");
  1844. do_abort(instance, 0);
  1845. return;
  1846. }
  1847. lun = msg[0] & 0x07;
  1848. /*
  1849. * We need to add code for SCSI-II to track which devices have
  1850. * I_T_L_Q nexuses established, and which have simple I_T_L
  1851. * nexuses so we can chose to do additional data transfer.
  1852. */
  1853. /*
  1854. * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
  1855. * just reestablished, and remove it from the disconnected queue.
  1856. */
  1857. tmp = NULL;
  1858. list_for_each_entry(ncmd, &hostdata->disconnected, list) {
  1859. struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
  1860. if (target_mask == (1 << scmd_id(cmd)) &&
  1861. lun == (u8)cmd->device->lun) {
  1862. list_del(&ncmd->list);
  1863. tmp = cmd;
  1864. break;
  1865. }
  1866. }
  1867. if (tmp) {
  1868. dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
  1869. "reselect: removed %p from disconnected queue\n", tmp);
  1870. } else {
  1871. int target = ffs(target_mask) - 1;
  1872. shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
  1873. target_mask, lun);
  1874. /*
  1875. * Since we have an established nexus that we can't do anything
  1876. * with, we must abort it.
  1877. */
  1878. if (do_abort(instance, 0) == 0)
  1879. hostdata->busy[target] &= ~(1 << lun);
  1880. return;
  1881. }
  1882. #ifdef CONFIG_SUN3
  1883. if (sun3_dma_setup_done != tmp) {
  1884. int count;
  1885. advance_sg_buffer(ncmd);
  1886. count = sun3scsi_dma_xfer_len(hostdata, tmp);
  1887. if (count > 0) {
  1888. if (tmp->sc_data_direction == DMA_TO_DEVICE)
  1889. sun3scsi_dma_send_setup(hostdata,
  1890. ncmd->ptr, count);
  1891. else
  1892. sun3scsi_dma_recv_setup(hostdata,
  1893. ncmd->ptr, count);
  1894. sun3_dma_setup_done = tmp;
  1895. }
  1896. }
  1897. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
  1898. #endif /* CONFIG_SUN3 */
  1899. /* Accept message by clearing ACK */
  1900. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1901. hostdata->connected = tmp;
  1902. dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu\n",
  1903. scmd_id(tmp), tmp->device->lun);
  1904. }
  1905. /**
  1906. * list_find_cmd - test for presence of a command in a linked list
  1907. * @haystack: list of commands
  1908. * @needle: command to search for
  1909. */
  1910. static bool list_find_cmd(struct list_head *haystack,
  1911. struct scsi_cmnd *needle)
  1912. {
  1913. struct NCR5380_cmd *ncmd;
  1914. list_for_each_entry(ncmd, haystack, list)
  1915. if (NCR5380_to_scmd(ncmd) == needle)
  1916. return true;
  1917. return false;
  1918. }
  1919. /**
  1920. * list_remove_cmd - remove a command from linked list
  1921. * @haystack: list of commands
  1922. * @needle: command to remove
  1923. */
  1924. static bool list_del_cmd(struct list_head *haystack,
  1925. struct scsi_cmnd *needle)
  1926. {
  1927. if (list_find_cmd(haystack, needle)) {
  1928. struct NCR5380_cmd *ncmd = NCR5380_to_ncmd(needle);
  1929. list_del(&ncmd->list);
  1930. return true;
  1931. }
  1932. return false;
  1933. }
  1934. /**
  1935. * NCR5380_abort - scsi host eh_abort_handler() method
  1936. * @cmd: the command to be aborted
  1937. *
  1938. * Try to abort a given command by removing it from queues and/or sending
  1939. * the target an abort message. This may not succeed in causing a target
  1940. * to abort the command. Nonetheless, the low-level driver must forget about
  1941. * the command because the mid-layer reclaims it and it may be re-issued.
  1942. *
  1943. * The normal path taken by a command is as follows. For EH we trace this
  1944. * same path to locate and abort the command.
  1945. *
  1946. * unissued -> selecting -> [unissued -> selecting ->]... connected ->
  1947. * [disconnected -> connected ->]...
  1948. * [autosense -> connected ->] done
  1949. *
  1950. * If cmd was not found at all then presumably it has already been completed,
  1951. * in which case return SUCCESS to try to avoid further EH measures.
  1952. *
  1953. * If the command has not completed yet, we must not fail to find it.
  1954. * We have no option but to forget the aborted command (even if it still
  1955. * lacks sense data). The mid-layer may re-issue a command that is in error
  1956. * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
  1957. * this driver are such that a command can appear on one queue only.
  1958. *
  1959. * The lock protects driver data structures, but EH handlers also use it
  1960. * to serialize their own execution and prevent their own re-entry.
  1961. */
  1962. static int NCR5380_abort(struct scsi_cmnd *cmd)
  1963. {
  1964. struct Scsi_Host *instance = cmd->device->host;
  1965. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  1966. unsigned long flags;
  1967. int result = SUCCESS;
  1968. spin_lock_irqsave(&hostdata->lock, flags);
  1969. #if (NDEBUG & NDEBUG_ANY)
  1970. scmd_printk(KERN_INFO, cmd, __func__);
  1971. #endif
  1972. NCR5380_dprint(NDEBUG_ANY, instance);
  1973. NCR5380_dprint_phase(NDEBUG_ANY, instance);
  1974. if (list_del_cmd(&hostdata->unissued, cmd)) {
  1975. dsprintk(NDEBUG_ABORT, instance,
  1976. "abort: removed %p from issue queue\n", cmd);
  1977. cmd->result = DID_ABORT << 16;
  1978. scsi_done(cmd); /* No tag or busy flag to worry about */
  1979. goto out;
  1980. }
  1981. if (hostdata->selecting == cmd) {
  1982. dsprintk(NDEBUG_ABORT, instance,
  1983. "abort: cmd %p == selecting\n", cmd);
  1984. hostdata->selecting = NULL;
  1985. cmd->result = DID_ABORT << 16;
  1986. complete_cmd(instance, cmd);
  1987. goto out;
  1988. }
  1989. if (list_del_cmd(&hostdata->disconnected, cmd)) {
  1990. dsprintk(NDEBUG_ABORT, instance,
  1991. "abort: removed %p from disconnected list\n", cmd);
  1992. /* Can't call NCR5380_select() and send ABORT because that
  1993. * means releasing the lock. Need a bus reset.
  1994. */
  1995. set_host_byte(cmd, DID_ERROR);
  1996. complete_cmd(instance, cmd);
  1997. result = FAILED;
  1998. goto out;
  1999. }
  2000. if (hostdata->connected == cmd) {
  2001. dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
  2002. hostdata->connected = NULL;
  2003. hostdata->dma_len = 0;
  2004. if (do_abort(instance, 0) < 0) {
  2005. set_host_byte(cmd, DID_ERROR);
  2006. complete_cmd(instance, cmd);
  2007. result = FAILED;
  2008. goto out;
  2009. }
  2010. set_host_byte(cmd, DID_ABORT);
  2011. complete_cmd(instance, cmd);
  2012. goto out;
  2013. }
  2014. if (list_del_cmd(&hostdata->autosense, cmd)) {
  2015. dsprintk(NDEBUG_ABORT, instance,
  2016. "abort: removed %p from sense queue\n", cmd);
  2017. complete_cmd(instance, cmd);
  2018. }
  2019. out:
  2020. if (result == FAILED)
  2021. dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
  2022. else {
  2023. hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
  2024. dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
  2025. }
  2026. queue_work(hostdata->work_q, &hostdata->main_task);
  2027. spin_unlock_irqrestore(&hostdata->lock, flags);
  2028. return result;
  2029. }
  2030. static void bus_reset_cleanup(struct Scsi_Host *instance)
  2031. {
  2032. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  2033. int i;
  2034. struct NCR5380_cmd *ncmd;
  2035. /* reset NCR registers */
  2036. NCR5380_write(MODE_REG, MR_BASE);
  2037. NCR5380_write(TARGET_COMMAND_REG, 0);
  2038. NCR5380_write(SELECT_ENABLE_REG, 0);
  2039. /* After the reset, there are no more connected or disconnected commands
  2040. * and no busy units; so clear the low-level status here to avoid
  2041. * conflicts when the mid-level code tries to wake up the affected
  2042. * commands!
  2043. */
  2044. if (hostdata->selecting) {
  2045. hostdata->selecting->result = DID_RESET << 16;
  2046. complete_cmd(instance, hostdata->selecting);
  2047. hostdata->selecting = NULL;
  2048. }
  2049. list_for_each_entry(ncmd, &hostdata->disconnected, list) {
  2050. struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
  2051. set_host_byte(cmd, DID_RESET);
  2052. complete_cmd(instance, cmd);
  2053. }
  2054. INIT_LIST_HEAD(&hostdata->disconnected);
  2055. list_for_each_entry(ncmd, &hostdata->autosense, list) {
  2056. struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
  2057. scsi_done(cmd);
  2058. }
  2059. INIT_LIST_HEAD(&hostdata->autosense);
  2060. if (hostdata->connected) {
  2061. set_host_byte(hostdata->connected, DID_RESET);
  2062. complete_cmd(instance, hostdata->connected);
  2063. hostdata->connected = NULL;
  2064. }
  2065. for (i = 0; i < 8; ++i)
  2066. hostdata->busy[i] = 0;
  2067. hostdata->dma_len = 0;
  2068. queue_work(hostdata->work_q, &hostdata->main_task);
  2069. }
  2070. /**
  2071. * NCR5380_host_reset - reset the SCSI host
  2072. * @cmd: SCSI command undergoing EH
  2073. *
  2074. * Returns SUCCESS
  2075. */
  2076. static int NCR5380_host_reset(struct scsi_cmnd *cmd)
  2077. {
  2078. struct Scsi_Host *instance = cmd->device->host;
  2079. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  2080. unsigned long flags;
  2081. struct NCR5380_cmd *ncmd;
  2082. spin_lock_irqsave(&hostdata->lock, flags);
  2083. #if (NDEBUG & NDEBUG_ANY)
  2084. shost_printk(KERN_INFO, instance, __func__);
  2085. #endif
  2086. NCR5380_dprint(NDEBUG_ANY, instance);
  2087. NCR5380_dprint_phase(NDEBUG_ANY, instance);
  2088. list_for_each_entry(ncmd, &hostdata->unissued, list) {
  2089. struct scsi_cmnd *scmd = NCR5380_to_scmd(ncmd);
  2090. scmd->result = DID_RESET << 16;
  2091. scsi_done(scmd);
  2092. }
  2093. INIT_LIST_HEAD(&hostdata->unissued);
  2094. do_reset(instance);
  2095. bus_reset_cleanup(instance);
  2096. spin_unlock_irqrestore(&hostdata->lock, flags);
  2097. return SUCCESS;
  2098. }