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