53c700.c 67 KB

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