53c700.c 67 KB

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