53c700.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174
  1. /* -*- mode: c; c-basic-offset: 8 -*- */
  2. /* NCR (or Symbios) 53c700 and 53c700-66 Driver
  3. *
  4. * Copyright (C) 2001 by James.Bottomley@HansenPartnership.com
  5. **-----------------------------------------------------------------------------
  6. **
  7. ** This program is free software; you can redistribute it and/or modify
  8. ** it under the terms of the GNU General Public License as published by
  9. ** the Free Software Foundation; either version 2 of the License, or
  10. ** (at your option) any later version.
  11. **
  12. ** This program is distributed in the hope that it will be useful,
  13. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ** GNU General Public License for more details.
  16. **
  17. ** You should have received a copy of the GNU General Public License
  18. ** along with this program; if not, write to the Free Software
  19. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. **
  21. **-----------------------------------------------------------------------------
  22. */
  23. /* Notes:
  24. *
  25. * This driver is designed exclusively for these chips (virtually the
  26. * earliest of the scripts engine chips). They need their own drivers
  27. * because they are missing so many of the scripts and snazzy register
  28. * features of their elder brothers (the 710, 720 and 770).
  29. *
  30. * The 700 is the lowliest of the line, it can only do async SCSI.
  31. * The 700-66 can at least do synchronous SCSI up to 10MHz.
  32. *
  33. * The 700 chip has no host bus interface logic of its own. However,
  34. * it is usually mapped to a location with well defined register
  35. * offsets. Therefore, if you can determine the base address and the
  36. * irq your board incorporating this chip uses, you can probably use
  37. * this driver to run it (although you'll probably have to write a
  38. * minimal wrapper for the purpose---see the NCR_D700 driver for
  39. * details about how to do this).
  40. *
  41. *
  42. * TODO List:
  43. *
  44. * 1. Better statistics in the proc fs
  45. *
  46. * 2. Implement message queue (queues SCSI messages like commands) and make
  47. * the abort and device reset functions use them.
  48. * */
  49. /* CHANGELOG
  50. *
  51. * Version 2.8
  52. *
  53. * Fixed bad bug affecting tag starvation processing (previously the
  54. * driver would hang the system if too many tags starved. Also fixed
  55. * bad bug having to do with 10 byte command processing and REQUEST
  56. * SENSE (the command would loop forever getting a transfer length
  57. * mismatch in the CMD phase).
  58. *
  59. * Version 2.7
  60. *
  61. * Fixed scripts problem which caused certain devices (notably CDRWs)
  62. * to hang on initial INQUIRY. Updated NCR_700_readl/writel to use
  63. * __raw_readl/writel for parisc compatibility (Thomas
  64. * Bogendoerfer). Added missing SCp->request_bufflen initialisation
  65. * for sense requests (Ryan Bradetich).
  66. *
  67. * Version 2.6
  68. *
  69. * Following test of the 64 bit parisc kernel by Richard Hirst,
  70. * several problems have now been corrected. Also adds support for
  71. * consistent memory allocation.
  72. *
  73. * Version 2.5
  74. *
  75. * More Compatibility changes for 710 (now actually works). Enhanced
  76. * support for odd clock speeds which constrain SDTR negotiations.
  77. * correct cacheline separation for scsi messages and status for
  78. * incoherent architectures. Use of the pci mapping functions on
  79. * buffers to begin support for 64 bit drivers.
  80. *
  81. * Version 2.4
  82. *
  83. * Added support for the 53c710 chip (in 53c700 emulation mode only---no
  84. * special 53c710 instructions or registers are used).
  85. *
  86. * Version 2.3
  87. *
  88. * More endianness/cache coherency changes.
  89. *
  90. * Better bad device handling (handles devices lying about tag
  91. * queueing support and devices which fail to provide sense data on
  92. * contingent allegiance conditions)
  93. *
  94. * Many thanks to Richard Hirst <rhirst@linuxcare.com> for patiently
  95. * debugging this driver on the parisc architecture and suggesting
  96. * many improvements and bug fixes.
  97. *
  98. * Thanks also go to Linuxcare Inc. for providing several PARISC
  99. * machines for me to debug the driver on.
  100. *
  101. * Version 2.2
  102. *
  103. * Made the driver mem or io mapped; added endian invariance; added
  104. * dma cache flushing operations for architectures which need it;
  105. * added support for more varied clocking speeds.
  106. *
  107. * Version 2.1
  108. *
  109. * Initial modularisation from the D700. See NCR_D700.c for the rest of
  110. * the changelog.
  111. * */
  112. #define NCR_700_VERSION "2.8"
  113. #include <linux/config.h>
  114. #include <linux/kernel.h>
  115. #include <linux/types.h>
  116. #include <linux/string.h>
  117. #include <linux/ioport.h>
  118. #include <linux/delay.h>
  119. #include <linux/spinlock.h>
  120. #include <linux/completion.h>
  121. #include <linux/sched.h>
  122. #include <linux/init.h>
  123. #include <linux/proc_fs.h>
  124. #include <linux/blkdev.h>
  125. #include <linux/module.h>
  126. #include <linux/interrupt.h>
  127. #include <asm/dma.h>
  128. #include <asm/system.h>
  129. #include <asm/io.h>
  130. #include <asm/pgtable.h>
  131. #include <asm/byteorder.h>
  132. #include <scsi/scsi.h>
  133. #include <scsi/scsi_cmnd.h>
  134. #include <scsi/scsi_dbg.h>
  135. #include <scsi/scsi_eh.h>
  136. #include <scsi/scsi_host.h>
  137. #include <scsi/scsi_tcq.h>
  138. #include <scsi/scsi_transport.h>
  139. #include <scsi/scsi_transport_spi.h>
  140. #include "53c700.h"
  141. /* NOTE: For 64 bit drivers there are points in the code where we use
  142. * a non dereferenceable pointer to point to a structure in dma-able
  143. * memory (which is 32 bits) so that we can use all of the structure
  144. * operations but take the address at the end. This macro allows us
  145. * to truncate the 64 bit pointer down to 32 bits without the compiler
  146. * complaining */
  147. #define to32bit(x) ((__u32)((unsigned long)(x)))
  148. #ifdef NCR_700_DEBUG
  149. #define STATIC
  150. #else
  151. #define STATIC static
  152. #endif
  153. MODULE_AUTHOR("James Bottomley");
  154. MODULE_DESCRIPTION("53c700 and 53c700-66 Driver");
  155. MODULE_LICENSE("GPL");
  156. /* This is the script */
  157. #include "53c700_d.h"
  158. STATIC int NCR_700_queuecommand(struct scsi_cmnd *, void (*done)(struct scsi_cmnd *));
  159. STATIC int NCR_700_abort(struct scsi_cmnd * SCpnt);
  160. STATIC int NCR_700_bus_reset(struct scsi_cmnd * SCpnt);
  161. STATIC int NCR_700_dev_reset(struct scsi_cmnd * SCpnt);
  162. STATIC int NCR_700_host_reset(struct scsi_cmnd * SCpnt);
  163. STATIC void NCR_700_chip_setup(struct Scsi_Host *host);
  164. STATIC void NCR_700_chip_reset(struct Scsi_Host *host);
  165. STATIC int NCR_700_slave_configure(struct scsi_device *SDpnt);
  166. STATIC void NCR_700_slave_destroy(struct scsi_device *SDpnt);
  167. static int NCR_700_change_queue_depth(struct scsi_device *SDpnt, int depth);
  168. static int NCR_700_change_queue_type(struct scsi_device *SDpnt, int depth);
  169. STATIC struct device_attribute *NCR_700_dev_attrs[];
  170. STATIC struct scsi_transport_template *NCR_700_transport_template = NULL;
  171. static char *NCR_700_phase[] = {
  172. "",
  173. "after selection",
  174. "before command phase",
  175. "after command phase",
  176. "after status phase",
  177. "after data in phase",
  178. "after data out phase",
  179. "during data phase",
  180. };
  181. static char *NCR_700_condition[] = {
  182. "",
  183. "NOT MSG_OUT",
  184. "UNEXPECTED PHASE",
  185. "NOT MSG_IN",
  186. "UNEXPECTED MSG",
  187. "MSG_IN",
  188. "SDTR_MSG RECEIVED",
  189. "REJECT_MSG RECEIVED",
  190. "DISCONNECT_MSG RECEIVED",
  191. "MSG_OUT",
  192. "DATA_IN",
  193. };
  194. static char *NCR_700_fatal_messages[] = {
  195. "unexpected message after reselection",
  196. "still MSG_OUT after message injection",
  197. "not MSG_IN after selection",
  198. "Illegal message length received",
  199. };
  200. static char *NCR_700_SBCL_bits[] = {
  201. "IO ",
  202. "CD ",
  203. "MSG ",
  204. "ATN ",
  205. "SEL ",
  206. "BSY ",
  207. "ACK ",
  208. "REQ ",
  209. };
  210. static char *NCR_700_SBCL_to_phase[] = {
  211. "DATA_OUT",
  212. "DATA_IN",
  213. "CMD_OUT",
  214. "STATE",
  215. "ILLEGAL PHASE",
  216. "ILLEGAL PHASE",
  217. "MSG OUT",
  218. "MSG IN",
  219. };
  220. static __u8 NCR_700_SDTR_msg[] = {
  221. 0x01, /* Extended message */
  222. 0x03, /* Extended message Length */
  223. 0x01, /* SDTR Extended message */
  224. NCR_700_MIN_PERIOD,
  225. NCR_700_MAX_OFFSET
  226. };
  227. /* This translates the SDTR message offset and period to a value
  228. * which can be loaded into the SXFER_REG.
  229. *
  230. * NOTE: According to SCSI-2, the true transfer period (in ns) is
  231. * actually four times this period value */
  232. static inline __u8
  233. NCR_700_offset_period_to_sxfer(struct NCR_700_Host_Parameters *hostdata,
  234. __u8 offset, __u8 period)
  235. {
  236. int XFERP;
  237. __u8 min_xferp = (hostdata->chip710
  238. ? NCR_710_MIN_XFERP : NCR_700_MIN_XFERP);
  239. __u8 max_offset = (hostdata->chip710
  240. ? NCR_710_MAX_OFFSET : NCR_700_MAX_OFFSET);
  241. if(offset == 0)
  242. return 0;
  243. if(period < hostdata->min_period) {
  244. printk(KERN_WARNING "53c700: Period %dns is less than this chip's minimum, setting to %d\n", period*4, NCR_700_SDTR_msg[3]*4);
  245. period = hostdata->min_period;
  246. }
  247. XFERP = (period*4 * hostdata->sync_clock)/1000 - 4;
  248. if(offset > max_offset) {
  249. printk(KERN_WARNING "53c700: Offset %d exceeds chip maximum, setting to %d\n",
  250. offset, max_offset);
  251. offset = max_offset;
  252. }
  253. if(XFERP < min_xferp) {
  254. printk(KERN_WARNING "53c700: XFERP %d is less than minium, setting to %d\n",
  255. XFERP, min_xferp);
  256. XFERP = min_xferp;
  257. }
  258. return (offset & 0x0f) | (XFERP & 0x07)<<4;
  259. }
  260. static inline __u8
  261. NCR_700_get_SXFER(struct scsi_device *SDp)
  262. {
  263. struct NCR_700_Host_Parameters *hostdata =
  264. (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
  265. return NCR_700_offset_period_to_sxfer(hostdata,
  266. spi_offset(SDp->sdev_target),
  267. spi_period(SDp->sdev_target));
  268. }
  269. struct Scsi_Host *
  270. NCR_700_detect(struct scsi_host_template *tpnt,
  271. struct NCR_700_Host_Parameters *hostdata, struct device *dev)
  272. {
  273. dma_addr_t pScript, pSlots;
  274. __u8 *memory;
  275. __u32 *script;
  276. struct Scsi_Host *host;
  277. static int banner = 0;
  278. int j;
  279. if(tpnt->sdev_attrs == NULL)
  280. tpnt->sdev_attrs = NCR_700_dev_attrs;
  281. memory = dma_alloc_noncoherent(hostdata->dev, TOTAL_MEM_SIZE,
  282. &pScript, GFP_KERNEL);
  283. if(memory == NULL) {
  284. printk(KERN_ERR "53c700: Failed to allocate memory for driver, detatching\n");
  285. return NULL;
  286. }
  287. script = (__u32 *)memory;
  288. hostdata->msgin = memory + MSGIN_OFFSET;
  289. hostdata->msgout = memory + MSGOUT_OFFSET;
  290. hostdata->status = memory + STATUS_OFFSET;
  291. /* all of these offsets are L1_CACHE_BYTES separated. It is fatal
  292. * if this isn't sufficient separation to avoid dma flushing issues */
  293. BUG_ON(!dma_is_consistent(pScript) && L1_CACHE_BYTES < dma_get_cache_alignment());
  294. hostdata->slots = (struct NCR_700_command_slot *)(memory + SLOTS_OFFSET);
  295. hostdata->dev = dev;
  296. pSlots = pScript + SLOTS_OFFSET;
  297. /* Fill in the missing routines from the host template */
  298. tpnt->queuecommand = NCR_700_queuecommand;
  299. tpnt->eh_abort_handler = NCR_700_abort;
  300. tpnt->eh_device_reset_handler = NCR_700_dev_reset;
  301. tpnt->eh_bus_reset_handler = NCR_700_bus_reset;
  302. tpnt->eh_host_reset_handler = NCR_700_host_reset;
  303. tpnt->can_queue = NCR_700_COMMAND_SLOTS_PER_HOST;
  304. tpnt->sg_tablesize = NCR_700_SG_SEGMENTS;
  305. tpnt->cmd_per_lun = NCR_700_CMD_PER_LUN;
  306. tpnt->use_clustering = ENABLE_CLUSTERING;
  307. tpnt->slave_configure = NCR_700_slave_configure;
  308. tpnt->slave_destroy = NCR_700_slave_destroy;
  309. tpnt->change_queue_depth = NCR_700_change_queue_depth;
  310. tpnt->change_queue_type = NCR_700_change_queue_type;
  311. if(tpnt->name == NULL)
  312. tpnt->name = "53c700";
  313. if(tpnt->proc_name == NULL)
  314. tpnt->proc_name = "53c700";
  315. host = scsi_host_alloc(tpnt, 4);
  316. if (!host)
  317. return NULL;
  318. memset(hostdata->slots, 0, sizeof(struct NCR_700_command_slot)
  319. * NCR_700_COMMAND_SLOTS_PER_HOST);
  320. for(j = 0; j < NCR_700_COMMAND_SLOTS_PER_HOST; j++) {
  321. dma_addr_t offset = (dma_addr_t)((unsigned long)&hostdata->slots[j].SG[0]
  322. - (unsigned long)&hostdata->slots[0].SG[0]);
  323. hostdata->slots[j].pSG = (struct NCR_700_SG_List *)((unsigned long)(pSlots + offset));
  324. if(j == 0)
  325. hostdata->free_list = &hostdata->slots[j];
  326. else
  327. hostdata->slots[j-1].ITL_forw = &hostdata->slots[j];
  328. hostdata->slots[j].state = NCR_700_SLOT_FREE;
  329. }
  330. for(j = 0; j < sizeof(SCRIPT)/sizeof(SCRIPT[0]); j++) {
  331. script[j] = bS_to_host(SCRIPT[j]);
  332. }
  333. /* adjust all labels to be bus physical */
  334. for(j = 0; j < PATCHES; j++) {
  335. script[LABELPATCHES[j]] = bS_to_host(pScript + SCRIPT[LABELPATCHES[j]]);
  336. }
  337. /* now patch up fixed addresses. */
  338. script_patch_32(script, MessageLocation,
  339. pScript + MSGOUT_OFFSET);
  340. script_patch_32(script, StatusAddress,
  341. pScript + STATUS_OFFSET);
  342. script_patch_32(script, ReceiveMsgAddress,
  343. pScript + MSGIN_OFFSET);
  344. hostdata->script = script;
  345. hostdata->pScript = pScript;
  346. dma_sync_single_for_device(hostdata->dev, pScript, sizeof(SCRIPT), DMA_TO_DEVICE);
  347. hostdata->state = NCR_700_HOST_FREE;
  348. hostdata->cmd = NULL;
  349. host->max_id = 7;
  350. host->max_lun = NCR_700_MAX_LUNS;
  351. BUG_ON(NCR_700_transport_template == NULL);
  352. host->transportt = NCR_700_transport_template;
  353. host->unique_id = (unsigned long)hostdata->base;
  354. hostdata->eh_complete = NULL;
  355. host->hostdata[0] = (unsigned long)hostdata;
  356. /* kick the chip */
  357. NCR_700_writeb(0xff, host, CTEST9_REG);
  358. if(hostdata->chip710)
  359. hostdata->rev = (NCR_700_readb(host, CTEST8_REG)>>4) & 0x0f;
  360. else
  361. hostdata->rev = (NCR_700_readb(host, CTEST7_REG)>>4) & 0x0f;
  362. hostdata->fast = (NCR_700_readb(host, CTEST9_REG) == 0);
  363. if(banner == 0) {
  364. printk(KERN_NOTICE "53c700: Version " NCR_700_VERSION " By James.Bottomley@HansenPartnership.com\n");
  365. banner = 1;
  366. }
  367. printk(KERN_NOTICE "scsi%d: %s rev %d %s\n", host->host_no,
  368. hostdata->chip710 ? "53c710" :
  369. (hostdata->fast ? "53c700-66" : "53c700"),
  370. hostdata->rev, hostdata->differential ?
  371. "(Differential)" : "");
  372. /* reset the chip */
  373. NCR_700_chip_reset(host);
  374. if (scsi_add_host(host, dev)) {
  375. dev_printk(KERN_ERR, dev, "53c700: scsi_add_host failed\n");
  376. scsi_host_put(host);
  377. return NULL;
  378. }
  379. spi_signalling(host) = hostdata->differential ? SPI_SIGNAL_HVD :
  380. SPI_SIGNAL_SE;
  381. return host;
  382. }
  383. int
  384. NCR_700_release(struct Scsi_Host *host)
  385. {
  386. struct NCR_700_Host_Parameters *hostdata =
  387. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  388. dma_free_noncoherent(hostdata->dev, TOTAL_MEM_SIZE,
  389. hostdata->script, hostdata->pScript);
  390. return 1;
  391. }
  392. static inline __u8
  393. NCR_700_identify(int can_disconnect, __u8 lun)
  394. {
  395. return IDENTIFY_BASE |
  396. ((can_disconnect) ? 0x40 : 0) |
  397. (lun & NCR_700_LUN_MASK);
  398. }
  399. /*
  400. * Function : static int data_residual (Scsi_Host *host)
  401. *
  402. * Purpose : return residual data count of what's in the chip. If you
  403. * really want to know what this function is doing, it's almost a
  404. * direct transcription of the algorithm described in the 53c710
  405. * guide, except that the DBC and DFIFO registers are only 6 bits
  406. * wide on a 53c700.
  407. *
  408. * Inputs : host - SCSI host */
  409. static inline int
  410. NCR_700_data_residual (struct Scsi_Host *host) {
  411. struct NCR_700_Host_Parameters *hostdata =
  412. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  413. int count, synchronous = 0;
  414. unsigned int ddir;
  415. if(hostdata->chip710) {
  416. count = ((NCR_700_readb(host, DFIFO_REG) & 0x7f) -
  417. (NCR_700_readl(host, DBC_REG) & 0x7f)) & 0x7f;
  418. } else {
  419. count = ((NCR_700_readb(host, DFIFO_REG) & 0x3f) -
  420. (NCR_700_readl(host, DBC_REG) & 0x3f)) & 0x3f;
  421. }
  422. if(hostdata->fast)
  423. synchronous = NCR_700_readb(host, SXFER_REG) & 0x0f;
  424. /* get the data direction */
  425. ddir = NCR_700_readb(host, CTEST0_REG) & 0x01;
  426. if (ddir) {
  427. /* Receive */
  428. if (synchronous)
  429. count += (NCR_700_readb(host, SSTAT2_REG) & 0xf0) >> 4;
  430. else
  431. if (NCR_700_readb(host, SSTAT1_REG) & SIDL_REG_FULL)
  432. ++count;
  433. } else {
  434. /* Send */
  435. __u8 sstat = NCR_700_readb(host, SSTAT1_REG);
  436. if (sstat & SODL_REG_FULL)
  437. ++count;
  438. if (synchronous && (sstat & SODR_REG_FULL))
  439. ++count;
  440. }
  441. #ifdef NCR_700_DEBUG
  442. if(count)
  443. printk("RESIDUAL IS %d (ddir %d)\n", count, ddir);
  444. #endif
  445. return count;
  446. }
  447. /* print out the SCSI wires and corresponding phase from the SBCL register
  448. * in the chip */
  449. static inline char *
  450. sbcl_to_string(__u8 sbcl)
  451. {
  452. int i;
  453. static char ret[256];
  454. ret[0]='\0';
  455. for(i=0; i<8; i++) {
  456. if((1<<i) & sbcl)
  457. strcat(ret, NCR_700_SBCL_bits[i]);
  458. }
  459. strcat(ret, NCR_700_SBCL_to_phase[sbcl & 0x07]);
  460. return ret;
  461. }
  462. static inline __u8
  463. bitmap_to_number(__u8 bitmap)
  464. {
  465. __u8 i;
  466. for(i=0; i<8 && !(bitmap &(1<<i)); i++)
  467. ;
  468. return i;
  469. }
  470. /* Pull a slot off the free list */
  471. STATIC struct NCR_700_command_slot *
  472. find_empty_slot(struct NCR_700_Host_Parameters *hostdata)
  473. {
  474. struct NCR_700_command_slot *slot = hostdata->free_list;
  475. if(slot == NULL) {
  476. /* sanity check */
  477. if(hostdata->command_slot_count != NCR_700_COMMAND_SLOTS_PER_HOST)
  478. printk(KERN_ERR "SLOTS FULL, but count is %d, should be %d\n", hostdata->command_slot_count, NCR_700_COMMAND_SLOTS_PER_HOST);
  479. return NULL;
  480. }
  481. if(slot->state != NCR_700_SLOT_FREE)
  482. /* should panic! */
  483. printk(KERN_ERR "BUSY SLOT ON FREE LIST!!!\n");
  484. hostdata->free_list = slot->ITL_forw;
  485. slot->ITL_forw = NULL;
  486. /* NOTE: set the state to busy here, not queued, since this
  487. * indicates the slot is in use and cannot be run by the IRQ
  488. * finish routine. If we cannot queue the command when it
  489. * is properly build, we then change to NCR_700_SLOT_QUEUED */
  490. slot->state = NCR_700_SLOT_BUSY;
  491. hostdata->command_slot_count++;
  492. return slot;
  493. }
  494. STATIC void
  495. free_slot(struct NCR_700_command_slot *slot,
  496. struct NCR_700_Host_Parameters *hostdata)
  497. {
  498. if((slot->state & NCR_700_SLOT_MASK) != NCR_700_SLOT_MAGIC) {
  499. printk(KERN_ERR "53c700: SLOT %p is not MAGIC!!!\n", slot);
  500. }
  501. if(slot->state == NCR_700_SLOT_FREE) {
  502. printk(KERN_ERR "53c700: SLOT %p is FREE!!!\n", slot);
  503. }
  504. slot->resume_offset = 0;
  505. slot->cmnd = NULL;
  506. slot->state = NCR_700_SLOT_FREE;
  507. slot->ITL_forw = hostdata->free_list;
  508. hostdata->free_list = slot;
  509. hostdata->command_slot_count--;
  510. }
  511. /* This routine really does very little. The command is indexed on
  512. the ITL and (if tagged) the ITLQ lists in _queuecommand */
  513. STATIC void
  514. save_for_reselection(struct NCR_700_Host_Parameters *hostdata,
  515. struct scsi_cmnd *SCp, __u32 dsp)
  516. {
  517. /* Its just possible that this gets executed twice */
  518. if(SCp != NULL) {
  519. struct NCR_700_command_slot *slot =
  520. (struct NCR_700_command_slot *)SCp->host_scribble;
  521. slot->resume_offset = dsp;
  522. }
  523. hostdata->state = NCR_700_HOST_FREE;
  524. hostdata->cmd = NULL;
  525. }
  526. STATIC inline void
  527. NCR_700_unmap(struct NCR_700_Host_Parameters *hostdata, struct scsi_cmnd *SCp,
  528. struct NCR_700_command_slot *slot)
  529. {
  530. if(SCp->sc_data_direction != DMA_NONE &&
  531. SCp->sc_data_direction != DMA_BIDIRECTIONAL) {
  532. if(SCp->use_sg) {
  533. dma_unmap_sg(hostdata->dev, SCp->buffer,
  534. SCp->use_sg, SCp->sc_data_direction);
  535. } else {
  536. dma_unmap_single(hostdata->dev, slot->dma_handle,
  537. SCp->request_bufflen,
  538. SCp->sc_data_direction);
  539. }
  540. }
  541. }
  542. STATIC inline void
  543. NCR_700_scsi_done(struct NCR_700_Host_Parameters *hostdata,
  544. struct scsi_cmnd *SCp, int result)
  545. {
  546. hostdata->state = NCR_700_HOST_FREE;
  547. hostdata->cmd = NULL;
  548. if(SCp != NULL) {
  549. struct NCR_700_command_slot *slot =
  550. (struct NCR_700_command_slot *)SCp->host_scribble;
  551. NCR_700_unmap(hostdata, SCp, slot);
  552. dma_unmap_single(hostdata->dev, slot->pCmd,
  553. sizeof(SCp->cmnd), DMA_TO_DEVICE);
  554. if(SCp->cmnd[0] == REQUEST_SENSE && SCp->cmnd[6] == NCR_700_INTERNAL_SENSE_MAGIC) {
  555. #ifdef NCR_700_DEBUG
  556. printk(" ORIGINAL CMD %p RETURNED %d, new return is %d sense is\n",
  557. SCp, SCp->cmnd[7], result);
  558. scsi_print_sense("53c700", SCp);
  559. #endif
  560. /* restore the old result if the request sense was
  561. * successful */
  562. if(result == 0)
  563. result = SCp->cmnd[7];
  564. /* now restore the original command */
  565. memcpy((void *) SCp->cmnd, (void *) SCp->data_cmnd,
  566. sizeof(SCp->data_cmnd));
  567. SCp->request_buffer = SCp->buffer;
  568. SCp->request_bufflen = SCp->bufflen;
  569. SCp->use_sg = SCp->old_use_sg;
  570. SCp->cmd_len = SCp->old_cmd_len;
  571. SCp->sc_data_direction = SCp->sc_old_data_direction;
  572. SCp->underflow = SCp->old_underflow;
  573. }
  574. free_slot(slot, hostdata);
  575. #ifdef NCR_700_DEBUG
  576. if(NCR_700_get_depth(SCp->device) == 0 ||
  577. NCR_700_get_depth(SCp->device) > SCp->device->queue_depth)
  578. printk(KERN_ERR "Invalid depth in NCR_700_scsi_done(): %d\n",
  579. NCR_700_get_depth(SCp->device));
  580. #endif /* NCR_700_DEBUG */
  581. NCR_700_set_depth(SCp->device, NCR_700_get_depth(SCp->device) - 1);
  582. SCp->host_scribble = NULL;
  583. SCp->result = result;
  584. SCp->scsi_done(SCp);
  585. } else {
  586. printk(KERN_ERR "53c700: SCSI DONE HAS NULL SCp\n");
  587. }
  588. }
  589. STATIC void
  590. NCR_700_internal_bus_reset(struct Scsi_Host *host)
  591. {
  592. /* Bus reset */
  593. NCR_700_writeb(ASSERT_RST, host, SCNTL1_REG);
  594. udelay(50);
  595. NCR_700_writeb(0, host, SCNTL1_REG);
  596. }
  597. STATIC void
  598. NCR_700_chip_setup(struct Scsi_Host *host)
  599. {
  600. struct NCR_700_Host_Parameters *hostdata =
  601. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  602. __u32 dcntl_extra = 0;
  603. __u8 min_period;
  604. __u8 min_xferp = (hostdata->chip710 ? NCR_710_MIN_XFERP : NCR_700_MIN_XFERP);
  605. if(hostdata->chip710) {
  606. __u8 burst_disable = hostdata->burst_disable
  607. ? BURST_DISABLE : 0;
  608. dcntl_extra = COMPAT_700_MODE;
  609. NCR_700_writeb(dcntl_extra, host, DCNTL_REG);
  610. NCR_700_writeb(BURST_LENGTH_8 | hostdata->dmode_extra,
  611. host, DMODE_710_REG);
  612. NCR_700_writeb(burst_disable | (hostdata->differential ?
  613. DIFF : 0), host, CTEST7_REG);
  614. NCR_700_writeb(BTB_TIMER_DISABLE, host, CTEST0_REG);
  615. NCR_700_writeb(FULL_ARBITRATION | ENABLE_PARITY | PARITY
  616. | AUTO_ATN, host, SCNTL0_REG);
  617. } else {
  618. NCR_700_writeb(BURST_LENGTH_8 | hostdata->dmode_extra,
  619. host, DMODE_700_REG);
  620. NCR_700_writeb(hostdata->differential ?
  621. DIFF : 0, host, CTEST7_REG);
  622. if(hostdata->fast) {
  623. /* this is for 700-66, does nothing on 700 */
  624. NCR_700_writeb(LAST_DIS_ENBL | ENABLE_ACTIVE_NEGATION
  625. | GENERATE_RECEIVE_PARITY, host,
  626. CTEST8_REG);
  627. } else {
  628. NCR_700_writeb(FULL_ARBITRATION | ENABLE_PARITY
  629. | PARITY | AUTO_ATN, host, SCNTL0_REG);
  630. }
  631. }
  632. NCR_700_writeb(1 << host->this_id, host, SCID_REG);
  633. NCR_700_writeb(0, host, SBCL_REG);
  634. NCR_700_writeb(ASYNC_OPERATION, host, SXFER_REG);
  635. NCR_700_writeb(PHASE_MM_INT | SEL_TIMEOUT_INT | GROSS_ERR_INT | UX_DISC_INT
  636. | RST_INT | PAR_ERR_INT | SELECT_INT, host, SIEN_REG);
  637. NCR_700_writeb(ABORT_INT | INT_INST_INT | ILGL_INST_INT, host, DIEN_REG);
  638. NCR_700_writeb(ENABLE_SELECT, host, SCNTL1_REG);
  639. if(hostdata->clock > 75) {
  640. printk(KERN_ERR "53c700: Clock speed %dMHz is too high: 75Mhz is the maximum this chip can be driven at\n", hostdata->clock);
  641. /* do the best we can, but the async clock will be out
  642. * of spec: sync divider 2, async divider 3 */
  643. DEBUG(("53c700: sync 2 async 3\n"));
  644. NCR_700_writeb(SYNC_DIV_2_0, host, SBCL_REG);
  645. NCR_700_writeb(ASYNC_DIV_3_0 | dcntl_extra, host, DCNTL_REG);
  646. hostdata->sync_clock = hostdata->clock/2;
  647. } else if(hostdata->clock > 50 && hostdata->clock <= 75) {
  648. /* sync divider 1.5, async divider 3 */
  649. DEBUG(("53c700: sync 1.5 async 3\n"));
  650. NCR_700_writeb(SYNC_DIV_1_5, host, SBCL_REG);
  651. NCR_700_writeb(ASYNC_DIV_3_0 | dcntl_extra, host, DCNTL_REG);
  652. hostdata->sync_clock = hostdata->clock*2;
  653. hostdata->sync_clock /= 3;
  654. } else if(hostdata->clock > 37 && hostdata->clock <= 50) {
  655. /* sync divider 1, async divider 2 */
  656. DEBUG(("53c700: sync 1 async 2\n"));
  657. NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
  658. NCR_700_writeb(ASYNC_DIV_2_0 | dcntl_extra, host, DCNTL_REG);
  659. hostdata->sync_clock = hostdata->clock;
  660. } else if(hostdata->clock > 25 && hostdata->clock <=37) {
  661. /* sync divider 1, async divider 1.5 */
  662. DEBUG(("53c700: sync 1 async 1.5\n"));
  663. NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
  664. NCR_700_writeb(ASYNC_DIV_1_5 | dcntl_extra, host, DCNTL_REG);
  665. hostdata->sync_clock = hostdata->clock;
  666. } else {
  667. DEBUG(("53c700: sync 1 async 1\n"));
  668. NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
  669. NCR_700_writeb(ASYNC_DIV_1_0 | dcntl_extra, host, DCNTL_REG);
  670. /* sync divider 1, async divider 1 */
  671. hostdata->sync_clock = hostdata->clock;
  672. }
  673. /* Calculate the actual minimum period that can be supported
  674. * by our synchronous clock speed. See the 710 manual for
  675. * exact details of this calculation which is based on a
  676. * setting of the SXFER register */
  677. min_period = 1000*(4+min_xferp)/(4*hostdata->sync_clock);
  678. hostdata->min_period = NCR_700_MIN_PERIOD;
  679. if(min_period > NCR_700_MIN_PERIOD)
  680. hostdata->min_period = min_period;
  681. }
  682. STATIC void
  683. NCR_700_chip_reset(struct Scsi_Host *host)
  684. {
  685. struct NCR_700_Host_Parameters *hostdata =
  686. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  687. if(hostdata->chip710) {
  688. NCR_700_writeb(SOFTWARE_RESET_710, host, ISTAT_REG);
  689. udelay(100);
  690. NCR_700_writeb(0, host, ISTAT_REG);
  691. } else {
  692. NCR_700_writeb(SOFTWARE_RESET, host, DCNTL_REG);
  693. udelay(100);
  694. NCR_700_writeb(0, host, DCNTL_REG);
  695. }
  696. mdelay(1000);
  697. NCR_700_chip_setup(host);
  698. }
  699. /* The heart of the message processing engine is that the instruction
  700. * immediately after the INT is the normal case (and so must be CLEAR
  701. * ACK). If we want to do something else, we call that routine in
  702. * scripts and set temp to be the normal case + 8 (skipping the CLEAR
  703. * ACK) so that the routine returns correctly to resume its activity
  704. * */
  705. STATIC __u32
  706. process_extended_message(struct Scsi_Host *host,
  707. struct NCR_700_Host_Parameters *hostdata,
  708. struct scsi_cmnd *SCp, __u32 dsp, __u32 dsps)
  709. {
  710. __u32 resume_offset = dsp, temp = dsp + 8;
  711. __u8 pun = 0xff, lun = 0xff;
  712. if(SCp != NULL) {
  713. pun = SCp->device->id;
  714. lun = SCp->device->lun;
  715. }
  716. switch(hostdata->msgin[2]) {
  717. case A_SDTR_MSG:
  718. if(SCp != NULL && NCR_700_is_flag_set(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION)) {
  719. struct scsi_target *starget = SCp->device->sdev_target;
  720. __u8 period = hostdata->msgin[3];
  721. __u8 offset = hostdata->msgin[4];
  722. if(offset == 0 || period == 0) {
  723. offset = 0;
  724. period = 0;
  725. }
  726. spi_offset(starget) = offset;
  727. spi_period(starget) = period;
  728. if(NCR_700_is_flag_set(SCp->device, NCR_700_DEV_PRINT_SYNC_NEGOTIATION)) {
  729. spi_display_xfer_agreement(starget);
  730. NCR_700_clear_flag(SCp->device, NCR_700_DEV_PRINT_SYNC_NEGOTIATION);
  731. }
  732. NCR_700_set_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
  733. NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  734. NCR_700_writeb(NCR_700_get_SXFER(SCp->device),
  735. host, SXFER_REG);
  736. } else {
  737. /* SDTR message out of the blue, reject it */
  738. printk(KERN_WARNING "scsi%d Unexpected SDTR msg\n",
  739. host->host_no);
  740. hostdata->msgout[0] = A_REJECT_MSG;
  741. dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
  742. script_patch_16(hostdata->script, MessageCount, 1);
  743. /* SendMsgOut returns, so set up the return
  744. * address */
  745. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  746. }
  747. break;
  748. case A_WDTR_MSG:
  749. printk(KERN_INFO "scsi%d: (%d:%d), Unsolicited WDTR after CMD, Rejecting\n",
  750. host->host_no, pun, lun);
  751. hostdata->msgout[0] = A_REJECT_MSG;
  752. dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
  753. script_patch_16(hostdata->script, MessageCount, 1);
  754. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  755. break;
  756. default:
  757. printk(KERN_INFO "scsi%d (%d:%d): Unexpected message %s: ",
  758. host->host_no, pun, lun,
  759. NCR_700_phase[(dsps & 0xf00) >> 8]);
  760. scsi_print_msg(hostdata->msgin);
  761. printk("\n");
  762. /* just reject it */
  763. hostdata->msgout[0] = A_REJECT_MSG;
  764. dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
  765. script_patch_16(hostdata->script, MessageCount, 1);
  766. /* SendMsgOut returns, so set up the return
  767. * address */
  768. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  769. }
  770. NCR_700_writel(temp, host, TEMP_REG);
  771. return resume_offset;
  772. }
  773. STATIC __u32
  774. process_message(struct Scsi_Host *host, struct NCR_700_Host_Parameters *hostdata,
  775. struct scsi_cmnd *SCp, __u32 dsp, __u32 dsps)
  776. {
  777. /* work out where to return to */
  778. __u32 temp = dsp + 8, resume_offset = dsp;
  779. __u8 pun = 0xff, lun = 0xff;
  780. if(SCp != NULL) {
  781. pun = SCp->device->id;
  782. lun = SCp->device->lun;
  783. }
  784. #ifdef NCR_700_DEBUG
  785. printk("scsi%d (%d:%d): message %s: ", host->host_no, pun, lun,
  786. NCR_700_phase[(dsps & 0xf00) >> 8]);
  787. scsi_print_msg(hostdata->msgin);
  788. printk("\n");
  789. #endif
  790. switch(hostdata->msgin[0]) {
  791. case A_EXTENDED_MSG:
  792. resume_offset = process_extended_message(host, hostdata, SCp,
  793. dsp, dsps);
  794. break;
  795. case A_REJECT_MSG:
  796. if(SCp != NULL && NCR_700_is_flag_set(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION)) {
  797. /* Rejected our sync negotiation attempt */
  798. spi_period(SCp->device->sdev_target) =
  799. spi_offset(SCp->device->sdev_target) = 0;
  800. NCR_700_set_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
  801. NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  802. } else if(SCp != NULL && NCR_700_get_tag_neg_state(SCp->device) == NCR_700_DURING_TAG_NEGOTIATION) {
  803. /* rejected our first simple tag message */
  804. printk(KERN_WARNING "scsi%d (%d:%d) Rejected first tag queue attempt, turning off tag queueing\n", host->host_no, pun, lun);
  805. /* we're done negotiating */
  806. NCR_700_set_tag_neg_state(SCp->device, NCR_700_FINISHED_TAG_NEGOTIATION);
  807. hostdata->tag_negotiated &= ~(1<<SCp->device->id);
  808. SCp->device->tagged_supported = 0;
  809. scsi_deactivate_tcq(SCp->device, host->cmd_per_lun);
  810. } else {
  811. printk(KERN_WARNING "scsi%d (%d:%d) Unexpected REJECT Message %s\n",
  812. host->host_no, pun, lun,
  813. NCR_700_phase[(dsps & 0xf00) >> 8]);
  814. /* however, just ignore it */
  815. }
  816. break;
  817. case A_PARITY_ERROR_MSG:
  818. printk(KERN_ERR "scsi%d (%d:%d) Parity Error!\n", host->host_no,
  819. pun, lun);
  820. NCR_700_internal_bus_reset(host);
  821. break;
  822. case A_SIMPLE_TAG_MSG:
  823. printk(KERN_INFO "scsi%d (%d:%d) SIMPLE TAG %d %s\n", host->host_no,
  824. pun, lun, hostdata->msgin[1],
  825. NCR_700_phase[(dsps & 0xf00) >> 8]);
  826. /* just ignore it */
  827. break;
  828. default:
  829. printk(KERN_INFO "scsi%d (%d:%d): Unexpected message %s: ",
  830. host->host_no, pun, lun,
  831. NCR_700_phase[(dsps & 0xf00) >> 8]);
  832. scsi_print_msg(hostdata->msgin);
  833. printk("\n");
  834. /* just reject it */
  835. hostdata->msgout[0] = A_REJECT_MSG;
  836. dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
  837. script_patch_16(hostdata->script, MessageCount, 1);
  838. /* SendMsgOut returns, so set up the return
  839. * address */
  840. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  841. break;
  842. }
  843. NCR_700_writel(temp, host, TEMP_REG);
  844. /* set us up to receive another message */
  845. dma_cache_sync(hostdata->msgin, MSG_ARRAY_SIZE, DMA_FROM_DEVICE);
  846. return resume_offset;
  847. }
  848. STATIC __u32
  849. process_script_interrupt(__u32 dsps, __u32 dsp, struct scsi_cmnd *SCp,
  850. struct Scsi_Host *host,
  851. struct NCR_700_Host_Parameters *hostdata)
  852. {
  853. __u32 resume_offset = 0;
  854. __u8 pun = 0xff, lun=0xff;
  855. if(SCp != NULL) {
  856. pun = SCp->device->id;
  857. lun = SCp->device->lun;
  858. }
  859. if(dsps == A_GOOD_STATUS_AFTER_STATUS) {
  860. DEBUG((" COMMAND COMPLETE, status=%02x\n",
  861. hostdata->status[0]));
  862. /* OK, if TCQ still under negotiation, we now know it works */
  863. if (NCR_700_get_tag_neg_state(SCp->device) == NCR_700_DURING_TAG_NEGOTIATION)
  864. NCR_700_set_tag_neg_state(SCp->device,
  865. NCR_700_FINISHED_TAG_NEGOTIATION);
  866. /* check for contingent allegiance contitions */
  867. if(status_byte(hostdata->status[0]) == CHECK_CONDITION ||
  868. status_byte(hostdata->status[0]) == COMMAND_TERMINATED) {
  869. struct NCR_700_command_slot *slot =
  870. (struct NCR_700_command_slot *)SCp->host_scribble;
  871. if(SCp->cmnd[0] == REQUEST_SENSE) {
  872. /* OOPS: bad device, returning another
  873. * contingent allegiance condition */
  874. printk(KERN_ERR "scsi%d (%d:%d) broken device is looping in contingent allegiance: ignoring\n", host->host_no, pun, lun);
  875. NCR_700_scsi_done(hostdata, SCp, hostdata->status[0]);
  876. } else {
  877. #ifdef NCR_DEBUG
  878. scsi_print_command(SCp);
  879. printk(" cmd %p has status %d, requesting sense\n",
  880. SCp, hostdata->status[0]);
  881. #endif
  882. /* we can destroy the command here
  883. * because the contingent allegiance
  884. * condition will cause a retry which
  885. * will re-copy the command from the
  886. * saved data_cmnd. We also unmap any
  887. * data associated with the command
  888. * here */
  889. NCR_700_unmap(hostdata, SCp, slot);
  890. SCp->cmnd[0] = REQUEST_SENSE;
  891. SCp->cmnd[1] = (SCp->device->lun & 0x7) << 5;
  892. SCp->cmnd[2] = 0;
  893. SCp->cmnd[3] = 0;
  894. SCp->cmnd[4] = sizeof(SCp->sense_buffer);
  895. SCp->cmnd[5] = 0;
  896. SCp->cmd_len = 6;
  897. /* Here's a quiet hack: the
  898. * REQUEST_SENSE command is six bytes,
  899. * so store a flag indicating that
  900. * this was an internal sense request
  901. * and the original status at the end
  902. * of the command */
  903. SCp->cmnd[6] = NCR_700_INTERNAL_SENSE_MAGIC;
  904. SCp->cmnd[7] = hostdata->status[0];
  905. SCp->use_sg = 0;
  906. SCp->sc_data_direction = DMA_FROM_DEVICE;
  907. dma_sync_single_for_device(hostdata->dev, slot->pCmd,
  908. SCp->cmd_len, DMA_TO_DEVICE);
  909. SCp->request_bufflen = sizeof(SCp->sense_buffer);
  910. slot->dma_handle = dma_map_single(hostdata->dev, SCp->sense_buffer, sizeof(SCp->sense_buffer), DMA_FROM_DEVICE);
  911. slot->SG[0].ins = bS_to_host(SCRIPT_MOVE_DATA_IN | sizeof(SCp->sense_buffer));
  912. slot->SG[0].pAddr = bS_to_host(slot->dma_handle);
  913. slot->SG[1].ins = bS_to_host(SCRIPT_RETURN);
  914. slot->SG[1].pAddr = 0;
  915. slot->resume_offset = hostdata->pScript;
  916. dma_cache_sync(slot->SG, sizeof(slot->SG[0])*2, DMA_TO_DEVICE);
  917. dma_cache_sync(SCp->sense_buffer, sizeof(SCp->sense_buffer), DMA_FROM_DEVICE);
  918. /* queue the command for reissue */
  919. slot->state = NCR_700_SLOT_QUEUED;
  920. hostdata->state = NCR_700_HOST_FREE;
  921. hostdata->cmd = NULL;
  922. }
  923. } else {
  924. // Currently rely on the mid layer evaluation
  925. // of the tag queuing capability
  926. //
  927. //if(status_byte(hostdata->status[0]) == GOOD &&
  928. // SCp->cmnd[0] == INQUIRY && SCp->use_sg == 0) {
  929. // /* Piggy back the tag queueing support
  930. // * on this command */
  931. // dma_sync_single_for_cpu(hostdata->dev,
  932. // slot->dma_handle,
  933. // SCp->request_bufflen,
  934. // DMA_FROM_DEVICE);
  935. // if(((char *)SCp->request_buffer)[7] & 0x02) {
  936. // printk(KERN_INFO "scsi%d: (%d:%d) Enabling Tag Command Queuing\n", host->host_no, pun, lun);
  937. // hostdata->tag_negotiated |= (1<<SCp->device->id);
  938. // NCR_700_set_flag(SCp->device, NCR_700_DEV_BEGIN_TAG_QUEUEING);
  939. // } else {
  940. // NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_TAG_QUEUEING);
  941. // hostdata->tag_negotiated &= ~(1<<SCp->device->id);
  942. // }
  943. //}
  944. NCR_700_scsi_done(hostdata, SCp, hostdata->status[0]);
  945. }
  946. } else if((dsps & 0xfffff0f0) == A_UNEXPECTED_PHASE) {
  947. __u8 i = (dsps & 0xf00) >> 8;
  948. printk(KERN_ERR "scsi%d: (%d:%d), UNEXPECTED PHASE %s (%s)\n",
  949. host->host_no, pun, lun,
  950. NCR_700_phase[i],
  951. sbcl_to_string(NCR_700_readb(host, SBCL_REG)));
  952. printk(KERN_ERR " len = %d, cmd =", SCp->cmd_len);
  953. scsi_print_command(SCp);
  954. NCR_700_internal_bus_reset(host);
  955. } else if((dsps & 0xfffff000) == A_FATAL) {
  956. int i = (dsps & 0xfff);
  957. printk(KERN_ERR "scsi%d: (%d:%d) FATAL ERROR: %s\n",
  958. host->host_no, pun, lun, NCR_700_fatal_messages[i]);
  959. if(dsps == A_FATAL_ILLEGAL_MSG_LENGTH) {
  960. printk(KERN_ERR " msg begins %02x %02x\n",
  961. hostdata->msgin[0], hostdata->msgin[1]);
  962. }
  963. NCR_700_internal_bus_reset(host);
  964. } else if((dsps & 0xfffff0f0) == A_DISCONNECT) {
  965. #ifdef NCR_700_DEBUG
  966. __u8 i = (dsps & 0xf00) >> 8;
  967. printk("scsi%d: (%d:%d), DISCONNECTED (%d) %s\n",
  968. host->host_no, pun, lun,
  969. i, NCR_700_phase[i]);
  970. #endif
  971. save_for_reselection(hostdata, SCp, dsp);
  972. } else if(dsps == A_RESELECTION_IDENTIFIED) {
  973. __u8 lun;
  974. struct NCR_700_command_slot *slot;
  975. __u8 reselection_id = hostdata->reselection_id;
  976. struct scsi_device *SDp;
  977. lun = hostdata->msgin[0] & 0x1f;
  978. hostdata->reselection_id = 0xff;
  979. DEBUG(("scsi%d: (%d:%d) RESELECTED!\n",
  980. host->host_no, reselection_id, lun));
  981. /* clear the reselection indicator */
  982. SDp = __scsi_device_lookup(host, 0, reselection_id, lun);
  983. if(unlikely(SDp == NULL)) {
  984. printk(KERN_ERR "scsi%d: (%d:%d) HAS NO device\n",
  985. host->host_no, reselection_id, lun);
  986. BUG();
  987. }
  988. if(hostdata->msgin[1] == A_SIMPLE_TAG_MSG) {
  989. struct scsi_cmnd *SCp = scsi_find_tag(SDp, hostdata->msgin[2]);
  990. if(unlikely(SCp == NULL)) {
  991. printk(KERN_ERR "scsi%d: (%d:%d) no saved request for tag %d\n",
  992. host->host_no, reselection_id, lun, hostdata->msgin[2]);
  993. BUG();
  994. }
  995. slot = (struct NCR_700_command_slot *)SCp->host_scribble;
  996. DEBUG(("53c700: %d:%d:%d, reselection is tag %d, slot %p(%d)\n",
  997. host->host_no, SDp->id, SDp->lun,
  998. hostdata->msgin[2], slot, slot->tag));
  999. } else {
  1000. struct scsi_cmnd *SCp = scsi_find_tag(SDp, SCSI_NO_TAG);
  1001. if(unlikely(SCp == NULL)) {
  1002. printk(KERN_ERR "scsi%d: (%d:%d) no saved request for untagged cmd\n",
  1003. host->host_no, reselection_id, lun);
  1004. BUG();
  1005. }
  1006. slot = (struct NCR_700_command_slot *)SCp->host_scribble;
  1007. }
  1008. if(slot == NULL) {
  1009. printk(KERN_ERR "scsi%d: (%d:%d) RESELECTED but no saved command (MSG = %02x %02x %02x)!!\n",
  1010. host->host_no, reselection_id, lun,
  1011. hostdata->msgin[0], hostdata->msgin[1],
  1012. hostdata->msgin[2]);
  1013. } else {
  1014. if(hostdata->state != NCR_700_HOST_BUSY)
  1015. printk(KERN_ERR "scsi%d: FATAL, host not busy during valid reselection!\n",
  1016. host->host_no);
  1017. resume_offset = slot->resume_offset;
  1018. hostdata->cmd = slot->cmnd;
  1019. /* re-patch for this command */
  1020. script_patch_32_abs(hostdata->script, CommandAddress,
  1021. slot->pCmd);
  1022. script_patch_16(hostdata->script,
  1023. CommandCount, slot->cmnd->cmd_len);
  1024. script_patch_32_abs(hostdata->script, SGScriptStartAddress,
  1025. to32bit(&slot->pSG[0].ins));
  1026. /* Note: setting SXFER only works if we're
  1027. * still in the MESSAGE phase, so it is vital
  1028. * that ACK is still asserted when we process
  1029. * the reselection message. The resume offset
  1030. * should therefore always clear ACK */
  1031. NCR_700_writeb(NCR_700_get_SXFER(hostdata->cmd->device),
  1032. host, SXFER_REG);
  1033. dma_cache_sync(hostdata->msgin,
  1034. MSG_ARRAY_SIZE, DMA_FROM_DEVICE);
  1035. dma_cache_sync(hostdata->msgout,
  1036. MSG_ARRAY_SIZE, DMA_TO_DEVICE);
  1037. /* I'm just being paranoid here, the command should
  1038. * already have been flushed from the cache */
  1039. dma_cache_sync(slot->cmnd->cmnd,
  1040. slot->cmnd->cmd_len, DMA_TO_DEVICE);
  1041. }
  1042. } else if(dsps == A_RESELECTED_DURING_SELECTION) {
  1043. /* This section is full of debugging code because I've
  1044. * never managed to reach it. I think what happens is
  1045. * that, because the 700 runs with selection
  1046. * interrupts enabled the whole time that we take a
  1047. * selection interrupt before we manage to get to the
  1048. * reselected script interrupt */
  1049. __u8 reselection_id = NCR_700_readb(host, SFBR_REG);
  1050. struct NCR_700_command_slot *slot;
  1051. /* Take out our own ID */
  1052. reselection_id &= ~(1<<host->this_id);
  1053. /* I've never seen this happen, so keep this as a printk rather
  1054. * than a debug */
  1055. printk(KERN_INFO "scsi%d: (%d:%d) RESELECTION DURING SELECTION, dsp=%08x[%04x] state=%d, count=%d\n",
  1056. host->host_no, reselection_id, lun, dsp, dsp - hostdata->pScript, hostdata->state, hostdata->command_slot_count);
  1057. {
  1058. /* FIXME: DEBUGGING CODE */
  1059. __u32 SG = (__u32)bS_to_cpu(hostdata->script[A_SGScriptStartAddress_used[0]]);
  1060. int i;
  1061. for(i=0; i< NCR_700_COMMAND_SLOTS_PER_HOST; i++) {
  1062. if(SG >= to32bit(&hostdata->slots[i].pSG[0])
  1063. && SG <= to32bit(&hostdata->slots[i].pSG[NCR_700_SG_SEGMENTS]))
  1064. break;
  1065. }
  1066. printk(KERN_INFO "IDENTIFIED SG segment as being %08x in slot %p, cmd %p, slot->resume_offset=%08x\n", SG, &hostdata->slots[i], hostdata->slots[i].cmnd, hostdata->slots[i].resume_offset);
  1067. SCp = hostdata->slots[i].cmnd;
  1068. }
  1069. if(SCp != NULL) {
  1070. slot = (struct NCR_700_command_slot *)SCp->host_scribble;
  1071. /* change slot from busy to queued to redo command */
  1072. slot->state = NCR_700_SLOT_QUEUED;
  1073. }
  1074. hostdata->cmd = NULL;
  1075. if(reselection_id == 0) {
  1076. if(hostdata->reselection_id == 0xff) {
  1077. printk(KERN_ERR "scsi%d: Invalid reselection during selection!!\n", host->host_no);
  1078. return 0;
  1079. } else {
  1080. printk(KERN_ERR "scsi%d: script reselected and we took a selection interrupt\n",
  1081. host->host_no);
  1082. reselection_id = hostdata->reselection_id;
  1083. }
  1084. } else {
  1085. /* convert to real ID */
  1086. reselection_id = bitmap_to_number(reselection_id);
  1087. }
  1088. hostdata->reselection_id = reselection_id;
  1089. /* just in case we have a stale simple tag message, clear it */
  1090. hostdata->msgin[1] = 0;
  1091. dma_cache_sync(hostdata->msgin,
  1092. MSG_ARRAY_SIZE, DMA_BIDIRECTIONAL);
  1093. if(hostdata->tag_negotiated & (1<<reselection_id)) {
  1094. resume_offset = hostdata->pScript + Ent_GetReselectionWithTag;
  1095. } else {
  1096. resume_offset = hostdata->pScript + Ent_GetReselectionData;
  1097. }
  1098. } else if(dsps == A_COMPLETED_SELECTION_AS_TARGET) {
  1099. /* we've just disconnected from the bus, do nothing since
  1100. * a return here will re-run the queued command slot
  1101. * that may have been interrupted by the initial selection */
  1102. DEBUG((" SELECTION COMPLETED\n"));
  1103. } else if((dsps & 0xfffff0f0) == A_MSG_IN) {
  1104. resume_offset = process_message(host, hostdata, SCp,
  1105. dsp, dsps);
  1106. } else if((dsps & 0xfffff000) == 0) {
  1107. __u8 i = (dsps & 0xf0) >> 4, j = (dsps & 0xf00) >> 8;
  1108. printk(KERN_ERR "scsi%d: (%d:%d), unhandled script condition %s %s at %04x\n",
  1109. host->host_no, pun, lun, NCR_700_condition[i],
  1110. NCR_700_phase[j], dsp - hostdata->pScript);
  1111. if(SCp != NULL) {
  1112. scsi_print_command(SCp);
  1113. if(SCp->use_sg) {
  1114. for(i = 0; i < SCp->use_sg + 1; i++) {
  1115. printk(KERN_INFO " SG[%d].length = %d, move_insn=%08x, addr %08x\n", i, ((struct scatterlist *)SCp->buffer)[i].length, ((struct NCR_700_command_slot *)SCp->host_scribble)->SG[i].ins, ((struct NCR_700_command_slot *)SCp->host_scribble)->SG[i].pAddr);
  1116. }
  1117. }
  1118. }
  1119. NCR_700_internal_bus_reset(host);
  1120. } else if((dsps & 0xfffff000) == A_DEBUG_INTERRUPT) {
  1121. printk(KERN_NOTICE "scsi%d (%d:%d) DEBUG INTERRUPT %d AT %08x[%04x], continuing\n",
  1122. host->host_no, pun, lun, dsps & 0xfff, dsp, dsp - hostdata->pScript);
  1123. resume_offset = dsp;
  1124. } else {
  1125. printk(KERN_ERR "scsi%d: (%d:%d), unidentified script interrupt 0x%x at %04x\n",
  1126. host->host_no, pun, lun, dsps, dsp - hostdata->pScript);
  1127. NCR_700_internal_bus_reset(host);
  1128. }
  1129. return resume_offset;
  1130. }
  1131. /* We run the 53c700 with selection interrupts always enabled. This
  1132. * means that the chip may be selected as soon as the bus frees. On a
  1133. * busy bus, this can be before the scripts engine finishes its
  1134. * processing. Therefore, part of the selection processing has to be
  1135. * to find out what the scripts engine is doing and complete the
  1136. * function if necessary (i.e. process the pending disconnect or save
  1137. * the interrupted initial selection */
  1138. STATIC inline __u32
  1139. process_selection(struct Scsi_Host *host, __u32 dsp)
  1140. {
  1141. __u8 id = 0; /* Squash compiler warning */
  1142. int count = 0;
  1143. __u32 resume_offset = 0;
  1144. struct NCR_700_Host_Parameters *hostdata =
  1145. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  1146. struct scsi_cmnd *SCp = hostdata->cmd;
  1147. __u8 sbcl;
  1148. for(count = 0; count < 5; count++) {
  1149. id = NCR_700_readb(host, hostdata->chip710 ?
  1150. CTEST9_REG : SFBR_REG);
  1151. /* Take out our own ID */
  1152. id &= ~(1<<host->this_id);
  1153. if(id != 0)
  1154. break;
  1155. udelay(5);
  1156. }
  1157. sbcl = NCR_700_readb(host, SBCL_REG);
  1158. if((sbcl & SBCL_IO) == 0) {
  1159. /* mark as having been selected rather than reselected */
  1160. id = 0xff;
  1161. } else {
  1162. /* convert to real ID */
  1163. hostdata->reselection_id = id = bitmap_to_number(id);
  1164. DEBUG(("scsi%d: Reselected by %d\n",
  1165. host->host_no, id));
  1166. }
  1167. if(hostdata->state == NCR_700_HOST_BUSY && SCp != NULL) {
  1168. struct NCR_700_command_slot *slot =
  1169. (struct NCR_700_command_slot *)SCp->host_scribble;
  1170. DEBUG((" ID %d WARNING: RESELECTION OF BUSY HOST, saving cmd %p, slot %p, addr %x [%04x], resume %x!\n", id, hostdata->cmd, slot, dsp, dsp - hostdata->pScript, resume_offset));
  1171. switch(dsp - hostdata->pScript) {
  1172. case Ent_Disconnect1:
  1173. case Ent_Disconnect2:
  1174. save_for_reselection(hostdata, SCp, Ent_Disconnect2 + hostdata->pScript);
  1175. break;
  1176. case Ent_Disconnect3:
  1177. case Ent_Disconnect4:
  1178. save_for_reselection(hostdata, SCp, Ent_Disconnect4 + hostdata->pScript);
  1179. break;
  1180. case Ent_Disconnect5:
  1181. case Ent_Disconnect6:
  1182. save_for_reselection(hostdata, SCp, Ent_Disconnect6 + hostdata->pScript);
  1183. break;
  1184. case Ent_Disconnect7:
  1185. case Ent_Disconnect8:
  1186. save_for_reselection(hostdata, SCp, Ent_Disconnect8 + hostdata->pScript);
  1187. break;
  1188. case Ent_Finish1:
  1189. case Ent_Finish2:
  1190. process_script_interrupt(A_GOOD_STATUS_AFTER_STATUS, dsp, SCp, host, hostdata);
  1191. break;
  1192. default:
  1193. slot->state = NCR_700_SLOT_QUEUED;
  1194. break;
  1195. }
  1196. }
  1197. hostdata->state = NCR_700_HOST_BUSY;
  1198. hostdata->cmd = NULL;
  1199. /* clear any stale simple tag message */
  1200. hostdata->msgin[1] = 0;
  1201. dma_cache_sync(hostdata->msgin, MSG_ARRAY_SIZE,
  1202. DMA_BIDIRECTIONAL);
  1203. if(id == 0xff) {
  1204. /* Selected as target, Ignore */
  1205. resume_offset = hostdata->pScript + Ent_SelectedAsTarget;
  1206. } else if(hostdata->tag_negotiated & (1<<id)) {
  1207. resume_offset = hostdata->pScript + Ent_GetReselectionWithTag;
  1208. } else {
  1209. resume_offset = hostdata->pScript + Ent_GetReselectionData;
  1210. }
  1211. return resume_offset;
  1212. }
  1213. static inline void
  1214. NCR_700_clear_fifo(struct Scsi_Host *host) {
  1215. const struct NCR_700_Host_Parameters *hostdata
  1216. = (struct NCR_700_Host_Parameters *)host->hostdata[0];
  1217. if(hostdata->chip710) {
  1218. NCR_700_writeb(CLR_FIFO_710, host, CTEST8_REG);
  1219. } else {
  1220. NCR_700_writeb(CLR_FIFO, host, DFIFO_REG);
  1221. }
  1222. }
  1223. static inline void
  1224. NCR_700_flush_fifo(struct Scsi_Host *host) {
  1225. const struct NCR_700_Host_Parameters *hostdata
  1226. = (struct NCR_700_Host_Parameters *)host->hostdata[0];
  1227. if(hostdata->chip710) {
  1228. NCR_700_writeb(FLUSH_DMA_FIFO_710, host, CTEST8_REG);
  1229. udelay(10);
  1230. NCR_700_writeb(0, host, CTEST8_REG);
  1231. } else {
  1232. NCR_700_writeb(FLUSH_DMA_FIFO, host, DFIFO_REG);
  1233. udelay(10);
  1234. NCR_700_writeb(0, host, DFIFO_REG);
  1235. }
  1236. }
  1237. /* The queue lock with interrupts disabled must be held on entry to
  1238. * this function */
  1239. STATIC int
  1240. NCR_700_start_command(struct scsi_cmnd *SCp)
  1241. {
  1242. struct NCR_700_command_slot *slot =
  1243. (struct NCR_700_command_slot *)SCp->host_scribble;
  1244. struct NCR_700_Host_Parameters *hostdata =
  1245. (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
  1246. __u16 count = 1; /* for IDENTIFY message */
  1247. if(hostdata->state != NCR_700_HOST_FREE) {
  1248. /* keep this inside the lock to close the race window where
  1249. * the running command finishes on another CPU while we don't
  1250. * change the state to queued on this one */
  1251. slot->state = NCR_700_SLOT_QUEUED;
  1252. DEBUG(("scsi%d: host busy, queueing command %p, slot %p\n",
  1253. SCp->device->host->host_no, slot->cmnd, slot));
  1254. return 0;
  1255. }
  1256. hostdata->state = NCR_700_HOST_BUSY;
  1257. hostdata->cmd = SCp;
  1258. slot->state = NCR_700_SLOT_BUSY;
  1259. /* keep interrupts disabled until we have the command correctly
  1260. * set up so we cannot take a selection interrupt */
  1261. hostdata->msgout[0] = NCR_700_identify(SCp->cmnd[0] != REQUEST_SENSE,
  1262. SCp->device->lun);
  1263. /* for INQUIRY or REQUEST_SENSE commands, we cannot be sure
  1264. * if the negotiated transfer parameters still hold, so
  1265. * always renegotiate them */
  1266. if(SCp->cmnd[0] == INQUIRY || SCp->cmnd[0] == REQUEST_SENSE) {
  1267. NCR_700_clear_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
  1268. }
  1269. /* REQUEST_SENSE is asking for contingent I_T_L(_Q) status.
  1270. * If a contingent allegiance condition exists, the device
  1271. * will refuse all tags, so send the request sense as untagged
  1272. * */
  1273. if((hostdata->tag_negotiated & (1<<SCp->device->id))
  1274. && (slot->tag != SCSI_NO_TAG && SCp->cmnd[0] != REQUEST_SENSE)) {
  1275. count += scsi_populate_tag_msg(SCp, &hostdata->msgout[count]);
  1276. }
  1277. if(hostdata->fast &&
  1278. NCR_700_is_flag_clear(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC)) {
  1279. memcpy(&hostdata->msgout[count], NCR_700_SDTR_msg,
  1280. sizeof(NCR_700_SDTR_msg));
  1281. hostdata->msgout[count+3] = spi_period(SCp->device->sdev_target);
  1282. hostdata->msgout[count+4] = spi_offset(SCp->device->sdev_target);
  1283. count += sizeof(NCR_700_SDTR_msg);
  1284. NCR_700_set_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  1285. }
  1286. script_patch_16(hostdata->script, MessageCount, count);
  1287. script_patch_ID(hostdata->script,
  1288. Device_ID, 1<<SCp->device->id);
  1289. script_patch_32_abs(hostdata->script, CommandAddress,
  1290. slot->pCmd);
  1291. script_patch_16(hostdata->script, CommandCount, SCp->cmd_len);
  1292. /* finally plumb the beginning of the SG list into the script
  1293. * */
  1294. script_patch_32_abs(hostdata->script, SGScriptStartAddress,
  1295. to32bit(&slot->pSG[0].ins));
  1296. NCR_700_clear_fifo(SCp->device->host);
  1297. if(slot->resume_offset == 0)
  1298. slot->resume_offset = hostdata->pScript;
  1299. /* now perform all the writebacks and invalidates */
  1300. dma_cache_sync(hostdata->msgout, count, DMA_TO_DEVICE);
  1301. dma_cache_sync(hostdata->msgin, MSG_ARRAY_SIZE,
  1302. DMA_FROM_DEVICE);
  1303. dma_cache_sync(SCp->cmnd, SCp->cmd_len, DMA_TO_DEVICE);
  1304. dma_cache_sync(hostdata->status, 1, DMA_FROM_DEVICE);
  1305. /* set the synchronous period/offset */
  1306. NCR_700_writeb(NCR_700_get_SXFER(SCp->device),
  1307. SCp->device->host, SXFER_REG);
  1308. NCR_700_writel(slot->temp, SCp->device->host, TEMP_REG);
  1309. NCR_700_writel(slot->resume_offset, SCp->device->host, DSP_REG);
  1310. return 1;
  1311. }
  1312. irqreturn_t
  1313. NCR_700_intr(int irq, void *dev_id, struct pt_regs *regs)
  1314. {
  1315. struct Scsi_Host *host = (struct Scsi_Host *)dev_id;
  1316. struct NCR_700_Host_Parameters *hostdata =
  1317. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  1318. __u8 istat;
  1319. __u32 resume_offset = 0;
  1320. __u8 pun = 0xff, lun = 0xff;
  1321. unsigned long flags;
  1322. int handled = 0;
  1323. /* Use the host lock to serialise acess to the 53c700
  1324. * hardware. Note: In future, we may need to take the queue
  1325. * lock to enter the done routines. When that happens, we
  1326. * need to ensure that for this driver, the host lock and the
  1327. * queue lock point to the same thing. */
  1328. spin_lock_irqsave(host->host_lock, flags);
  1329. if((istat = NCR_700_readb(host, ISTAT_REG))
  1330. & (SCSI_INT_PENDING | DMA_INT_PENDING)) {
  1331. __u32 dsps;
  1332. __u8 sstat0 = 0, dstat = 0;
  1333. __u32 dsp;
  1334. struct scsi_cmnd *SCp = hostdata->cmd;
  1335. enum NCR_700_Host_State state;
  1336. handled = 1;
  1337. state = hostdata->state;
  1338. SCp = hostdata->cmd;
  1339. if(istat & SCSI_INT_PENDING) {
  1340. udelay(10);
  1341. sstat0 = NCR_700_readb(host, SSTAT0_REG);
  1342. }
  1343. if(istat & DMA_INT_PENDING) {
  1344. udelay(10);
  1345. dstat = NCR_700_readb(host, DSTAT_REG);
  1346. }
  1347. dsps = NCR_700_readl(host, DSPS_REG);
  1348. dsp = NCR_700_readl(host, DSP_REG);
  1349. DEBUG(("scsi%d: istat %02x sstat0 %02x dstat %02x dsp %04x[%08x] dsps 0x%x\n",
  1350. host->host_no, istat, sstat0, dstat,
  1351. (dsp - (__u32)(hostdata->pScript))/4,
  1352. dsp, dsps));
  1353. if(SCp != NULL) {
  1354. pun = SCp->device->id;
  1355. lun = SCp->device->lun;
  1356. }
  1357. if(sstat0 & SCSI_RESET_DETECTED) {
  1358. struct scsi_device *SDp;
  1359. int i;
  1360. hostdata->state = NCR_700_HOST_BUSY;
  1361. printk(KERN_ERR "scsi%d: Bus Reset detected, executing command %p, slot %p, dsp %08x[%04x]\n",
  1362. host->host_no, SCp, SCp == NULL ? NULL : SCp->host_scribble, dsp, dsp - hostdata->pScript);
  1363. scsi_report_bus_reset(host, 0);
  1364. /* clear all the negotiated parameters */
  1365. __shost_for_each_device(SDp, host)
  1366. SDp->hostdata = NULL;
  1367. /* clear all the slots and their pending commands */
  1368. for(i = 0; i < NCR_700_COMMAND_SLOTS_PER_HOST; i++) {
  1369. struct scsi_cmnd *SCp;
  1370. struct NCR_700_command_slot *slot =
  1371. &hostdata->slots[i];
  1372. if(slot->state == NCR_700_SLOT_FREE)
  1373. continue;
  1374. SCp = slot->cmnd;
  1375. printk(KERN_ERR " failing command because of reset, slot %p, cmnd %p\n",
  1376. slot, SCp);
  1377. free_slot(slot, hostdata);
  1378. SCp->host_scribble = NULL;
  1379. NCR_700_set_depth(SCp->device, 0);
  1380. /* NOTE: deadlock potential here: we
  1381. * rely on mid-layer guarantees that
  1382. * scsi_done won't try to issue the
  1383. * command again otherwise we'll
  1384. * deadlock on the
  1385. * hostdata->state_lock */
  1386. SCp->result = DID_RESET << 16;
  1387. SCp->scsi_done(SCp);
  1388. }
  1389. mdelay(25);
  1390. NCR_700_chip_setup(host);
  1391. hostdata->state = NCR_700_HOST_FREE;
  1392. hostdata->cmd = NULL;
  1393. /* signal back if this was an eh induced reset */
  1394. if(hostdata->eh_complete != NULL)
  1395. complete(hostdata->eh_complete);
  1396. goto out_unlock;
  1397. } else if(sstat0 & SELECTION_TIMEOUT) {
  1398. DEBUG(("scsi%d: (%d:%d) selection timeout\n",
  1399. host->host_no, pun, lun));
  1400. NCR_700_scsi_done(hostdata, SCp, DID_NO_CONNECT<<16);
  1401. } else if(sstat0 & PHASE_MISMATCH) {
  1402. struct NCR_700_command_slot *slot = (SCp == NULL) ? NULL :
  1403. (struct NCR_700_command_slot *)SCp->host_scribble;
  1404. if(dsp == Ent_SendMessage + 8 + hostdata->pScript) {
  1405. /* It wants to reply to some part of
  1406. * our message */
  1407. #ifdef NCR_700_DEBUG
  1408. __u32 temp = NCR_700_readl(host, TEMP_REG);
  1409. int count = (hostdata->script[Ent_SendMessage/4] & 0xffffff) - ((NCR_700_readl(host, DBC_REG) & 0xffffff) + NCR_700_data_residual(host));
  1410. printk("scsi%d (%d:%d) PHASE MISMATCH IN SEND MESSAGE %d remain, return %p[%04x], phase %s\n", host->host_no, pun, lun, count, (void *)temp, temp - hostdata->pScript, sbcl_to_string(NCR_700_readb(host, SBCL_REG)));
  1411. #endif
  1412. resume_offset = hostdata->pScript + Ent_SendMessagePhaseMismatch;
  1413. } else if(dsp >= to32bit(&slot->pSG[0].ins) &&
  1414. dsp <= to32bit(&slot->pSG[NCR_700_SG_SEGMENTS].ins)) {
  1415. int data_transfer = NCR_700_readl(host, DBC_REG) & 0xffffff;
  1416. int SGcount = (dsp - to32bit(&slot->pSG[0].ins))/sizeof(struct NCR_700_SG_List);
  1417. int residual = NCR_700_data_residual(host);
  1418. int i;
  1419. #ifdef NCR_700_DEBUG
  1420. __u32 naddr = NCR_700_readl(host, DNAD_REG);
  1421. printk("scsi%d: (%d:%d) Expected phase mismatch in slot->SG[%d], transferred 0x%x\n",
  1422. host->host_no, pun, lun,
  1423. SGcount, data_transfer);
  1424. scsi_print_command(SCp);
  1425. if(residual) {
  1426. printk("scsi%d: (%d:%d) Expected phase mismatch in slot->SG[%d], transferred 0x%x, residual %d\n",
  1427. host->host_no, pun, lun,
  1428. SGcount, data_transfer, residual);
  1429. }
  1430. #endif
  1431. data_transfer += residual;
  1432. if(data_transfer != 0) {
  1433. int count;
  1434. __u32 pAddr;
  1435. SGcount--;
  1436. count = (bS_to_cpu(slot->SG[SGcount].ins) & 0x00ffffff);
  1437. DEBUG(("DATA TRANSFER MISMATCH, count = %d, transferred %d\n", count, count-data_transfer));
  1438. slot->SG[SGcount].ins &= bS_to_host(0xff000000);
  1439. slot->SG[SGcount].ins |= bS_to_host(data_transfer);
  1440. pAddr = bS_to_cpu(slot->SG[SGcount].pAddr);
  1441. pAddr += (count - data_transfer);
  1442. #ifdef NCR_700_DEBUG
  1443. if(pAddr != naddr) {
  1444. printk("scsi%d (%d:%d) transfer mismatch pAddr=%lx, naddr=%lx, data_transfer=%d, residual=%d\n", host->host_no, pun, lun, (unsigned long)pAddr, (unsigned long)naddr, data_transfer, residual);
  1445. }
  1446. #endif
  1447. slot->SG[SGcount].pAddr = bS_to_host(pAddr);
  1448. }
  1449. /* set the executed moves to nops */
  1450. for(i=0; i<SGcount; i++) {
  1451. slot->SG[i].ins = bS_to_host(SCRIPT_NOP);
  1452. slot->SG[i].pAddr = 0;
  1453. }
  1454. dma_cache_sync(slot->SG, sizeof(slot->SG), DMA_TO_DEVICE);
  1455. /* and pretend we disconnected after
  1456. * the command phase */
  1457. resume_offset = hostdata->pScript + Ent_MsgInDuringData;
  1458. /* make sure all the data is flushed */
  1459. NCR_700_flush_fifo(host);
  1460. } else {
  1461. __u8 sbcl = NCR_700_readb(host, SBCL_REG);
  1462. printk(KERN_ERR "scsi%d: (%d:%d) phase mismatch at %04x, phase %s\n",
  1463. host->host_no, pun, lun, dsp - hostdata->pScript, sbcl_to_string(sbcl));
  1464. NCR_700_internal_bus_reset(host);
  1465. }
  1466. } else if(sstat0 & SCSI_GROSS_ERROR) {
  1467. printk(KERN_ERR "scsi%d: (%d:%d) GROSS ERROR\n",
  1468. host->host_no, pun, lun);
  1469. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1470. } else if(sstat0 & PARITY_ERROR) {
  1471. printk(KERN_ERR "scsi%d: (%d:%d) PARITY ERROR\n",
  1472. host->host_no, pun, lun);
  1473. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1474. } else if(dstat & SCRIPT_INT_RECEIVED) {
  1475. DEBUG(("scsi%d: (%d:%d) ====>SCRIPT INTERRUPT<====\n",
  1476. host->host_no, pun, lun));
  1477. resume_offset = process_script_interrupt(dsps, dsp, SCp, host, hostdata);
  1478. } else if(dstat & (ILGL_INST_DETECTED)) {
  1479. printk(KERN_ERR "scsi%d: (%d:%d) Illegal Instruction detected at 0x%08x[0x%x]!!!\n"
  1480. " Please email James.Bottomley@HansenPartnership.com with the details\n",
  1481. host->host_no, pun, lun,
  1482. dsp, dsp - hostdata->pScript);
  1483. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1484. } else if(dstat & (WATCH_DOG_INTERRUPT|ABORTED)) {
  1485. printk(KERN_ERR "scsi%d: (%d:%d) serious DMA problem, dstat=%02x\n",
  1486. host->host_no, pun, lun, dstat);
  1487. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1488. }
  1489. /* NOTE: selection interrupt processing MUST occur
  1490. * after script interrupt processing to correctly cope
  1491. * with the case where we process a disconnect and
  1492. * then get reselected before we process the
  1493. * disconnection */
  1494. if(sstat0 & SELECTED) {
  1495. /* FIXME: It currently takes at least FOUR
  1496. * interrupts to complete a command that
  1497. * disconnects: one for the disconnect, one
  1498. * for the reselection, one to get the
  1499. * reselection data and one to complete the
  1500. * command. If we guess the reselected
  1501. * command here and prepare it, we only need
  1502. * to get a reselection data interrupt if we
  1503. * guessed wrongly. Since the interrupt
  1504. * overhead is much greater than the command
  1505. * setup, this would be an efficient
  1506. * optimisation particularly as we probably
  1507. * only have one outstanding command on a
  1508. * target most of the time */
  1509. resume_offset = process_selection(host, dsp);
  1510. }
  1511. }
  1512. if(resume_offset) {
  1513. if(hostdata->state != NCR_700_HOST_BUSY) {
  1514. printk(KERN_ERR "scsi%d: Driver error: resume at 0x%08x [0x%04x] with non busy host!\n",
  1515. host->host_no, resume_offset, resume_offset - hostdata->pScript);
  1516. hostdata->state = NCR_700_HOST_BUSY;
  1517. }
  1518. DEBUG(("Attempting to resume at %x\n", resume_offset));
  1519. NCR_700_clear_fifo(host);
  1520. NCR_700_writel(resume_offset, host, DSP_REG);
  1521. }
  1522. /* There is probably a technical no-no about this: If we're a
  1523. * shared interrupt and we got this interrupt because the
  1524. * other device needs servicing not us, we're still going to
  1525. * check our queued commands here---of course, there shouldn't
  1526. * be any outstanding.... */
  1527. if(hostdata->state == NCR_700_HOST_FREE) {
  1528. int i;
  1529. for(i = 0; i < NCR_700_COMMAND_SLOTS_PER_HOST; i++) {
  1530. /* fairness: always run the queue from the last
  1531. * position we left off */
  1532. int j = (i + hostdata->saved_slot_position)
  1533. % NCR_700_COMMAND_SLOTS_PER_HOST;
  1534. if(hostdata->slots[j].state != NCR_700_SLOT_QUEUED)
  1535. continue;
  1536. if(NCR_700_start_command(hostdata->slots[j].cmnd)) {
  1537. DEBUG(("scsi%d: Issuing saved command slot %p, cmd %p\t\n",
  1538. host->host_no, &hostdata->slots[j],
  1539. hostdata->slots[j].cmnd));
  1540. hostdata->saved_slot_position = j + 1;
  1541. }
  1542. break;
  1543. }
  1544. }
  1545. out_unlock:
  1546. spin_unlock_irqrestore(host->host_lock, flags);
  1547. return IRQ_RETVAL(handled);
  1548. }
  1549. STATIC int
  1550. NCR_700_queuecommand(struct scsi_cmnd *SCp, void (*done)(struct scsi_cmnd *))
  1551. {
  1552. struct NCR_700_Host_Parameters *hostdata =
  1553. (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
  1554. __u32 move_ins;
  1555. enum dma_data_direction direction;
  1556. struct NCR_700_command_slot *slot;
  1557. if(hostdata->command_slot_count >= NCR_700_COMMAND_SLOTS_PER_HOST) {
  1558. /* We're over our allocation, this should never happen
  1559. * since we report the max allocation to the mid layer */
  1560. printk(KERN_WARNING "scsi%d: Command depth has gone over queue depth\n", SCp->device->host->host_no);
  1561. return 1;
  1562. }
  1563. /* check for untagged commands. We cannot have any outstanding
  1564. * commands if we accept them. Commands could be untagged because:
  1565. *
  1566. * - The tag negotiated bitmap is clear
  1567. * - The blk layer sent and untagged command
  1568. */
  1569. if(NCR_700_get_depth(SCp->device) != 0
  1570. && (!(hostdata->tag_negotiated & (1<<SCp->device->id))
  1571. || !blk_rq_tagged(SCp->request))) {
  1572. DEBUG((KERN_ERR "scsi%d (%d:%d) has non zero depth %d\n",
  1573. SCp->device->host->host_no, SCp->device->id, SCp->device->lun,
  1574. NCR_700_get_depth(SCp->device)));
  1575. return SCSI_MLQUEUE_DEVICE_BUSY;
  1576. }
  1577. if(NCR_700_get_depth(SCp->device) >= SCp->device->queue_depth) {
  1578. DEBUG((KERN_ERR "scsi%d (%d:%d) has max tag depth %d\n",
  1579. SCp->device->host->host_no, SCp->device->id, SCp->device->lun,
  1580. NCR_700_get_depth(SCp->device)));
  1581. return SCSI_MLQUEUE_DEVICE_BUSY;
  1582. }
  1583. NCR_700_set_depth(SCp->device, NCR_700_get_depth(SCp->device) + 1);
  1584. /* begin the command here */
  1585. /* no need to check for NULL, test for command_slot_count above
  1586. * ensures a slot is free */
  1587. slot = find_empty_slot(hostdata);
  1588. slot->cmnd = SCp;
  1589. SCp->scsi_done = done;
  1590. SCp->host_scribble = (unsigned char *)slot;
  1591. SCp->SCp.ptr = NULL;
  1592. SCp->SCp.buffer = NULL;
  1593. #ifdef NCR_700_DEBUG
  1594. printk("53c700: scsi%d, command ", SCp->device->host->host_no);
  1595. scsi_print_command(SCp);
  1596. #endif
  1597. if(blk_rq_tagged(SCp->request)
  1598. && (hostdata->tag_negotiated &(1<<SCp->device->id)) == 0
  1599. && NCR_700_get_tag_neg_state(SCp->device) == NCR_700_START_TAG_NEGOTIATION) {
  1600. printk(KERN_ERR "scsi%d: (%d:%d) Enabling Tag Command Queuing\n", SCp->device->host->host_no, SCp->device->id, SCp->device->lun);
  1601. hostdata->tag_negotiated |= (1<<SCp->device->id);
  1602. NCR_700_set_tag_neg_state(SCp->device, NCR_700_DURING_TAG_NEGOTIATION);
  1603. }
  1604. /* here we may have to process an untagged command. The gate
  1605. * above ensures that this will be the only one outstanding,
  1606. * so clear the tag negotiated bit.
  1607. *
  1608. * FIXME: This will royally screw up on multiple LUN devices
  1609. * */
  1610. if(!blk_rq_tagged(SCp->request)
  1611. && (hostdata->tag_negotiated &(1<<SCp->device->id))) {
  1612. printk(KERN_INFO "scsi%d: (%d:%d) Disabling Tag Command Queuing\n", SCp->device->host->host_no, SCp->device->id, SCp->device->lun);
  1613. hostdata->tag_negotiated &= ~(1<<SCp->device->id);
  1614. }
  1615. if((hostdata->tag_negotiated &(1<<SCp->device->id))
  1616. && scsi_get_tag_type(SCp->device)) {
  1617. slot->tag = SCp->request->tag;
  1618. DEBUG(("53c700 %d:%d:%d, sending out tag %d, slot %p\n",
  1619. SCp->device->host->host_no, SCp->device->id, SCp->device->lun, slot->tag,
  1620. slot));
  1621. } else {
  1622. slot->tag = SCSI_NO_TAG;
  1623. /* must populate current_cmnd for scsi_find_tag to work */
  1624. SCp->device->current_cmnd = SCp;
  1625. }
  1626. /* sanity check: some of the commands generated by the mid-layer
  1627. * have an eccentric idea of their sc_data_direction */
  1628. if(!SCp->use_sg && !SCp->request_bufflen
  1629. && SCp->sc_data_direction != DMA_NONE) {
  1630. #ifdef NCR_700_DEBUG
  1631. printk("53c700: Command");
  1632. scsi_print_command(SCp);
  1633. printk("Has wrong data direction %d\n", SCp->sc_data_direction);
  1634. #endif
  1635. SCp->sc_data_direction = DMA_NONE;
  1636. }
  1637. switch (SCp->cmnd[0]) {
  1638. case REQUEST_SENSE:
  1639. /* clear the internal sense magic */
  1640. SCp->cmnd[6] = 0;
  1641. /* fall through */
  1642. default:
  1643. /* OK, get it from the command */
  1644. switch(SCp->sc_data_direction) {
  1645. case DMA_BIDIRECTIONAL:
  1646. default:
  1647. printk(KERN_ERR "53c700: Unknown command for data direction ");
  1648. scsi_print_command(SCp);
  1649. move_ins = 0;
  1650. break;
  1651. case DMA_NONE:
  1652. move_ins = 0;
  1653. break;
  1654. case DMA_FROM_DEVICE:
  1655. move_ins = SCRIPT_MOVE_DATA_IN;
  1656. break;
  1657. case DMA_TO_DEVICE:
  1658. move_ins = SCRIPT_MOVE_DATA_OUT;
  1659. break;
  1660. }
  1661. }
  1662. /* now build the scatter gather list */
  1663. direction = SCp->sc_data_direction;
  1664. if(move_ins != 0) {
  1665. int i;
  1666. int sg_count;
  1667. dma_addr_t vPtr = 0;
  1668. __u32 count = 0;
  1669. if(SCp->use_sg) {
  1670. sg_count = dma_map_sg(hostdata->dev, SCp->buffer,
  1671. SCp->use_sg, direction);
  1672. } else {
  1673. vPtr = dma_map_single(hostdata->dev,
  1674. SCp->request_buffer,
  1675. SCp->request_bufflen,
  1676. direction);
  1677. count = SCp->request_bufflen;
  1678. slot->dma_handle = vPtr;
  1679. sg_count = 1;
  1680. }
  1681. for(i = 0; i < sg_count; i++) {
  1682. if(SCp->use_sg) {
  1683. struct scatterlist *sg = SCp->buffer;
  1684. vPtr = sg_dma_address(&sg[i]);
  1685. count = sg_dma_len(&sg[i]);
  1686. }
  1687. slot->SG[i].ins = bS_to_host(move_ins | count);
  1688. DEBUG((" scatter block %d: move %d[%08x] from 0x%lx\n",
  1689. i, count, slot->SG[i].ins, (unsigned long)vPtr));
  1690. slot->SG[i].pAddr = bS_to_host(vPtr);
  1691. }
  1692. slot->SG[i].ins = bS_to_host(SCRIPT_RETURN);
  1693. slot->SG[i].pAddr = 0;
  1694. dma_cache_sync(slot->SG, sizeof(slot->SG), DMA_TO_DEVICE);
  1695. DEBUG((" SETTING %08lx to %x\n",
  1696. (&slot->pSG[i].ins),
  1697. slot->SG[i].ins));
  1698. }
  1699. slot->resume_offset = 0;
  1700. slot->pCmd = dma_map_single(hostdata->dev, SCp->cmnd,
  1701. sizeof(SCp->cmnd), DMA_TO_DEVICE);
  1702. NCR_700_start_command(SCp);
  1703. return 0;
  1704. }
  1705. STATIC int
  1706. NCR_700_abort(struct scsi_cmnd * SCp)
  1707. {
  1708. struct NCR_700_command_slot *slot;
  1709. printk(KERN_INFO "scsi%d (%d:%d) New error handler wants to abort command\n\t",
  1710. SCp->device->host->host_no, SCp->device->id, SCp->device->lun);
  1711. scsi_print_command(SCp);
  1712. slot = (struct NCR_700_command_slot *)SCp->host_scribble;
  1713. if(slot == NULL)
  1714. /* no outstanding command to abort */
  1715. return SUCCESS;
  1716. if(SCp->cmnd[0] == TEST_UNIT_READY) {
  1717. /* FIXME: This is because of a problem in the new
  1718. * error handler. When it is in error recovery, it
  1719. * will send a TUR to a device it thinks may still be
  1720. * showing a problem. If the TUR isn't responded to,
  1721. * it will abort it and mark the device off line.
  1722. * Unfortunately, it does no other error recovery, so
  1723. * this would leave us with an outstanding command
  1724. * occupying a slot. Rather than allow this to
  1725. * happen, we issue a bus reset to force all
  1726. * outstanding commands to terminate here. */
  1727. NCR_700_internal_bus_reset(SCp->device->host);
  1728. /* still drop through and return failed */
  1729. }
  1730. return FAILED;
  1731. }
  1732. STATIC int
  1733. NCR_700_bus_reset(struct scsi_cmnd * SCp)
  1734. {
  1735. DECLARE_COMPLETION(complete);
  1736. struct NCR_700_Host_Parameters *hostdata =
  1737. (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
  1738. printk(KERN_INFO "scsi%d (%d:%d) New error handler wants BUS reset, cmd %p\n\t",
  1739. SCp->device->host->host_no, SCp->device->id, SCp->device->lun, SCp);
  1740. scsi_print_command(SCp);
  1741. /* In theory, eh_complete should always be null because the
  1742. * eh is single threaded, but just in case we're handling a
  1743. * reset via sg or something */
  1744. while(hostdata->eh_complete != NULL) {
  1745. spin_unlock_irq(SCp->device->host->host_lock);
  1746. msleep_interruptible(100);
  1747. spin_lock_irq(SCp->device->host->host_lock);
  1748. }
  1749. hostdata->eh_complete = &complete;
  1750. NCR_700_internal_bus_reset(SCp->device->host);
  1751. spin_unlock_irq(SCp->device->host->host_lock);
  1752. wait_for_completion(&complete);
  1753. spin_lock_irq(SCp->device->host->host_lock);
  1754. hostdata->eh_complete = NULL;
  1755. /* Revalidate the transport parameters of the failing device */
  1756. if(hostdata->fast)
  1757. spi_schedule_dv_device(SCp->device);
  1758. return SUCCESS;
  1759. }
  1760. STATIC int
  1761. NCR_700_dev_reset(struct scsi_cmnd * SCp)
  1762. {
  1763. printk(KERN_INFO "scsi%d (%d:%d) New error handler wants device reset\n\t",
  1764. SCp->device->host->host_no, SCp->device->id, SCp->device->lun);
  1765. scsi_print_command(SCp);
  1766. return FAILED;
  1767. }
  1768. STATIC int
  1769. NCR_700_host_reset(struct scsi_cmnd * SCp)
  1770. {
  1771. printk(KERN_INFO "scsi%d (%d:%d) New error handler wants HOST reset\n\t",
  1772. SCp->device->host->host_no, SCp->device->id, SCp->device->lun);
  1773. scsi_print_command(SCp);
  1774. NCR_700_internal_bus_reset(SCp->device->host);
  1775. NCR_700_chip_reset(SCp->device->host);
  1776. return SUCCESS;
  1777. }
  1778. STATIC void
  1779. NCR_700_set_period(struct scsi_target *STp, int period)
  1780. {
  1781. struct Scsi_Host *SHp = dev_to_shost(STp->dev.parent);
  1782. struct NCR_700_Host_Parameters *hostdata =
  1783. (struct NCR_700_Host_Parameters *)SHp->hostdata[0];
  1784. if(!hostdata->fast)
  1785. return;
  1786. if(period < hostdata->min_period)
  1787. period = hostdata->min_period;
  1788. spi_period(STp) = period;
  1789. spi_flags(STp) &= ~(NCR_700_DEV_NEGOTIATED_SYNC |
  1790. NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  1791. spi_flags(STp) |= NCR_700_DEV_PRINT_SYNC_NEGOTIATION;
  1792. }
  1793. STATIC void
  1794. NCR_700_set_offset(struct scsi_target *STp, int offset)
  1795. {
  1796. struct Scsi_Host *SHp = dev_to_shost(STp->dev.parent);
  1797. struct NCR_700_Host_Parameters *hostdata =
  1798. (struct NCR_700_Host_Parameters *)SHp->hostdata[0];
  1799. int max_offset = hostdata->chip710
  1800. ? NCR_710_MAX_OFFSET : NCR_700_MAX_OFFSET;
  1801. if(!hostdata->fast)
  1802. return;
  1803. if(offset > max_offset)
  1804. offset = max_offset;
  1805. /* if we're currently async, make sure the period is reasonable */
  1806. if(spi_offset(STp) == 0 && (spi_period(STp) < hostdata->min_period ||
  1807. spi_period(STp) > 0xff))
  1808. spi_period(STp) = hostdata->min_period;
  1809. spi_offset(STp) = offset;
  1810. spi_flags(STp) &= ~(NCR_700_DEV_NEGOTIATED_SYNC |
  1811. NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  1812. spi_flags(STp) |= NCR_700_DEV_PRINT_SYNC_NEGOTIATION;
  1813. }
  1814. STATIC int
  1815. NCR_700_slave_configure(struct scsi_device *SDp)
  1816. {
  1817. struct NCR_700_Host_Parameters *hostdata =
  1818. (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
  1819. /* to do here: allocate memory; build a queue_full list */
  1820. if(SDp->tagged_supported) {
  1821. scsi_set_tag_type(SDp, MSG_ORDERED_TAG);
  1822. scsi_activate_tcq(SDp, NCR_700_DEFAULT_TAGS);
  1823. NCR_700_set_tag_neg_state(SDp, NCR_700_START_TAG_NEGOTIATION);
  1824. } else {
  1825. /* initialise to default depth */
  1826. scsi_adjust_queue_depth(SDp, 0, SDp->host->cmd_per_lun);
  1827. }
  1828. if(hostdata->fast) {
  1829. /* Find the correct offset and period via domain validation */
  1830. if (!spi_initial_dv(SDp->sdev_target))
  1831. spi_dv_device(SDp);
  1832. } else {
  1833. spi_offset(SDp->sdev_target) = 0;
  1834. spi_period(SDp->sdev_target) = 0;
  1835. }
  1836. return 0;
  1837. }
  1838. STATIC void
  1839. NCR_700_slave_destroy(struct scsi_device *SDp)
  1840. {
  1841. /* to do here: deallocate memory */
  1842. }
  1843. static int
  1844. NCR_700_change_queue_depth(struct scsi_device *SDp, int depth)
  1845. {
  1846. if (depth > NCR_700_MAX_TAGS)
  1847. depth = NCR_700_MAX_TAGS;
  1848. scsi_adjust_queue_depth(SDp, scsi_get_tag_type(SDp), depth);
  1849. return depth;
  1850. }
  1851. static int NCR_700_change_queue_type(struct scsi_device *SDp, int tag_type)
  1852. {
  1853. int change_tag = ((tag_type ==0 && scsi_get_tag_type(SDp) != 0)
  1854. || (tag_type != 0 && scsi_get_tag_type(SDp) == 0));
  1855. struct NCR_700_Host_Parameters *hostdata =
  1856. (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
  1857. scsi_set_tag_type(SDp, tag_type);
  1858. /* We have a global (per target) flag to track whether TCQ is
  1859. * enabled, so we'll be turning it off for the entire target here.
  1860. * our tag algorithm will fail if we mix tagged and untagged commands,
  1861. * so quiesce the device before doing this */
  1862. if (change_tag)
  1863. scsi_target_quiesce(SDp->sdev_target);
  1864. if (!tag_type) {
  1865. /* shift back to the default unqueued number of commands
  1866. * (the user can still raise this) */
  1867. scsi_deactivate_tcq(SDp, SDp->host->cmd_per_lun);
  1868. hostdata->tag_negotiated &= ~(1 << SDp->id);
  1869. } else {
  1870. /* Here, we cleared the negotiation flag above, so this
  1871. * will force the driver to renegotiate */
  1872. scsi_activate_tcq(SDp, SDp->queue_depth);
  1873. if (change_tag)
  1874. NCR_700_set_tag_neg_state(SDp, NCR_700_START_TAG_NEGOTIATION);
  1875. }
  1876. if (change_tag)
  1877. scsi_target_resume(SDp->sdev_target);
  1878. return tag_type;
  1879. }
  1880. static ssize_t
  1881. NCR_700_show_active_tags(struct device *dev, char *buf)
  1882. {
  1883. struct scsi_device *SDp = to_scsi_device(dev);
  1884. return snprintf(buf, 20, "%d\n", NCR_700_get_depth(SDp));
  1885. }
  1886. static struct device_attribute NCR_700_active_tags_attr = {
  1887. .attr = {
  1888. .name = "active_tags",
  1889. .mode = S_IRUGO,
  1890. },
  1891. .show = NCR_700_show_active_tags,
  1892. };
  1893. STATIC struct device_attribute *NCR_700_dev_attrs[] = {
  1894. &NCR_700_active_tags_attr,
  1895. NULL,
  1896. };
  1897. EXPORT_SYMBOL(NCR_700_detect);
  1898. EXPORT_SYMBOL(NCR_700_release);
  1899. EXPORT_SYMBOL(NCR_700_intr);
  1900. static struct spi_function_template NCR_700_transport_functions = {
  1901. .set_period = NCR_700_set_period,
  1902. .show_period = 1,
  1903. .set_offset = NCR_700_set_offset,
  1904. .show_offset = 1,
  1905. };
  1906. static int __init NCR_700_init(void)
  1907. {
  1908. NCR_700_transport_template = spi_attach_transport(&NCR_700_transport_functions);
  1909. if(!NCR_700_transport_template)
  1910. return -ENODEV;
  1911. return 0;
  1912. }
  1913. static void __exit NCR_700_exit(void)
  1914. {
  1915. spi_release_transport(NCR_700_transport_template);
  1916. }
  1917. module_init(NCR_700_init);
  1918. module_exit(NCR_700_exit);