NCR5380.c 70 KB

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