53c700.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184
  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/sched.h>
  121. #include <linux/init.h>
  122. #include <linux/proc_fs.h>
  123. #include <linux/blkdev.h>
  124. #include <linux/module.h>
  125. #include <linux/interrupt.h>
  126. #include <linux/device.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_host_reset(struct scsi_cmnd * SCpnt);
  162. STATIC void NCR_700_chip_setup(struct Scsi_Host *host);
  163. STATIC void NCR_700_chip_reset(struct Scsi_Host *host);
  164. STATIC int NCR_700_slave_alloc(struct scsi_device *SDpnt);
  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. /* This translates the SDTR message offset and period to a value
  221. * which can be loaded into the SXFER_REG.
  222. *
  223. * NOTE: According to SCSI-2, the true transfer period (in ns) is
  224. * actually four times this period value */
  225. static inline __u8
  226. NCR_700_offset_period_to_sxfer(struct NCR_700_Host_Parameters *hostdata,
  227. __u8 offset, __u8 period)
  228. {
  229. int XFERP;
  230. __u8 min_xferp = (hostdata->chip710
  231. ? NCR_710_MIN_XFERP : NCR_700_MIN_XFERP);
  232. __u8 max_offset = (hostdata->chip710
  233. ? NCR_710_MAX_OFFSET : NCR_700_MAX_OFFSET);
  234. if(offset == 0)
  235. return 0;
  236. if(period < hostdata->min_period) {
  237. printk(KERN_WARNING "53c700: Period %dns is less than this chip's minimum, setting to %d\n", period*4, NCR_700_MIN_PERIOD*4);
  238. period = hostdata->min_period;
  239. }
  240. XFERP = (period*4 * hostdata->sync_clock)/1000 - 4;
  241. if(offset > max_offset) {
  242. printk(KERN_WARNING "53c700: Offset %d exceeds chip maximum, setting to %d\n",
  243. offset, max_offset);
  244. offset = max_offset;
  245. }
  246. if(XFERP < min_xferp) {
  247. printk(KERN_WARNING "53c700: XFERP %d is less than minium, setting to %d\n",
  248. XFERP, min_xferp);
  249. XFERP = min_xferp;
  250. }
  251. return (offset & 0x0f) | (XFERP & 0x07)<<4;
  252. }
  253. static inline __u8
  254. NCR_700_get_SXFER(struct scsi_device *SDp)
  255. {
  256. struct NCR_700_Host_Parameters *hostdata =
  257. (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
  258. return NCR_700_offset_period_to_sxfer(hostdata,
  259. spi_offset(SDp->sdev_target),
  260. spi_period(SDp->sdev_target));
  261. }
  262. struct Scsi_Host *
  263. NCR_700_detect(struct scsi_host_template *tpnt,
  264. struct NCR_700_Host_Parameters *hostdata, struct device *dev)
  265. {
  266. dma_addr_t pScript, pSlots;
  267. __u8 *memory;
  268. __u32 *script;
  269. struct Scsi_Host *host;
  270. static int banner = 0;
  271. int j;
  272. if(tpnt->sdev_attrs == NULL)
  273. tpnt->sdev_attrs = NCR_700_dev_attrs;
  274. memory = dma_alloc_noncoherent(hostdata->dev, TOTAL_MEM_SIZE,
  275. &pScript, GFP_KERNEL);
  276. if(memory == NULL) {
  277. printk(KERN_ERR "53c700: Failed to allocate memory for driver, detatching\n");
  278. return NULL;
  279. }
  280. script = (__u32 *)memory;
  281. hostdata->msgin = memory + MSGIN_OFFSET;
  282. hostdata->msgout = memory + MSGOUT_OFFSET;
  283. hostdata->status = memory + STATUS_OFFSET;
  284. /* all of these offsets are L1_CACHE_BYTES separated. It is fatal
  285. * if this isn't sufficient separation to avoid dma flushing issues */
  286. BUG_ON(!dma_is_consistent(hostdata->dev, pScript) && L1_CACHE_BYTES < dma_get_cache_alignment());
  287. hostdata->slots = (struct NCR_700_command_slot *)(memory + SLOTS_OFFSET);
  288. hostdata->dev = dev;
  289. pSlots = pScript + SLOTS_OFFSET;
  290. /* Fill in the missing routines from the host template */
  291. tpnt->queuecommand = NCR_700_queuecommand;
  292. tpnt->eh_abort_handler = NCR_700_abort;
  293. tpnt->eh_bus_reset_handler = NCR_700_bus_reset;
  294. tpnt->eh_host_reset_handler = NCR_700_host_reset;
  295. tpnt->can_queue = NCR_700_COMMAND_SLOTS_PER_HOST;
  296. tpnt->sg_tablesize = NCR_700_SG_SEGMENTS;
  297. tpnt->cmd_per_lun = NCR_700_CMD_PER_LUN;
  298. tpnt->use_clustering = ENABLE_CLUSTERING;
  299. tpnt->slave_configure = NCR_700_slave_configure;
  300. tpnt->slave_destroy = NCR_700_slave_destroy;
  301. tpnt->slave_alloc = NCR_700_slave_alloc;
  302. tpnt->change_queue_depth = NCR_700_change_queue_depth;
  303. tpnt->change_queue_type = NCR_700_change_queue_type;
  304. if(tpnt->name == NULL)
  305. tpnt->name = "53c700";
  306. if(tpnt->proc_name == NULL)
  307. tpnt->proc_name = "53c700";
  308. host = scsi_host_alloc(tpnt, 4);
  309. if (!host)
  310. return NULL;
  311. memset(hostdata->slots, 0, sizeof(struct NCR_700_command_slot)
  312. * NCR_700_COMMAND_SLOTS_PER_HOST);
  313. for (j = 0; j < NCR_700_COMMAND_SLOTS_PER_HOST; j++) {
  314. dma_addr_t offset = (dma_addr_t)((unsigned long)&hostdata->slots[j].SG[0]
  315. - (unsigned long)&hostdata->slots[0].SG[0]);
  316. hostdata->slots[j].pSG = (struct NCR_700_SG_List *)((unsigned long)(pSlots + offset));
  317. if(j == 0)
  318. hostdata->free_list = &hostdata->slots[j];
  319. else
  320. hostdata->slots[j-1].ITL_forw = &hostdata->slots[j];
  321. hostdata->slots[j].state = NCR_700_SLOT_FREE;
  322. }
  323. for (j = 0; j < ARRAY_SIZE(SCRIPT); j++)
  324. script[j] = bS_to_host(SCRIPT[j]);
  325. /* adjust all labels to be bus physical */
  326. for (j = 0; j < PATCHES; j++)
  327. script[LABELPATCHES[j]] = bS_to_host(pScript + SCRIPT[LABELPATCHES[j]]);
  328. /* now patch up fixed addresses. */
  329. script_patch_32(hostdata->dev, script, MessageLocation,
  330. pScript + MSGOUT_OFFSET);
  331. script_patch_32(hostdata->dev, script, StatusAddress,
  332. pScript + STATUS_OFFSET);
  333. script_patch_32(hostdata->dev, script, ReceiveMsgAddress,
  334. pScript + MSGIN_OFFSET);
  335. hostdata->script = script;
  336. hostdata->pScript = pScript;
  337. dma_sync_single_for_device(hostdata->dev, pScript, sizeof(SCRIPT), DMA_TO_DEVICE);
  338. hostdata->state = NCR_700_HOST_FREE;
  339. hostdata->cmd = NULL;
  340. host->max_id = 8;
  341. host->max_lun = NCR_700_MAX_LUNS;
  342. BUG_ON(NCR_700_transport_template == NULL);
  343. host->transportt = NCR_700_transport_template;
  344. host->unique_id = (unsigned long)hostdata->base;
  345. hostdata->eh_complete = NULL;
  346. host->hostdata[0] = (unsigned long)hostdata;
  347. /* kick the chip */
  348. NCR_700_writeb(0xff, host, CTEST9_REG);
  349. if (hostdata->chip710)
  350. hostdata->rev = (NCR_700_readb(host, CTEST8_REG)>>4) & 0x0f;
  351. else
  352. hostdata->rev = (NCR_700_readb(host, CTEST7_REG)>>4) & 0x0f;
  353. hostdata->fast = (NCR_700_readb(host, CTEST9_REG) == 0);
  354. if (banner == 0) {
  355. printk(KERN_NOTICE "53c700: Version " NCR_700_VERSION " By James.Bottomley@HansenPartnership.com\n");
  356. banner = 1;
  357. }
  358. printk(KERN_NOTICE "scsi%d: %s rev %d %s\n", host->host_no,
  359. hostdata->chip710 ? "53c710" :
  360. (hostdata->fast ? "53c700-66" : "53c700"),
  361. hostdata->rev, hostdata->differential ?
  362. "(Differential)" : "");
  363. /* reset the chip */
  364. NCR_700_chip_reset(host);
  365. if (scsi_add_host(host, dev)) {
  366. dev_printk(KERN_ERR, dev, "53c700: scsi_add_host failed\n");
  367. scsi_host_put(host);
  368. return NULL;
  369. }
  370. spi_signalling(host) = hostdata->differential ? SPI_SIGNAL_HVD :
  371. SPI_SIGNAL_SE;
  372. return host;
  373. }
  374. int
  375. NCR_700_release(struct Scsi_Host *host)
  376. {
  377. struct NCR_700_Host_Parameters *hostdata =
  378. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  379. dma_free_noncoherent(hostdata->dev, TOTAL_MEM_SIZE,
  380. hostdata->script, hostdata->pScript);
  381. return 1;
  382. }
  383. static inline __u8
  384. NCR_700_identify(int can_disconnect, __u8 lun)
  385. {
  386. return IDENTIFY_BASE |
  387. ((can_disconnect) ? 0x40 : 0) |
  388. (lun & NCR_700_LUN_MASK);
  389. }
  390. /*
  391. * Function : static int data_residual (Scsi_Host *host)
  392. *
  393. * Purpose : return residual data count of what's in the chip. If you
  394. * really want to know what this function is doing, it's almost a
  395. * direct transcription of the algorithm described in the 53c710
  396. * guide, except that the DBC and DFIFO registers are only 6 bits
  397. * wide on a 53c700.
  398. *
  399. * Inputs : host - SCSI host */
  400. static inline int
  401. NCR_700_data_residual (struct Scsi_Host *host) {
  402. struct NCR_700_Host_Parameters *hostdata =
  403. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  404. int count, synchronous = 0;
  405. unsigned int ddir;
  406. if(hostdata->chip710) {
  407. count = ((NCR_700_readb(host, DFIFO_REG) & 0x7f) -
  408. (NCR_700_readl(host, DBC_REG) & 0x7f)) & 0x7f;
  409. } else {
  410. count = ((NCR_700_readb(host, DFIFO_REG) & 0x3f) -
  411. (NCR_700_readl(host, DBC_REG) & 0x3f)) & 0x3f;
  412. }
  413. if(hostdata->fast)
  414. synchronous = NCR_700_readb(host, SXFER_REG) & 0x0f;
  415. /* get the data direction */
  416. ddir = NCR_700_readb(host, CTEST0_REG) & 0x01;
  417. if (ddir) {
  418. /* Receive */
  419. if (synchronous)
  420. count += (NCR_700_readb(host, SSTAT2_REG) & 0xf0) >> 4;
  421. else
  422. if (NCR_700_readb(host, SSTAT1_REG) & SIDL_REG_FULL)
  423. ++count;
  424. } else {
  425. /* Send */
  426. __u8 sstat = NCR_700_readb(host, SSTAT1_REG);
  427. if (sstat & SODL_REG_FULL)
  428. ++count;
  429. if (synchronous && (sstat & SODR_REG_FULL))
  430. ++count;
  431. }
  432. #ifdef NCR_700_DEBUG
  433. if(count)
  434. printk("RESIDUAL IS %d (ddir %d)\n", count, ddir);
  435. #endif
  436. return count;
  437. }
  438. /* print out the SCSI wires and corresponding phase from the SBCL register
  439. * in the chip */
  440. static inline char *
  441. sbcl_to_string(__u8 sbcl)
  442. {
  443. int i;
  444. static char ret[256];
  445. ret[0]='\0';
  446. for(i=0; i<8; i++) {
  447. if((1<<i) & sbcl)
  448. strcat(ret, NCR_700_SBCL_bits[i]);
  449. }
  450. strcat(ret, NCR_700_SBCL_to_phase[sbcl & 0x07]);
  451. return ret;
  452. }
  453. static inline __u8
  454. bitmap_to_number(__u8 bitmap)
  455. {
  456. __u8 i;
  457. for(i=0; i<8 && !(bitmap &(1<<i)); i++)
  458. ;
  459. return i;
  460. }
  461. /* Pull a slot off the free list */
  462. STATIC struct NCR_700_command_slot *
  463. find_empty_slot(struct NCR_700_Host_Parameters *hostdata)
  464. {
  465. struct NCR_700_command_slot *slot = hostdata->free_list;
  466. if(slot == NULL) {
  467. /* sanity check */
  468. if(hostdata->command_slot_count != NCR_700_COMMAND_SLOTS_PER_HOST)
  469. printk(KERN_ERR "SLOTS FULL, but count is %d, should be %d\n", hostdata->command_slot_count, NCR_700_COMMAND_SLOTS_PER_HOST);
  470. return NULL;
  471. }
  472. if(slot->state != NCR_700_SLOT_FREE)
  473. /* should panic! */
  474. printk(KERN_ERR "BUSY SLOT ON FREE LIST!!!\n");
  475. hostdata->free_list = slot->ITL_forw;
  476. slot->ITL_forw = NULL;
  477. /* NOTE: set the state to busy here, not queued, since this
  478. * indicates the slot is in use and cannot be run by the IRQ
  479. * finish routine. If we cannot queue the command when it
  480. * is properly build, we then change to NCR_700_SLOT_QUEUED */
  481. slot->state = NCR_700_SLOT_BUSY;
  482. slot->flags = 0;
  483. hostdata->command_slot_count++;
  484. return slot;
  485. }
  486. STATIC void
  487. free_slot(struct NCR_700_command_slot *slot,
  488. struct NCR_700_Host_Parameters *hostdata)
  489. {
  490. if((slot->state & NCR_700_SLOT_MASK) != NCR_700_SLOT_MAGIC) {
  491. printk(KERN_ERR "53c700: SLOT %p is not MAGIC!!!\n", slot);
  492. }
  493. if(slot->state == NCR_700_SLOT_FREE) {
  494. printk(KERN_ERR "53c700: SLOT %p is FREE!!!\n", slot);
  495. }
  496. slot->resume_offset = 0;
  497. slot->cmnd = NULL;
  498. slot->state = NCR_700_SLOT_FREE;
  499. slot->ITL_forw = hostdata->free_list;
  500. hostdata->free_list = slot;
  501. hostdata->command_slot_count--;
  502. }
  503. /* This routine really does very little. The command is indexed on
  504. the ITL and (if tagged) the ITLQ lists in _queuecommand */
  505. STATIC void
  506. save_for_reselection(struct NCR_700_Host_Parameters *hostdata,
  507. struct scsi_cmnd *SCp, __u32 dsp)
  508. {
  509. /* Its just possible that this gets executed twice */
  510. if(SCp != NULL) {
  511. struct NCR_700_command_slot *slot =
  512. (struct NCR_700_command_slot *)SCp->host_scribble;
  513. slot->resume_offset = dsp;
  514. }
  515. hostdata->state = NCR_700_HOST_FREE;
  516. hostdata->cmd = NULL;
  517. }
  518. STATIC inline void
  519. NCR_700_unmap(struct NCR_700_Host_Parameters *hostdata, struct scsi_cmnd *SCp,
  520. struct NCR_700_command_slot *slot)
  521. {
  522. if(SCp->sc_data_direction != DMA_NONE &&
  523. SCp->sc_data_direction != DMA_BIDIRECTIONAL) {
  524. if(SCp->use_sg) {
  525. dma_unmap_sg(hostdata->dev, SCp->request_buffer,
  526. SCp->use_sg, SCp->sc_data_direction);
  527. } else {
  528. dma_unmap_single(hostdata->dev, slot->dma_handle,
  529. SCp->request_bufflen,
  530. SCp->sc_data_direction);
  531. }
  532. }
  533. }
  534. STATIC inline void
  535. NCR_700_scsi_done(struct NCR_700_Host_Parameters *hostdata,
  536. struct scsi_cmnd *SCp, int result)
  537. {
  538. hostdata->state = NCR_700_HOST_FREE;
  539. hostdata->cmd = NULL;
  540. if(SCp != NULL) {
  541. struct NCR_700_command_slot *slot =
  542. (struct NCR_700_command_slot *)SCp->host_scribble;
  543. dma_unmap_single(hostdata->dev, slot->pCmd,
  544. sizeof(SCp->cmnd), DMA_TO_DEVICE);
  545. if (slot->flags == NCR_700_FLAG_AUTOSENSE) {
  546. char *cmnd = NCR_700_get_sense_cmnd(SCp->device);
  547. #ifdef NCR_700_DEBUG
  548. printk(" ORIGINAL CMD %p RETURNED %d, new return is %d sense is\n",
  549. SCp, SCp->cmnd[7], result);
  550. scsi_print_sense("53c700", SCp);
  551. #endif
  552. dma_unmap_single(hostdata->dev, slot->dma_handle, sizeof(SCp->sense_buffer), DMA_FROM_DEVICE);
  553. /* restore the old result if the request sense was
  554. * successful */
  555. if (result == 0)
  556. result = cmnd[7];
  557. /* restore the original length */
  558. SCp->cmd_len = cmnd[8];
  559. } else
  560. NCR_700_unmap(hostdata, SCp, slot);
  561. free_slot(slot, hostdata);
  562. #ifdef NCR_700_DEBUG
  563. if(NCR_700_get_depth(SCp->device) == 0 ||
  564. NCR_700_get_depth(SCp->device) > SCp->device->queue_depth)
  565. printk(KERN_ERR "Invalid depth in NCR_700_scsi_done(): %d\n",
  566. NCR_700_get_depth(SCp->device));
  567. #endif /* NCR_700_DEBUG */
  568. NCR_700_set_depth(SCp->device, NCR_700_get_depth(SCp->device) - 1);
  569. SCp->host_scribble = NULL;
  570. SCp->result = result;
  571. SCp->scsi_done(SCp);
  572. } else {
  573. printk(KERN_ERR "53c700: SCSI DONE HAS NULL SCp\n");
  574. }
  575. }
  576. STATIC void
  577. NCR_700_internal_bus_reset(struct Scsi_Host *host)
  578. {
  579. /* Bus reset */
  580. NCR_700_writeb(ASSERT_RST, host, SCNTL1_REG);
  581. udelay(50);
  582. NCR_700_writeb(0, host, SCNTL1_REG);
  583. }
  584. STATIC void
  585. NCR_700_chip_setup(struct Scsi_Host *host)
  586. {
  587. struct NCR_700_Host_Parameters *hostdata =
  588. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  589. __u32 dcntl_extra = 0;
  590. __u8 min_period;
  591. __u8 min_xferp = (hostdata->chip710 ? NCR_710_MIN_XFERP : NCR_700_MIN_XFERP);
  592. if(hostdata->chip710) {
  593. __u8 burst_disable = hostdata->burst_disable
  594. ? BURST_DISABLE : 0;
  595. dcntl_extra = COMPAT_700_MODE;
  596. NCR_700_writeb(dcntl_extra, host, DCNTL_REG);
  597. NCR_700_writeb(BURST_LENGTH_8 | hostdata->dmode_extra,
  598. host, DMODE_710_REG);
  599. NCR_700_writeb(burst_disable | (hostdata->differential ?
  600. DIFF : 0), host, CTEST7_REG);
  601. NCR_700_writeb(BTB_TIMER_DISABLE, host, CTEST0_REG);
  602. NCR_700_writeb(FULL_ARBITRATION | ENABLE_PARITY | PARITY
  603. | AUTO_ATN, host, SCNTL0_REG);
  604. } else {
  605. NCR_700_writeb(BURST_LENGTH_8 | hostdata->dmode_extra,
  606. host, DMODE_700_REG);
  607. NCR_700_writeb(hostdata->differential ?
  608. DIFF : 0, host, CTEST7_REG);
  609. if(hostdata->fast) {
  610. /* this is for 700-66, does nothing on 700 */
  611. NCR_700_writeb(LAST_DIS_ENBL | ENABLE_ACTIVE_NEGATION
  612. | GENERATE_RECEIVE_PARITY, host,
  613. CTEST8_REG);
  614. } else {
  615. NCR_700_writeb(FULL_ARBITRATION | ENABLE_PARITY
  616. | PARITY | AUTO_ATN, host, SCNTL0_REG);
  617. }
  618. }
  619. NCR_700_writeb(1 << host->this_id, host, SCID_REG);
  620. NCR_700_writeb(0, host, SBCL_REG);
  621. NCR_700_writeb(ASYNC_OPERATION, host, SXFER_REG);
  622. NCR_700_writeb(PHASE_MM_INT | SEL_TIMEOUT_INT | GROSS_ERR_INT | UX_DISC_INT
  623. | RST_INT | PAR_ERR_INT | SELECT_INT, host, SIEN_REG);
  624. NCR_700_writeb(ABORT_INT | INT_INST_INT | ILGL_INST_INT, host, DIEN_REG);
  625. NCR_700_writeb(ENABLE_SELECT, host, SCNTL1_REG);
  626. if(hostdata->clock > 75) {
  627. printk(KERN_ERR "53c700: Clock speed %dMHz is too high: 75Mhz is the maximum this chip can be driven at\n", hostdata->clock);
  628. /* do the best we can, but the async clock will be out
  629. * of spec: sync divider 2, async divider 3 */
  630. DEBUG(("53c700: sync 2 async 3\n"));
  631. NCR_700_writeb(SYNC_DIV_2_0, host, SBCL_REG);
  632. NCR_700_writeb(ASYNC_DIV_3_0 | dcntl_extra, host, DCNTL_REG);
  633. hostdata->sync_clock = hostdata->clock/2;
  634. } else if(hostdata->clock > 50 && hostdata->clock <= 75) {
  635. /* sync divider 1.5, async divider 3 */
  636. DEBUG(("53c700: sync 1.5 async 3\n"));
  637. NCR_700_writeb(SYNC_DIV_1_5, host, SBCL_REG);
  638. NCR_700_writeb(ASYNC_DIV_3_0 | dcntl_extra, host, DCNTL_REG);
  639. hostdata->sync_clock = hostdata->clock*2;
  640. hostdata->sync_clock /= 3;
  641. } else if(hostdata->clock > 37 && hostdata->clock <= 50) {
  642. /* sync divider 1, async divider 2 */
  643. DEBUG(("53c700: sync 1 async 2\n"));
  644. NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
  645. NCR_700_writeb(ASYNC_DIV_2_0 | dcntl_extra, host, DCNTL_REG);
  646. hostdata->sync_clock = hostdata->clock;
  647. } else if(hostdata->clock > 25 && hostdata->clock <=37) {
  648. /* sync divider 1, async divider 1.5 */
  649. DEBUG(("53c700: sync 1 async 1.5\n"));
  650. NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
  651. NCR_700_writeb(ASYNC_DIV_1_5 | dcntl_extra, host, DCNTL_REG);
  652. hostdata->sync_clock = hostdata->clock;
  653. } else {
  654. DEBUG(("53c700: sync 1 async 1\n"));
  655. NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
  656. NCR_700_writeb(ASYNC_DIV_1_0 | dcntl_extra, host, DCNTL_REG);
  657. /* sync divider 1, async divider 1 */
  658. hostdata->sync_clock = hostdata->clock;
  659. }
  660. /* Calculate the actual minimum period that can be supported
  661. * by our synchronous clock speed. See the 710 manual for
  662. * exact details of this calculation which is based on a
  663. * setting of the SXFER register */
  664. min_period = 1000*(4+min_xferp)/(4*hostdata->sync_clock);
  665. hostdata->min_period = NCR_700_MIN_PERIOD;
  666. if(min_period > NCR_700_MIN_PERIOD)
  667. hostdata->min_period = min_period;
  668. }
  669. STATIC void
  670. NCR_700_chip_reset(struct Scsi_Host *host)
  671. {
  672. struct NCR_700_Host_Parameters *hostdata =
  673. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  674. if(hostdata->chip710) {
  675. NCR_700_writeb(SOFTWARE_RESET_710, host, ISTAT_REG);
  676. udelay(100);
  677. NCR_700_writeb(0, host, ISTAT_REG);
  678. } else {
  679. NCR_700_writeb(SOFTWARE_RESET, host, DCNTL_REG);
  680. udelay(100);
  681. NCR_700_writeb(0, host, DCNTL_REG);
  682. }
  683. mdelay(1000);
  684. NCR_700_chip_setup(host);
  685. }
  686. /* The heart of the message processing engine is that the instruction
  687. * immediately after the INT is the normal case (and so must be CLEAR
  688. * ACK). If we want to do something else, we call that routine in
  689. * scripts and set temp to be the normal case + 8 (skipping the CLEAR
  690. * ACK) so that the routine returns correctly to resume its activity
  691. * */
  692. STATIC __u32
  693. process_extended_message(struct Scsi_Host *host,
  694. struct NCR_700_Host_Parameters *hostdata,
  695. struct scsi_cmnd *SCp, __u32 dsp, __u32 dsps)
  696. {
  697. __u32 resume_offset = dsp, temp = dsp + 8;
  698. __u8 pun = 0xff, lun = 0xff;
  699. if(SCp != NULL) {
  700. pun = SCp->device->id;
  701. lun = SCp->device->lun;
  702. }
  703. switch(hostdata->msgin[2]) {
  704. case A_SDTR_MSG:
  705. if(SCp != NULL && NCR_700_is_flag_set(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION)) {
  706. struct scsi_target *starget = SCp->device->sdev_target;
  707. __u8 period = hostdata->msgin[3];
  708. __u8 offset = hostdata->msgin[4];
  709. if(offset == 0 || period == 0) {
  710. offset = 0;
  711. period = 0;
  712. }
  713. spi_offset(starget) = offset;
  714. spi_period(starget) = period;
  715. if(NCR_700_is_flag_set(SCp->device, NCR_700_DEV_PRINT_SYNC_NEGOTIATION)) {
  716. spi_display_xfer_agreement(starget);
  717. NCR_700_clear_flag(SCp->device, NCR_700_DEV_PRINT_SYNC_NEGOTIATION);
  718. }
  719. NCR_700_set_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
  720. NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  721. NCR_700_writeb(NCR_700_get_SXFER(SCp->device),
  722. host, SXFER_REG);
  723. } else {
  724. /* SDTR message out of the blue, reject it */
  725. shost_printk(KERN_WARNING, host,
  726. "Unexpected SDTR msg\n");
  727. hostdata->msgout[0] = A_REJECT_MSG;
  728. dma_cache_sync(hostdata->dev, hostdata->msgout, 1, DMA_TO_DEVICE);
  729. script_patch_16(hostdata->dev, hostdata->script,
  730. MessageCount, 1);
  731. /* SendMsgOut returns, so set up the return
  732. * address */
  733. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  734. }
  735. break;
  736. case A_WDTR_MSG:
  737. printk(KERN_INFO "scsi%d: (%d:%d), Unsolicited WDTR after CMD, Rejecting\n",
  738. host->host_no, pun, lun);
  739. hostdata->msgout[0] = A_REJECT_MSG;
  740. dma_cache_sync(hostdata->dev, hostdata->msgout, 1, DMA_TO_DEVICE);
  741. script_patch_16(hostdata->dev, hostdata->script, MessageCount,
  742. 1);
  743. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  744. break;
  745. default:
  746. printk(KERN_INFO "scsi%d (%d:%d): Unexpected message %s: ",
  747. host->host_no, pun, lun,
  748. NCR_700_phase[(dsps & 0xf00) >> 8]);
  749. spi_print_msg(hostdata->msgin);
  750. printk("\n");
  751. /* just reject it */
  752. hostdata->msgout[0] = A_REJECT_MSG;
  753. dma_cache_sync(hostdata->dev, hostdata->msgout, 1, DMA_TO_DEVICE);
  754. script_patch_16(hostdata->dev, hostdata->script, MessageCount,
  755. 1);
  756. /* SendMsgOut returns, so set up the return
  757. * address */
  758. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  759. }
  760. NCR_700_writel(temp, host, TEMP_REG);
  761. return resume_offset;
  762. }
  763. STATIC __u32
  764. process_message(struct Scsi_Host *host, struct NCR_700_Host_Parameters *hostdata,
  765. struct scsi_cmnd *SCp, __u32 dsp, __u32 dsps)
  766. {
  767. /* work out where to return to */
  768. __u32 temp = dsp + 8, resume_offset = dsp;
  769. __u8 pun = 0xff, lun = 0xff;
  770. if(SCp != NULL) {
  771. pun = SCp->device->id;
  772. lun = SCp->device->lun;
  773. }
  774. #ifdef NCR_700_DEBUG
  775. printk("scsi%d (%d:%d): message %s: ", host->host_no, pun, lun,
  776. NCR_700_phase[(dsps & 0xf00) >> 8]);
  777. spi_print_msg(hostdata->msgin);
  778. printk("\n");
  779. #endif
  780. switch(hostdata->msgin[0]) {
  781. case A_EXTENDED_MSG:
  782. resume_offset = process_extended_message(host, hostdata, SCp,
  783. dsp, dsps);
  784. break;
  785. case A_REJECT_MSG:
  786. if(SCp != NULL && NCR_700_is_flag_set(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION)) {
  787. /* Rejected our sync negotiation attempt */
  788. spi_period(SCp->device->sdev_target) =
  789. spi_offset(SCp->device->sdev_target) = 0;
  790. NCR_700_set_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
  791. NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  792. } else if(SCp != NULL && NCR_700_get_tag_neg_state(SCp->device) == NCR_700_DURING_TAG_NEGOTIATION) {
  793. /* rejected our first simple tag message */
  794. scmd_printk(KERN_WARNING, SCp,
  795. "Rejected first tag queue attempt, turning off tag queueing\n");
  796. /* we're done negotiating */
  797. NCR_700_set_tag_neg_state(SCp->device, NCR_700_FINISHED_TAG_NEGOTIATION);
  798. hostdata->tag_negotiated &= ~(1<<scmd_id(SCp));
  799. SCp->device->tagged_supported = 0;
  800. scsi_deactivate_tcq(SCp->device, host->cmd_per_lun);
  801. } else {
  802. shost_printk(KERN_WARNING, host,
  803. "(%d:%d) Unexpected REJECT Message %s\n",
  804. pun, lun,
  805. NCR_700_phase[(dsps & 0xf00) >> 8]);
  806. /* however, just ignore it */
  807. }
  808. break;
  809. case A_PARITY_ERROR_MSG:
  810. printk(KERN_ERR "scsi%d (%d:%d) Parity Error!\n", host->host_no,
  811. pun, lun);
  812. NCR_700_internal_bus_reset(host);
  813. break;
  814. case A_SIMPLE_TAG_MSG:
  815. printk(KERN_INFO "scsi%d (%d:%d) SIMPLE TAG %d %s\n", host->host_no,
  816. pun, lun, hostdata->msgin[1],
  817. NCR_700_phase[(dsps & 0xf00) >> 8]);
  818. /* just ignore it */
  819. break;
  820. default:
  821. printk(KERN_INFO "scsi%d (%d:%d): Unexpected message %s: ",
  822. host->host_no, pun, lun,
  823. NCR_700_phase[(dsps & 0xf00) >> 8]);
  824. spi_print_msg(hostdata->msgin);
  825. printk("\n");
  826. /* just reject it */
  827. hostdata->msgout[0] = A_REJECT_MSG;
  828. dma_cache_sync(hostdata->dev, hostdata->msgout, 1, DMA_TO_DEVICE);
  829. script_patch_16(hostdata->dev, hostdata->script, MessageCount,
  830. 1);
  831. /* SendMsgOut returns, so set up the return
  832. * address */
  833. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  834. break;
  835. }
  836. NCR_700_writel(temp, host, TEMP_REG);
  837. /* set us up to receive another message */
  838. dma_cache_sync(hostdata->dev, hostdata->msgin, MSG_ARRAY_SIZE, DMA_FROM_DEVICE);
  839. return resume_offset;
  840. }
  841. STATIC __u32
  842. process_script_interrupt(__u32 dsps, __u32 dsp, struct scsi_cmnd *SCp,
  843. struct Scsi_Host *host,
  844. struct NCR_700_Host_Parameters *hostdata)
  845. {
  846. __u32 resume_offset = 0;
  847. __u8 pun = 0xff, lun=0xff;
  848. if(SCp != NULL) {
  849. pun = SCp->device->id;
  850. lun = SCp->device->lun;
  851. }
  852. if(dsps == A_GOOD_STATUS_AFTER_STATUS) {
  853. DEBUG((" COMMAND COMPLETE, status=%02x\n",
  854. hostdata->status[0]));
  855. /* OK, if TCQ still under negotiation, we now know it works */
  856. if (NCR_700_get_tag_neg_state(SCp->device) == NCR_700_DURING_TAG_NEGOTIATION)
  857. NCR_700_set_tag_neg_state(SCp->device,
  858. NCR_700_FINISHED_TAG_NEGOTIATION);
  859. /* check for contingent allegiance contitions */
  860. if(status_byte(hostdata->status[0]) == CHECK_CONDITION ||
  861. status_byte(hostdata->status[0]) == COMMAND_TERMINATED) {
  862. struct NCR_700_command_slot *slot =
  863. (struct NCR_700_command_slot *)SCp->host_scribble;
  864. if(slot->flags == NCR_700_FLAG_AUTOSENSE) {
  865. /* OOPS: bad device, returning another
  866. * contingent allegiance condition */
  867. scmd_printk(KERN_ERR, SCp,
  868. "broken device is looping in contingent allegiance: ignoring\n");
  869. NCR_700_scsi_done(hostdata, SCp, hostdata->status[0]);
  870. } else {
  871. char *cmnd =
  872. NCR_700_get_sense_cmnd(SCp->device);
  873. #ifdef NCR_DEBUG
  874. scsi_print_command(SCp);
  875. printk(" cmd %p has status %d, requesting sense\n",
  876. SCp, hostdata->status[0]);
  877. #endif
  878. /* we can destroy the command here
  879. * because the contingent allegiance
  880. * condition will cause a retry which
  881. * will re-copy the command from the
  882. * saved data_cmnd. We also unmap any
  883. * data associated with the command
  884. * here */
  885. NCR_700_unmap(hostdata, SCp, slot);
  886. dma_unmap_single(hostdata->dev, slot->pCmd,
  887. sizeof(SCp->cmnd),
  888. DMA_TO_DEVICE);
  889. cmnd[0] = REQUEST_SENSE;
  890. cmnd[1] = (SCp->device->lun & 0x7) << 5;
  891. cmnd[2] = 0;
  892. cmnd[3] = 0;
  893. cmnd[4] = sizeof(SCp->sense_buffer);
  894. cmnd[5] = 0;
  895. /* Here's a quiet hack: the
  896. * REQUEST_SENSE command is six bytes,
  897. * so store a flag indicating that
  898. * this was an internal sense request
  899. * and the original status at the end
  900. * of the command */
  901. cmnd[6] = NCR_700_INTERNAL_SENSE_MAGIC;
  902. cmnd[7] = hostdata->status[0];
  903. cmnd[8] = SCp->cmd_len;
  904. SCp->cmd_len = 6; /* command length for
  905. * REQUEST_SENSE */
  906. slot->pCmd = dma_map_single(hostdata->dev, cmnd, MAX_COMMAND_SIZE, DMA_TO_DEVICE);
  907. slot->dma_handle = dma_map_single(hostdata->dev, SCp->sense_buffer, sizeof(SCp->sense_buffer), DMA_FROM_DEVICE);
  908. slot->SG[0].ins = bS_to_host(SCRIPT_MOVE_DATA_IN | sizeof(SCp->sense_buffer));
  909. slot->SG[0].pAddr = bS_to_host(slot->dma_handle);
  910. slot->SG[1].ins = bS_to_host(SCRIPT_RETURN);
  911. slot->SG[1].pAddr = 0;
  912. slot->resume_offset = hostdata->pScript;
  913. dma_cache_sync(hostdata->dev, slot->SG, sizeof(slot->SG[0])*2, DMA_TO_DEVICE);
  914. dma_cache_sync(hostdata->dev, SCp->sense_buffer, sizeof(SCp->sense_buffer), DMA_FROM_DEVICE);
  915. /* queue the command for reissue */
  916. slot->state = NCR_700_SLOT_QUEUED;
  917. slot->flags = NCR_700_FLAG_AUTOSENSE;
  918. hostdata->state = NCR_700_HOST_FREE;
  919. hostdata->cmd = NULL;
  920. }
  921. } else {
  922. // Currently rely on the mid layer evaluation
  923. // of the tag queuing capability
  924. //
  925. //if(status_byte(hostdata->status[0]) == GOOD &&
  926. // SCp->cmnd[0] == INQUIRY && SCp->use_sg == 0) {
  927. // /* Piggy back the tag queueing support
  928. // * on this command */
  929. // dma_sync_single_for_cpu(hostdata->dev,
  930. // slot->dma_handle,
  931. // SCp->request_bufflen,
  932. // DMA_FROM_DEVICE);
  933. // if(((char *)SCp->request_buffer)[7] & 0x02) {
  934. // scmd_printk(KERN_INFO, SCp,
  935. // "Enabling Tag Command Queuing\n");
  936. // hostdata->tag_negotiated |= (1<<scmd_id(SCp));
  937. // NCR_700_set_flag(SCp->device, NCR_700_DEV_BEGIN_TAG_QUEUEING);
  938. // } else {
  939. // NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_TAG_QUEUEING);
  940. // hostdata->tag_negotiated &= ~(1<<scmd_id(SCp));
  941. // }
  942. //}
  943. NCR_700_scsi_done(hostdata, SCp, hostdata->status[0]);
  944. }
  945. } else if((dsps & 0xfffff0f0) == A_UNEXPECTED_PHASE) {
  946. __u8 i = (dsps & 0xf00) >> 8;
  947. scmd_printk(KERN_ERR, SCp, "UNEXPECTED PHASE %s (%s)\n",
  948. NCR_700_phase[i],
  949. sbcl_to_string(NCR_700_readb(host, SBCL_REG)));
  950. scmd_printk(KERN_ERR, SCp, " len = %d, cmd =",
  951. SCp->cmd_len);
  952. scsi_print_command(SCp);
  953. NCR_700_internal_bus_reset(host);
  954. } else if((dsps & 0xfffff000) == A_FATAL) {
  955. int i = (dsps & 0xfff);
  956. printk(KERN_ERR "scsi%d: (%d:%d) FATAL ERROR: %s\n",
  957. host->host_no, pun, lun, NCR_700_fatal_messages[i]);
  958. if(dsps == A_FATAL_ILLEGAL_MSG_LENGTH) {
  959. printk(KERN_ERR " msg begins %02x %02x\n",
  960. hostdata->msgin[0], hostdata->msgin[1]);
  961. }
  962. NCR_700_internal_bus_reset(host);
  963. } else if((dsps & 0xfffff0f0) == A_DISCONNECT) {
  964. #ifdef NCR_700_DEBUG
  965. __u8 i = (dsps & 0xf00) >> 8;
  966. printk("scsi%d: (%d:%d), DISCONNECTED (%d) %s\n",
  967. host->host_no, pun, lun,
  968. i, NCR_700_phase[i]);
  969. #endif
  970. save_for_reselection(hostdata, SCp, dsp);
  971. } else if(dsps == A_RESELECTION_IDENTIFIED) {
  972. __u8 lun;
  973. struct NCR_700_command_slot *slot;
  974. __u8 reselection_id = hostdata->reselection_id;
  975. struct scsi_device *SDp;
  976. lun = hostdata->msgin[0] & 0x1f;
  977. hostdata->reselection_id = 0xff;
  978. DEBUG(("scsi%d: (%d:%d) RESELECTED!\n",
  979. host->host_no, reselection_id, lun));
  980. /* clear the reselection indicator */
  981. SDp = __scsi_device_lookup(host, 0, reselection_id, lun);
  982. if(unlikely(SDp == NULL)) {
  983. printk(KERN_ERR "scsi%d: (%d:%d) HAS NO device\n",
  984. host->host_no, reselection_id, lun);
  985. BUG();
  986. }
  987. if(hostdata->msgin[1] == A_SIMPLE_TAG_MSG) {
  988. struct scsi_cmnd *SCp = scsi_find_tag(SDp, hostdata->msgin[2]);
  989. if(unlikely(SCp == NULL)) {
  990. printk(KERN_ERR "scsi%d: (%d:%d) no saved request for tag %d\n",
  991. host->host_no, reselection_id, lun, hostdata->msgin[2]);
  992. BUG();
  993. }
  994. slot = (struct NCR_700_command_slot *)SCp->host_scribble;
  995. DDEBUG(KERN_DEBUG, SDp,
  996. "reselection is tag %d, slot %p(%d)\n",
  997. hostdata->msgin[2], slot, slot->tag);
  998. } else {
  999. struct scsi_cmnd *SCp = scsi_find_tag(SDp, SCSI_NO_TAG);
  1000. if(unlikely(SCp == NULL)) {
  1001. sdev_printk(KERN_ERR, SDp,
  1002. "no saved request for untagged cmd\n");
  1003. BUG();
  1004. }
  1005. slot = (struct NCR_700_command_slot *)SCp->host_scribble;
  1006. }
  1007. if(slot == NULL) {
  1008. printk(KERN_ERR "scsi%d: (%d:%d) RESELECTED but no saved command (MSG = %02x %02x %02x)!!\n",
  1009. host->host_no, reselection_id, lun,
  1010. hostdata->msgin[0], hostdata->msgin[1],
  1011. hostdata->msgin[2]);
  1012. } else {
  1013. if(hostdata->state != NCR_700_HOST_BUSY)
  1014. printk(KERN_ERR "scsi%d: FATAL, host not busy during valid reselection!\n",
  1015. host->host_no);
  1016. resume_offset = slot->resume_offset;
  1017. hostdata->cmd = slot->cmnd;
  1018. /* re-patch for this command */
  1019. script_patch_32_abs(hostdata->dev, hostdata->script,
  1020. CommandAddress, slot->pCmd);
  1021. script_patch_16(hostdata->dev, hostdata->script,
  1022. CommandCount, slot->cmnd->cmd_len);
  1023. script_patch_32_abs(hostdata->dev, hostdata->script,
  1024. 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->dev, hostdata->msgin,
  1034. MSG_ARRAY_SIZE, DMA_FROM_DEVICE);
  1035. dma_cache_sync(hostdata->dev, 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(hostdata->dev, 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->dev, 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->request_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->dev, 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. slot->flags != NCR_700_FLAG_AUTOSENSE),
  1263. SCp->device->lun);
  1264. /* for INQUIRY or REQUEST_SENSE commands, we cannot be sure
  1265. * if the negotiated transfer parameters still hold, so
  1266. * always renegotiate them */
  1267. if(SCp->cmnd[0] == INQUIRY || SCp->cmnd[0] == REQUEST_SENSE ||
  1268. slot->flags == NCR_700_FLAG_AUTOSENSE) {
  1269. NCR_700_clear_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
  1270. }
  1271. /* REQUEST_SENSE is asking for contingent I_T_L(_Q) status.
  1272. * If a contingent allegiance condition exists, the device
  1273. * will refuse all tags, so send the request sense as untagged
  1274. * */
  1275. if((hostdata->tag_negotiated & (1<<scmd_id(SCp)))
  1276. && (slot->tag != SCSI_NO_TAG && SCp->cmnd[0] != REQUEST_SENSE &&
  1277. slot->flags != NCR_700_FLAG_AUTOSENSE)) {
  1278. count += scsi_populate_tag_msg(SCp, &hostdata->msgout[count]);
  1279. }
  1280. if(hostdata->fast &&
  1281. NCR_700_is_flag_clear(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC)) {
  1282. count += spi_populate_sync_msg(&hostdata->msgout[count],
  1283. spi_period(SCp->device->sdev_target),
  1284. spi_offset(SCp->device->sdev_target));
  1285. NCR_700_set_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  1286. }
  1287. script_patch_16(hostdata->dev, hostdata->script, MessageCount, count);
  1288. script_patch_ID(hostdata->dev, hostdata->script,
  1289. Device_ID, 1<<scmd_id(SCp));
  1290. script_patch_32_abs(hostdata->dev, hostdata->script, CommandAddress,
  1291. slot->pCmd);
  1292. script_patch_16(hostdata->dev, hostdata->script, CommandCount,
  1293. SCp->cmd_len);
  1294. /* finally plumb the beginning of the SG list into the script
  1295. * */
  1296. script_patch_32_abs(hostdata->dev, hostdata->script,
  1297. SGScriptStartAddress, to32bit(&slot->pSG[0].ins));
  1298. NCR_700_clear_fifo(SCp->device->host);
  1299. if(slot->resume_offset == 0)
  1300. slot->resume_offset = hostdata->pScript;
  1301. /* now perform all the writebacks and invalidates */
  1302. dma_cache_sync(hostdata->dev, hostdata->msgout, count, DMA_TO_DEVICE);
  1303. dma_cache_sync(hostdata->dev, hostdata->msgin, MSG_ARRAY_SIZE,
  1304. DMA_FROM_DEVICE);
  1305. dma_cache_sync(hostdata->dev, SCp->cmnd, SCp->cmd_len, DMA_TO_DEVICE);
  1306. dma_cache_sync(hostdata->dev, hostdata->status, 1, DMA_FROM_DEVICE);
  1307. /* set the synchronous period/offset */
  1308. NCR_700_writeb(NCR_700_get_SXFER(SCp->device),
  1309. SCp->device->host, SXFER_REG);
  1310. NCR_700_writel(slot->temp, SCp->device->host, TEMP_REG);
  1311. NCR_700_writel(slot->resume_offset, SCp->device->host, DSP_REG);
  1312. return 1;
  1313. }
  1314. irqreturn_t
  1315. NCR_700_intr(int irq, void *dev_id)
  1316. {
  1317. struct Scsi_Host *host = (struct Scsi_Host *)dev_id;
  1318. struct NCR_700_Host_Parameters *hostdata =
  1319. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  1320. __u8 istat;
  1321. __u32 resume_offset = 0;
  1322. __u8 pun = 0xff, lun = 0xff;
  1323. unsigned long flags;
  1324. int handled = 0;
  1325. /* Use the host lock to serialise acess to the 53c700
  1326. * hardware. Note: In future, we may need to take the queue
  1327. * lock to enter the done routines. When that happens, we
  1328. * need to ensure that for this driver, the host lock and the
  1329. * queue lock point to the same thing. */
  1330. spin_lock_irqsave(host->host_lock, flags);
  1331. if((istat = NCR_700_readb(host, ISTAT_REG))
  1332. & (SCSI_INT_PENDING | DMA_INT_PENDING)) {
  1333. __u32 dsps;
  1334. __u8 sstat0 = 0, dstat = 0;
  1335. __u32 dsp;
  1336. struct scsi_cmnd *SCp = hostdata->cmd;
  1337. enum NCR_700_Host_State state;
  1338. handled = 1;
  1339. state = hostdata->state;
  1340. SCp = hostdata->cmd;
  1341. if(istat & SCSI_INT_PENDING) {
  1342. udelay(10);
  1343. sstat0 = NCR_700_readb(host, SSTAT0_REG);
  1344. }
  1345. if(istat & DMA_INT_PENDING) {
  1346. udelay(10);
  1347. dstat = NCR_700_readb(host, DSTAT_REG);
  1348. }
  1349. dsps = NCR_700_readl(host, DSPS_REG);
  1350. dsp = NCR_700_readl(host, DSP_REG);
  1351. DEBUG(("scsi%d: istat %02x sstat0 %02x dstat %02x dsp %04x[%08x] dsps 0x%x\n",
  1352. host->host_no, istat, sstat0, dstat,
  1353. (dsp - (__u32)(hostdata->pScript))/4,
  1354. dsp, dsps));
  1355. if(SCp != NULL) {
  1356. pun = SCp->device->id;
  1357. lun = SCp->device->lun;
  1358. }
  1359. if(sstat0 & SCSI_RESET_DETECTED) {
  1360. struct scsi_device *SDp;
  1361. int i;
  1362. hostdata->state = NCR_700_HOST_BUSY;
  1363. printk(KERN_ERR "scsi%d: Bus Reset detected, executing command %p, slot %p, dsp %08x[%04x]\n",
  1364. host->host_no, SCp, SCp == NULL ? NULL : SCp->host_scribble, dsp, dsp - hostdata->pScript);
  1365. scsi_report_bus_reset(host, 0);
  1366. /* clear all the negotiated parameters */
  1367. __shost_for_each_device(SDp, host)
  1368. NCR_700_clear_flag(SDp, ~0);
  1369. /* clear all the slots and their pending commands */
  1370. for(i = 0; i < NCR_700_COMMAND_SLOTS_PER_HOST; i++) {
  1371. struct scsi_cmnd *SCp;
  1372. struct NCR_700_command_slot *slot =
  1373. &hostdata->slots[i];
  1374. if(slot->state == NCR_700_SLOT_FREE)
  1375. continue;
  1376. SCp = slot->cmnd;
  1377. printk(KERN_ERR " failing command because of reset, slot %p, cmnd %p\n",
  1378. slot, SCp);
  1379. free_slot(slot, hostdata);
  1380. SCp->host_scribble = NULL;
  1381. NCR_700_set_depth(SCp->device, 0);
  1382. /* NOTE: deadlock potential here: we
  1383. * rely on mid-layer guarantees that
  1384. * scsi_done won't try to issue the
  1385. * command again otherwise we'll
  1386. * deadlock on the
  1387. * hostdata->state_lock */
  1388. SCp->result = DID_RESET << 16;
  1389. SCp->scsi_done(SCp);
  1390. }
  1391. mdelay(25);
  1392. NCR_700_chip_setup(host);
  1393. hostdata->state = NCR_700_HOST_FREE;
  1394. hostdata->cmd = NULL;
  1395. /* signal back if this was an eh induced reset */
  1396. if(hostdata->eh_complete != NULL)
  1397. complete(hostdata->eh_complete);
  1398. goto out_unlock;
  1399. } else if(sstat0 & SELECTION_TIMEOUT) {
  1400. DEBUG(("scsi%d: (%d:%d) selection timeout\n",
  1401. host->host_no, pun, lun));
  1402. NCR_700_scsi_done(hostdata, SCp, DID_NO_CONNECT<<16);
  1403. } else if(sstat0 & PHASE_MISMATCH) {
  1404. struct NCR_700_command_slot *slot = (SCp == NULL) ? NULL :
  1405. (struct NCR_700_command_slot *)SCp->host_scribble;
  1406. if(dsp == Ent_SendMessage + 8 + hostdata->pScript) {
  1407. /* It wants to reply to some part of
  1408. * our message */
  1409. #ifdef NCR_700_DEBUG
  1410. __u32 temp = NCR_700_readl(host, TEMP_REG);
  1411. int count = (hostdata->script[Ent_SendMessage/4] & 0xffffff) - ((NCR_700_readl(host, DBC_REG) & 0xffffff) + NCR_700_data_residual(host));
  1412. 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)));
  1413. #endif
  1414. resume_offset = hostdata->pScript + Ent_SendMessagePhaseMismatch;
  1415. } else if(dsp >= to32bit(&slot->pSG[0].ins) &&
  1416. dsp <= to32bit(&slot->pSG[NCR_700_SG_SEGMENTS].ins)) {
  1417. int data_transfer = NCR_700_readl(host, DBC_REG) & 0xffffff;
  1418. int SGcount = (dsp - to32bit(&slot->pSG[0].ins))/sizeof(struct NCR_700_SG_List);
  1419. int residual = NCR_700_data_residual(host);
  1420. int i;
  1421. #ifdef NCR_700_DEBUG
  1422. __u32 naddr = NCR_700_readl(host, DNAD_REG);
  1423. printk("scsi%d: (%d:%d) Expected phase mismatch in slot->SG[%d], transferred 0x%x\n",
  1424. host->host_no, pun, lun,
  1425. SGcount, data_transfer);
  1426. scsi_print_command(SCp);
  1427. if(residual) {
  1428. printk("scsi%d: (%d:%d) Expected phase mismatch in slot->SG[%d], transferred 0x%x, residual %d\n",
  1429. host->host_no, pun, lun,
  1430. SGcount, data_transfer, residual);
  1431. }
  1432. #endif
  1433. data_transfer += residual;
  1434. if(data_transfer != 0) {
  1435. int count;
  1436. __u32 pAddr;
  1437. SGcount--;
  1438. count = (bS_to_cpu(slot->SG[SGcount].ins) & 0x00ffffff);
  1439. DEBUG(("DATA TRANSFER MISMATCH, count = %d, transferred %d\n", count, count-data_transfer));
  1440. slot->SG[SGcount].ins &= bS_to_host(0xff000000);
  1441. slot->SG[SGcount].ins |= bS_to_host(data_transfer);
  1442. pAddr = bS_to_cpu(slot->SG[SGcount].pAddr);
  1443. pAddr += (count - data_transfer);
  1444. #ifdef NCR_700_DEBUG
  1445. if(pAddr != naddr) {
  1446. 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);
  1447. }
  1448. #endif
  1449. slot->SG[SGcount].pAddr = bS_to_host(pAddr);
  1450. }
  1451. /* set the executed moves to nops */
  1452. for(i=0; i<SGcount; i++) {
  1453. slot->SG[i].ins = bS_to_host(SCRIPT_NOP);
  1454. slot->SG[i].pAddr = 0;
  1455. }
  1456. dma_cache_sync(hostdata->dev, slot->SG, sizeof(slot->SG), DMA_TO_DEVICE);
  1457. /* and pretend we disconnected after
  1458. * the command phase */
  1459. resume_offset = hostdata->pScript + Ent_MsgInDuringData;
  1460. /* make sure all the data is flushed */
  1461. NCR_700_flush_fifo(host);
  1462. } else {
  1463. __u8 sbcl = NCR_700_readb(host, SBCL_REG);
  1464. printk(KERN_ERR "scsi%d: (%d:%d) phase mismatch at %04x, phase %s\n",
  1465. host->host_no, pun, lun, dsp - hostdata->pScript, sbcl_to_string(sbcl));
  1466. NCR_700_internal_bus_reset(host);
  1467. }
  1468. } else if(sstat0 & SCSI_GROSS_ERROR) {
  1469. printk(KERN_ERR "scsi%d: (%d:%d) GROSS ERROR\n",
  1470. host->host_no, pun, lun);
  1471. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1472. } else if(sstat0 & PARITY_ERROR) {
  1473. printk(KERN_ERR "scsi%d: (%d:%d) PARITY ERROR\n",
  1474. host->host_no, pun, lun);
  1475. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1476. } else if(dstat & SCRIPT_INT_RECEIVED) {
  1477. DEBUG(("scsi%d: (%d:%d) ====>SCRIPT INTERRUPT<====\n",
  1478. host->host_no, pun, lun));
  1479. resume_offset = process_script_interrupt(dsps, dsp, SCp, host, hostdata);
  1480. } else if(dstat & (ILGL_INST_DETECTED)) {
  1481. printk(KERN_ERR "scsi%d: (%d:%d) Illegal Instruction detected at 0x%08x[0x%x]!!!\n"
  1482. " Please email James.Bottomley@HansenPartnership.com with the details\n",
  1483. host->host_no, pun, lun,
  1484. dsp, dsp - hostdata->pScript);
  1485. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1486. } else if(dstat & (WATCH_DOG_INTERRUPT|ABORTED)) {
  1487. printk(KERN_ERR "scsi%d: (%d:%d) serious DMA problem, dstat=%02x\n",
  1488. host->host_no, pun, lun, dstat);
  1489. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1490. }
  1491. /* NOTE: selection interrupt processing MUST occur
  1492. * after script interrupt processing to correctly cope
  1493. * with the case where we process a disconnect and
  1494. * then get reselected before we process the
  1495. * disconnection */
  1496. if(sstat0 & SELECTED) {
  1497. /* FIXME: It currently takes at least FOUR
  1498. * interrupts to complete a command that
  1499. * disconnects: one for the disconnect, one
  1500. * for the reselection, one to get the
  1501. * reselection data and one to complete the
  1502. * command. If we guess the reselected
  1503. * command here and prepare it, we only need
  1504. * to get a reselection data interrupt if we
  1505. * guessed wrongly. Since the interrupt
  1506. * overhead is much greater than the command
  1507. * setup, this would be an efficient
  1508. * optimisation particularly as we probably
  1509. * only have one outstanding command on a
  1510. * target most of the time */
  1511. resume_offset = process_selection(host, dsp);
  1512. }
  1513. }
  1514. if(resume_offset) {
  1515. if(hostdata->state != NCR_700_HOST_BUSY) {
  1516. printk(KERN_ERR "scsi%d: Driver error: resume at 0x%08x [0x%04x] with non busy host!\n",
  1517. host->host_no, resume_offset, resume_offset - hostdata->pScript);
  1518. hostdata->state = NCR_700_HOST_BUSY;
  1519. }
  1520. DEBUG(("Attempting to resume at %x\n", resume_offset));
  1521. NCR_700_clear_fifo(host);
  1522. NCR_700_writel(resume_offset, host, DSP_REG);
  1523. }
  1524. /* There is probably a technical no-no about this: If we're a
  1525. * shared interrupt and we got this interrupt because the
  1526. * other device needs servicing not us, we're still going to
  1527. * check our queued commands here---of course, there shouldn't
  1528. * be any outstanding.... */
  1529. if(hostdata->state == NCR_700_HOST_FREE) {
  1530. int i;
  1531. for(i = 0; i < NCR_700_COMMAND_SLOTS_PER_HOST; i++) {
  1532. /* fairness: always run the queue from the last
  1533. * position we left off */
  1534. int j = (i + hostdata->saved_slot_position)
  1535. % NCR_700_COMMAND_SLOTS_PER_HOST;
  1536. if(hostdata->slots[j].state != NCR_700_SLOT_QUEUED)
  1537. continue;
  1538. if(NCR_700_start_command(hostdata->slots[j].cmnd)) {
  1539. DEBUG(("scsi%d: Issuing saved command slot %p, cmd %p\t\n",
  1540. host->host_no, &hostdata->slots[j],
  1541. hostdata->slots[j].cmnd));
  1542. hostdata->saved_slot_position = j + 1;
  1543. }
  1544. break;
  1545. }
  1546. }
  1547. out_unlock:
  1548. spin_unlock_irqrestore(host->host_lock, flags);
  1549. return IRQ_RETVAL(handled);
  1550. }
  1551. STATIC int
  1552. NCR_700_queuecommand(struct scsi_cmnd *SCp, void (*done)(struct scsi_cmnd *))
  1553. {
  1554. struct NCR_700_Host_Parameters *hostdata =
  1555. (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
  1556. __u32 move_ins;
  1557. enum dma_data_direction direction;
  1558. struct NCR_700_command_slot *slot;
  1559. if(hostdata->command_slot_count >= NCR_700_COMMAND_SLOTS_PER_HOST) {
  1560. /* We're over our allocation, this should never happen
  1561. * since we report the max allocation to the mid layer */
  1562. printk(KERN_WARNING "scsi%d: Command depth has gone over queue depth\n", SCp->device->host->host_no);
  1563. return 1;
  1564. }
  1565. /* check for untagged commands. We cannot have any outstanding
  1566. * commands if we accept them. Commands could be untagged because:
  1567. *
  1568. * - The tag negotiated bitmap is clear
  1569. * - The blk layer sent and untagged command
  1570. */
  1571. if(NCR_700_get_depth(SCp->device) != 0
  1572. && (!(hostdata->tag_negotiated & (1<<scmd_id(SCp)))
  1573. || !blk_rq_tagged(SCp->request))) {
  1574. CDEBUG(KERN_ERR, SCp, "has non zero depth %d\n",
  1575. NCR_700_get_depth(SCp->device));
  1576. return SCSI_MLQUEUE_DEVICE_BUSY;
  1577. }
  1578. if(NCR_700_get_depth(SCp->device) >= SCp->device->queue_depth) {
  1579. CDEBUG(KERN_ERR, SCp, "has max tag depth %d\n",
  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<<scmd_id(SCp))) == 0
  1599. && NCR_700_get_tag_neg_state(SCp->device) == NCR_700_START_TAG_NEGOTIATION) {
  1600. scmd_printk(KERN_ERR, SCp, "Enabling Tag Command Queuing\n");
  1601. hostdata->tag_negotiated |= (1<<scmd_id(SCp));
  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<<scmd_id(SCp)))) {
  1612. scmd_printk(KERN_INFO, SCp, "Disabling Tag Command Queuing\n");
  1613. hostdata->tag_negotiated &= ~(1<<scmd_id(SCp));
  1614. }
  1615. if((hostdata->tag_negotiated &(1<<scmd_id(SCp)))
  1616. && scsi_get_tag_type(SCp->device)) {
  1617. slot->tag = SCp->request->tag;
  1618. CDEBUG(KERN_DEBUG, SCp, "sending out tag %d, slot %p\n",
  1619. slot->tag, slot);
  1620. } else {
  1621. slot->tag = SCSI_NO_TAG;
  1622. /* must populate current_cmnd for scsi_find_tag to work */
  1623. SCp->device->current_cmnd = SCp;
  1624. }
  1625. /* sanity check: some of the commands generated by the mid-layer
  1626. * have an eccentric idea of their sc_data_direction */
  1627. if(!SCp->use_sg && !SCp->request_bufflen
  1628. && SCp->sc_data_direction != DMA_NONE) {
  1629. #ifdef NCR_700_DEBUG
  1630. printk("53c700: Command");
  1631. scsi_print_command(SCp);
  1632. printk("Has wrong data direction %d\n", SCp->sc_data_direction);
  1633. #endif
  1634. SCp->sc_data_direction = DMA_NONE;
  1635. }
  1636. switch (SCp->cmnd[0]) {
  1637. case REQUEST_SENSE:
  1638. /* clear the internal sense magic */
  1639. SCp->cmnd[6] = 0;
  1640. /* fall through */
  1641. default:
  1642. /* OK, get it from the command */
  1643. switch(SCp->sc_data_direction) {
  1644. case DMA_BIDIRECTIONAL:
  1645. default:
  1646. printk(KERN_ERR "53c700: Unknown command for data direction ");
  1647. scsi_print_command(SCp);
  1648. move_ins = 0;
  1649. break;
  1650. case DMA_NONE:
  1651. move_ins = 0;
  1652. break;
  1653. case DMA_FROM_DEVICE:
  1654. move_ins = SCRIPT_MOVE_DATA_IN;
  1655. break;
  1656. case DMA_TO_DEVICE:
  1657. move_ins = SCRIPT_MOVE_DATA_OUT;
  1658. break;
  1659. }
  1660. }
  1661. /* now build the scatter gather list */
  1662. direction = SCp->sc_data_direction;
  1663. if(move_ins != 0) {
  1664. int i;
  1665. int sg_count;
  1666. dma_addr_t vPtr = 0;
  1667. __u32 count = 0;
  1668. if(SCp->use_sg) {
  1669. sg_count = dma_map_sg(hostdata->dev,
  1670. SCp->request_buffer, SCp->use_sg,
  1671. 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->request_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(hostdata->dev, 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. scmd_printk(KERN_INFO, SCp,
  1710. "New error handler wants to abort command\n\t");
  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_ONSTACK(complete);
  1736. struct NCR_700_Host_Parameters *hostdata =
  1737. (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
  1738. scmd_printk(KERN_INFO, SCp,
  1739. "New error handler wants BUS reset, cmd %p\n\t", 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. spin_lock_irq(SCp->device->host->host_lock);
  1745. while (hostdata->eh_complete != NULL) {
  1746. spin_unlock_irq(SCp->device->host->host_lock);
  1747. msleep_interruptible(100);
  1748. spin_lock_irq(SCp->device->host->host_lock);
  1749. }
  1750. hostdata->eh_complete = &complete;
  1751. NCR_700_internal_bus_reset(SCp->device->host);
  1752. spin_unlock_irq(SCp->device->host->host_lock);
  1753. wait_for_completion(&complete);
  1754. spin_lock_irq(SCp->device->host->host_lock);
  1755. hostdata->eh_complete = NULL;
  1756. /* Revalidate the transport parameters of the failing device */
  1757. if(hostdata->fast)
  1758. spi_schedule_dv_device(SCp->device);
  1759. spin_unlock_irq(SCp->device->host->host_lock);
  1760. return SUCCESS;
  1761. }
  1762. STATIC int
  1763. NCR_700_host_reset(struct scsi_cmnd * SCp)
  1764. {
  1765. scmd_printk(KERN_INFO, SCp, "New error handler wants HOST reset\n\t");
  1766. scsi_print_command(SCp);
  1767. spin_lock_irq(SCp->device->host->host_lock);
  1768. NCR_700_internal_bus_reset(SCp->device->host);
  1769. NCR_700_chip_reset(SCp->device->host);
  1770. spin_unlock_irq(SCp->device->host->host_lock);
  1771. return SUCCESS;
  1772. }
  1773. STATIC void
  1774. NCR_700_set_period(struct scsi_target *STp, int period)
  1775. {
  1776. struct Scsi_Host *SHp = dev_to_shost(STp->dev.parent);
  1777. struct NCR_700_Host_Parameters *hostdata =
  1778. (struct NCR_700_Host_Parameters *)SHp->hostdata[0];
  1779. if(!hostdata->fast)
  1780. return;
  1781. if(period < hostdata->min_period)
  1782. period = hostdata->min_period;
  1783. spi_period(STp) = period;
  1784. spi_flags(STp) &= ~(NCR_700_DEV_NEGOTIATED_SYNC |
  1785. NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  1786. spi_flags(STp) |= NCR_700_DEV_PRINT_SYNC_NEGOTIATION;
  1787. }
  1788. STATIC void
  1789. NCR_700_set_offset(struct scsi_target *STp, int offset)
  1790. {
  1791. struct Scsi_Host *SHp = dev_to_shost(STp->dev.parent);
  1792. struct NCR_700_Host_Parameters *hostdata =
  1793. (struct NCR_700_Host_Parameters *)SHp->hostdata[0];
  1794. int max_offset = hostdata->chip710
  1795. ? NCR_710_MAX_OFFSET : NCR_700_MAX_OFFSET;
  1796. if(!hostdata->fast)
  1797. return;
  1798. if(offset > max_offset)
  1799. offset = max_offset;
  1800. /* if we're currently async, make sure the period is reasonable */
  1801. if(spi_offset(STp) == 0 && (spi_period(STp) < hostdata->min_period ||
  1802. spi_period(STp) > 0xff))
  1803. spi_period(STp) = hostdata->min_period;
  1804. spi_offset(STp) = offset;
  1805. spi_flags(STp) &= ~(NCR_700_DEV_NEGOTIATED_SYNC |
  1806. NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  1807. spi_flags(STp) |= NCR_700_DEV_PRINT_SYNC_NEGOTIATION;
  1808. }
  1809. STATIC int
  1810. NCR_700_slave_alloc(struct scsi_device *SDp)
  1811. {
  1812. SDp->hostdata = kzalloc(sizeof(struct NCR_700_Device_Parameters),
  1813. GFP_KERNEL);
  1814. if (!SDp->hostdata)
  1815. return -ENOMEM;
  1816. return 0;
  1817. }
  1818. STATIC int
  1819. NCR_700_slave_configure(struct scsi_device *SDp)
  1820. {
  1821. struct NCR_700_Host_Parameters *hostdata =
  1822. (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
  1823. /* to do here: allocate memory; build a queue_full list */
  1824. if(SDp->tagged_supported) {
  1825. scsi_set_tag_type(SDp, MSG_ORDERED_TAG);
  1826. scsi_activate_tcq(SDp, NCR_700_DEFAULT_TAGS);
  1827. NCR_700_set_tag_neg_state(SDp, NCR_700_START_TAG_NEGOTIATION);
  1828. } else {
  1829. /* initialise to default depth */
  1830. scsi_adjust_queue_depth(SDp, 0, SDp->host->cmd_per_lun);
  1831. }
  1832. if(hostdata->fast) {
  1833. /* Find the correct offset and period via domain validation */
  1834. if (!spi_initial_dv(SDp->sdev_target))
  1835. spi_dv_device(SDp);
  1836. } else {
  1837. spi_offset(SDp->sdev_target) = 0;
  1838. spi_period(SDp->sdev_target) = 0;
  1839. }
  1840. return 0;
  1841. }
  1842. STATIC void
  1843. NCR_700_slave_destroy(struct scsi_device *SDp)
  1844. {
  1845. kfree(SDp->hostdata);
  1846. SDp->hostdata = NULL;
  1847. }
  1848. static int
  1849. NCR_700_change_queue_depth(struct scsi_device *SDp, int depth)
  1850. {
  1851. if (depth > NCR_700_MAX_TAGS)
  1852. depth = NCR_700_MAX_TAGS;
  1853. scsi_adjust_queue_depth(SDp, scsi_get_tag_type(SDp), depth);
  1854. return depth;
  1855. }
  1856. static int NCR_700_change_queue_type(struct scsi_device *SDp, int tag_type)
  1857. {
  1858. int change_tag = ((tag_type ==0 && scsi_get_tag_type(SDp) != 0)
  1859. || (tag_type != 0 && scsi_get_tag_type(SDp) == 0));
  1860. struct NCR_700_Host_Parameters *hostdata =
  1861. (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
  1862. scsi_set_tag_type(SDp, tag_type);
  1863. /* We have a global (per target) flag to track whether TCQ is
  1864. * enabled, so we'll be turning it off for the entire target here.
  1865. * our tag algorithm will fail if we mix tagged and untagged commands,
  1866. * so quiesce the device before doing this */
  1867. if (change_tag)
  1868. scsi_target_quiesce(SDp->sdev_target);
  1869. if (!tag_type) {
  1870. /* shift back to the default unqueued number of commands
  1871. * (the user can still raise this) */
  1872. scsi_deactivate_tcq(SDp, SDp->host->cmd_per_lun);
  1873. hostdata->tag_negotiated &= ~(1 << sdev_id(SDp));
  1874. } else {
  1875. /* Here, we cleared the negotiation flag above, so this
  1876. * will force the driver to renegotiate */
  1877. scsi_activate_tcq(SDp, SDp->queue_depth);
  1878. if (change_tag)
  1879. NCR_700_set_tag_neg_state(SDp, NCR_700_START_TAG_NEGOTIATION);
  1880. }
  1881. if (change_tag)
  1882. scsi_target_resume(SDp->sdev_target);
  1883. return tag_type;
  1884. }
  1885. static ssize_t
  1886. NCR_700_show_active_tags(struct device *dev, struct device_attribute *attr, char *buf)
  1887. {
  1888. struct scsi_device *SDp = to_scsi_device(dev);
  1889. return snprintf(buf, 20, "%d\n", NCR_700_get_depth(SDp));
  1890. }
  1891. static struct device_attribute NCR_700_active_tags_attr = {
  1892. .attr = {
  1893. .name = "active_tags",
  1894. .mode = S_IRUGO,
  1895. },
  1896. .show = NCR_700_show_active_tags,
  1897. };
  1898. STATIC struct device_attribute *NCR_700_dev_attrs[] = {
  1899. &NCR_700_active_tags_attr,
  1900. NULL,
  1901. };
  1902. EXPORT_SYMBOL(NCR_700_detect);
  1903. EXPORT_SYMBOL(NCR_700_release);
  1904. EXPORT_SYMBOL(NCR_700_intr);
  1905. static struct spi_function_template NCR_700_transport_functions = {
  1906. .set_period = NCR_700_set_period,
  1907. .show_period = 1,
  1908. .set_offset = NCR_700_set_offset,
  1909. .show_offset = 1,
  1910. };
  1911. static int __init NCR_700_init(void)
  1912. {
  1913. NCR_700_transport_template = spi_attach_transport(&NCR_700_transport_functions);
  1914. if(!NCR_700_transport_template)
  1915. return -ENODEV;
  1916. return 0;
  1917. }
  1918. static void __exit NCR_700_exit(void)
  1919. {
  1920. spi_release_transport(NCR_700_transport_template);
  1921. }
  1922. module_init(NCR_700_init);
  1923. module_exit(NCR_700_exit);