nsp_cs.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  1. /*======================================================================
  2. NinjaSCSI-3 / NinjaSCSI-32Bi PCMCIA SCSI host adapter card driver
  3. By: YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>
  4. Ver.2.8 Support 32bit MMIO mode
  5. Support Synchronous Data Transfer Request (SDTR) mode
  6. Ver.2.0 Support 32bit PIO mode
  7. Ver.1.1.2 Fix for scatter list buffer exceeds
  8. Ver.1.1 Support scatter list
  9. Ver.0.1 Initial version
  10. This software may be used and distributed according to the terms of
  11. the GNU General Public License.
  12. ======================================================================*/
  13. /***********************************************************************
  14. This driver is for these PCcards.
  15. I-O DATA PCSC-F (Workbit NinjaSCSI-3)
  16. "WBT", "NinjaSCSI-3", "R1.0"
  17. I-O DATA CBSC-II (Workbit NinjaSCSI-32Bi in 16bit mode)
  18. "IO DATA", "CBSC16 ", "1"
  19. ***********************************************************************/
  20. /* $Id: nsp_cs.c,v 1.23 2003/08/18 11:09:19 elca Exp $ */
  21. #include <linux/version.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/sched.h>
  26. #include <linux/slab.h>
  27. #include <linux/string.h>
  28. #include <linux/timer.h>
  29. #include <linux/ioport.h>
  30. #include <linux/delay.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/major.h>
  33. #include <linux/blkdev.h>
  34. #include <linux/stat.h>
  35. #include <asm/io.h>
  36. #include <asm/irq.h>
  37. #include <../drivers/scsi/scsi.h>
  38. #include <scsi/scsi_host.h>
  39. #include <scsi/scsi.h>
  40. #include <scsi/scsi_ioctl.h>
  41. #include <pcmcia/cs_types.h>
  42. #include <pcmcia/cs.h>
  43. #include <pcmcia/cistpl.h>
  44. #include <pcmcia/cisreg.h>
  45. #include <pcmcia/ds.h>
  46. #include "nsp_cs.h"
  47. MODULE_AUTHOR("YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>");
  48. MODULE_DESCRIPTION("WorkBit NinjaSCSI-3 / NinjaSCSI-32Bi(16bit) PCMCIA SCSI host adapter module $Revision: 1.23 $");
  49. MODULE_SUPPORTED_DEVICE("sd,sr,sg,st");
  50. #ifdef MODULE_LICENSE
  51. MODULE_LICENSE("GPL");
  52. #endif
  53. #include "nsp_io.h"
  54. /*====================================================================*/
  55. /* Parameters that can be set with 'insmod' */
  56. static int nsp_burst_mode = BURST_MEM32;
  57. module_param(nsp_burst_mode, int, 0);
  58. MODULE_PARM_DESC(nsp_burst_mode, "Burst transfer mode (0=io8, 1=io32, 2=mem32(default))");
  59. /* Release IO ports after configuration? */
  60. static int free_ports = 0;
  61. module_param(free_ports, bool, 0);
  62. MODULE_PARM_DESC(free_ports, "Release IO ports after configuration? (default: 0 (=no))");
  63. static struct scsi_host_template nsp_driver_template = {
  64. .proc_name = "nsp_cs",
  65. .proc_info = nsp_proc_info,
  66. .name = "WorkBit NinjaSCSI-3/32Bi(16bit)",
  67. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
  68. .detect = nsp_detect_old,
  69. .release = nsp_release_old,
  70. #endif
  71. .info = nsp_info,
  72. .queuecommand = nsp_queuecommand,
  73. /* .eh_abort_handler = nsp_eh_abort,*/
  74. .eh_bus_reset_handler = nsp_eh_bus_reset,
  75. .eh_host_reset_handler = nsp_eh_host_reset,
  76. .can_queue = 1,
  77. .this_id = NSP_INITIATOR_ID,
  78. .sg_tablesize = SG_ALL,
  79. .cmd_per_lun = 1,
  80. .use_clustering = DISABLE_CLUSTERING,
  81. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,2))
  82. .use_new_eh_code = 1,
  83. #endif
  84. };
  85. static nsp_hw_data nsp_data_base; /* attach <-> detect glue */
  86. /*
  87. * debug, error print
  88. */
  89. #ifndef NSP_DEBUG
  90. # define NSP_DEBUG_MASK 0x000000
  91. # define nsp_msg(type, args...) nsp_cs_message("", 0, (type), args)
  92. # define nsp_dbg(mask, args...) /* */
  93. #else
  94. # define NSP_DEBUG_MASK 0xffffff
  95. # define nsp_msg(type, args...) \
  96. nsp_cs_message (__FUNCTION__, __LINE__, (type), args)
  97. # define nsp_dbg(mask, args...) \
  98. nsp_cs_dmessage(__FUNCTION__, __LINE__, (mask), args)
  99. #endif
  100. #define NSP_DEBUG_QUEUECOMMAND BIT(0)
  101. #define NSP_DEBUG_REGISTER BIT(1)
  102. #define NSP_DEBUG_AUTOSCSI BIT(2)
  103. #define NSP_DEBUG_INTR BIT(3)
  104. #define NSP_DEBUG_SGLIST BIT(4)
  105. #define NSP_DEBUG_BUSFREE BIT(5)
  106. #define NSP_DEBUG_CDB_CONTENTS BIT(6)
  107. #define NSP_DEBUG_RESELECTION BIT(7)
  108. #define NSP_DEBUG_MSGINOCCUR BIT(8)
  109. #define NSP_DEBUG_EEPROM BIT(9)
  110. #define NSP_DEBUG_MSGOUTOCCUR BIT(10)
  111. #define NSP_DEBUG_BUSRESET BIT(11)
  112. #define NSP_DEBUG_RESTART BIT(12)
  113. #define NSP_DEBUG_SYNC BIT(13)
  114. #define NSP_DEBUG_WAIT BIT(14)
  115. #define NSP_DEBUG_TARGETFLAG BIT(15)
  116. #define NSP_DEBUG_PROC BIT(16)
  117. #define NSP_DEBUG_INIT BIT(17)
  118. #define NSP_DEBUG_DATA_IO BIT(18)
  119. #define NSP_SPECIAL_PRINT_REGISTER BIT(20)
  120. #define NSP_DEBUG_BUF_LEN 150
  121. static void nsp_cs_message(const char *func, int line, char *type, char *fmt, ...)
  122. {
  123. va_list args;
  124. char buf[NSP_DEBUG_BUF_LEN];
  125. va_start(args, fmt);
  126. vsnprintf(buf, sizeof(buf), fmt, args);
  127. va_end(args);
  128. #ifndef NSP_DEBUG
  129. printk("%snsp_cs: %s\n", type, buf);
  130. #else
  131. printk("%snsp_cs: %s (%d): %s\n", type, func, line, buf);
  132. #endif
  133. }
  134. #ifdef NSP_DEBUG
  135. static void nsp_cs_dmessage(const char *func, int line, int mask, char *fmt, ...)
  136. {
  137. va_list args;
  138. char buf[NSP_DEBUG_BUF_LEN];
  139. va_start(args, fmt);
  140. vsnprintf(buf, sizeof(buf), fmt, args);
  141. va_end(args);
  142. if (mask & NSP_DEBUG_MASK) {
  143. printk("nsp_cs-debug: 0x%x %s (%d): %s\n", mask, func, line, buf);
  144. }
  145. }
  146. #endif
  147. /***********************************************************/
  148. /*====================================================
  149. * Clenaup parameters and call done() functions.
  150. * You must be set SCpnt->result before call this function.
  151. */
  152. static void nsp_scsi_done(Scsi_Cmnd *SCpnt)
  153. {
  154. nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
  155. data->CurrentSC = NULL;
  156. SCpnt->scsi_done(SCpnt);
  157. }
  158. static int nsp_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
  159. {
  160. #ifdef NSP_DEBUG
  161. /*unsigned int host_id = SCpnt->device->host->this_id;*/
  162. /*unsigned int base = SCpnt->device->host->io_port;*/
  163. unsigned char target = scmd_id(SCpnt);
  164. #endif
  165. nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
  166. nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "SCpnt=0x%p target=%d lun=%d buff=0x%p bufflen=%d use_sg=%d",
  167. SCpnt, target, SCpnt->device->lun, SCpnt->request_buffer, SCpnt->request_bufflen, SCpnt->use_sg);
  168. //nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "before CurrentSC=0x%p", data->CurrentSC);
  169. SCpnt->scsi_done = done;
  170. if (data->CurrentSC != NULL) {
  171. nsp_msg(KERN_DEBUG, "CurrentSC!=NULL this can't be happen");
  172. SCpnt->result = DID_BAD_TARGET << 16;
  173. nsp_scsi_done(SCpnt);
  174. return 0;
  175. }
  176. #if 0
  177. /* XXX: pcmcia-cs generates SCSI command with "scsi_info" utility.
  178. This makes kernel crash when suspending... */
  179. if (data->ScsiInfo->stop != 0) {
  180. nsp_msg(KERN_INFO, "suspending device. reject command.");
  181. SCpnt->result = DID_BAD_TARGET << 16;
  182. nsp_scsi_done(SCpnt);
  183. return SCSI_MLQUEUE_HOST_BUSY;
  184. }
  185. #endif
  186. show_command(SCpnt);
  187. data->CurrentSC = SCpnt;
  188. SCpnt->SCp.Status = CHECK_CONDITION;
  189. SCpnt->SCp.Message = 0;
  190. SCpnt->SCp.have_data_in = IO_UNKNOWN;
  191. SCpnt->SCp.sent_command = 0;
  192. SCpnt->SCp.phase = PH_UNDETERMINED;
  193. SCpnt->resid = SCpnt->request_bufflen;
  194. /* setup scratch area
  195. SCp.ptr : buffer pointer
  196. SCp.this_residual : buffer length
  197. SCp.buffer : next buffer
  198. SCp.buffers_residual : left buffers in list
  199. SCp.phase : current state of the command */
  200. if (SCpnt->use_sg) {
  201. SCpnt->SCp.buffer = (struct scatterlist *) SCpnt->request_buffer;
  202. SCpnt->SCp.ptr = BUFFER_ADDR;
  203. SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
  204. SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
  205. } else {
  206. SCpnt->SCp.ptr = (char *) SCpnt->request_buffer;
  207. SCpnt->SCp.this_residual = SCpnt->request_bufflen;
  208. SCpnt->SCp.buffer = NULL;
  209. SCpnt->SCp.buffers_residual = 0;
  210. }
  211. if (nsphw_start_selection(SCpnt) == FALSE) {
  212. nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "selection fail");
  213. SCpnt->result = DID_BUS_BUSY << 16;
  214. nsp_scsi_done(SCpnt);
  215. return 0;
  216. }
  217. //nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "out");
  218. #ifdef NSP_DEBUG
  219. data->CmdId++;
  220. #endif
  221. return 0;
  222. }
  223. /*
  224. * setup PIO FIFO transfer mode and enable/disable to data out
  225. */
  226. static void nsp_setup_fifo(nsp_hw_data *data, int enabled)
  227. {
  228. unsigned int base = data->BaseAddress;
  229. unsigned char transfer_mode_reg;
  230. //nsp_dbg(NSP_DEBUG_DATA_IO, "enabled=%d", enabled);
  231. if (enabled != FALSE) {
  232. transfer_mode_reg = TRANSFER_GO | BRAIND;
  233. } else {
  234. transfer_mode_reg = 0;
  235. }
  236. transfer_mode_reg |= data->TransferMode;
  237. nsp_index_write(base, TRANSFERMODE, transfer_mode_reg);
  238. }
  239. static void nsphw_init_sync(nsp_hw_data *data)
  240. {
  241. sync_data tmp_sync = { .SyncNegotiation = SYNC_NOT_YET,
  242. .SyncPeriod = 0,
  243. .SyncOffset = 0
  244. };
  245. int i;
  246. /* setup sync data */
  247. for ( i = 0; i < ARRAY_SIZE(data->Sync); i++ ) {
  248. data->Sync[i] = tmp_sync;
  249. }
  250. }
  251. /*
  252. * Initialize Ninja hardware
  253. */
  254. static int nsphw_init(nsp_hw_data *data)
  255. {
  256. unsigned int base = data->BaseAddress;
  257. nsp_dbg(NSP_DEBUG_INIT, "in base=0x%x", base);
  258. data->ScsiClockDiv = CLOCK_40M | FAST_20;
  259. data->CurrentSC = NULL;
  260. data->FifoCount = 0;
  261. data->TransferMode = MODE_IO8;
  262. nsphw_init_sync(data);
  263. /* block all interrupts */
  264. nsp_write(base, IRQCONTROL, IRQCONTROL_ALLMASK);
  265. /* setup SCSI interface */
  266. nsp_write(base, IFSELECT, IF_IFSEL);
  267. nsp_index_write(base, SCSIIRQMODE, 0);
  268. nsp_index_write(base, TRANSFERMODE, MODE_IO8);
  269. nsp_index_write(base, CLOCKDIV, data->ScsiClockDiv);
  270. nsp_index_write(base, PARITYCTRL, 0);
  271. nsp_index_write(base, POINTERCLR, POINTER_CLEAR |
  272. ACK_COUNTER_CLEAR |
  273. REQ_COUNTER_CLEAR |
  274. HOST_COUNTER_CLEAR);
  275. /* setup fifo asic */
  276. nsp_write(base, IFSELECT, IF_REGSEL);
  277. nsp_index_write(base, TERMPWRCTRL, 0);
  278. if ((nsp_index_read(base, OTHERCONTROL) & TPWR_SENSE) == 0) {
  279. nsp_msg(KERN_INFO, "terminator power on");
  280. nsp_index_write(base, TERMPWRCTRL, POWER_ON);
  281. }
  282. nsp_index_write(base, TIMERCOUNT, 0);
  283. nsp_index_write(base, TIMERCOUNT, 0); /* requires 2 times!! */
  284. nsp_index_write(base, SYNCREG, 0);
  285. nsp_index_write(base, ACKWIDTH, 0);
  286. /* enable interrupts and ack them */
  287. nsp_index_write(base, SCSIIRQMODE, SCSI_PHASE_CHANGE_EI |
  288. RESELECT_EI |
  289. SCSI_RESET_IRQ_EI );
  290. nsp_write(base, IRQCONTROL, IRQCONTROL_ALLCLEAR);
  291. nsp_setup_fifo(data, FALSE);
  292. return TRUE;
  293. }
  294. /*
  295. * Start selection phase
  296. */
  297. static int nsphw_start_selection(Scsi_Cmnd *SCpnt)
  298. {
  299. unsigned int host_id = SCpnt->device->host->this_id;
  300. unsigned int base = SCpnt->device->host->io_port;
  301. unsigned char target = scmd_id(SCpnt);
  302. nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
  303. int time_out;
  304. unsigned char phase, arbit;
  305. //nsp_dbg(NSP_DEBUG_RESELECTION, "in");
  306. phase = nsp_index_read(base, SCSIBUSMON);
  307. if(phase != BUSMON_BUS_FREE) {
  308. //nsp_dbg(NSP_DEBUG_RESELECTION, "bus busy");
  309. return FALSE;
  310. }
  311. /* start arbitration */
  312. //nsp_dbg(NSP_DEBUG_RESELECTION, "start arbit");
  313. SCpnt->SCp.phase = PH_ARBSTART;
  314. nsp_index_write(base, SETARBIT, ARBIT_GO);
  315. time_out = 1000;
  316. do {
  317. /* XXX: what a stupid chip! */
  318. arbit = nsp_index_read(base, ARBITSTATUS);
  319. //nsp_dbg(NSP_DEBUG_RESELECTION, "arbit=%d, wait_count=%d", arbit, wait_count);
  320. udelay(1); /* hold 1.2us */
  321. } while((arbit & (ARBIT_WIN | ARBIT_FAIL)) == 0 &&
  322. (time_out-- != 0));
  323. if (!(arbit & ARBIT_WIN)) {
  324. //nsp_dbg(NSP_DEBUG_RESELECTION, "arbit fail");
  325. nsp_index_write(base, SETARBIT, ARBIT_FLAG_CLEAR);
  326. return FALSE;
  327. }
  328. /* assert select line */
  329. //nsp_dbg(NSP_DEBUG_RESELECTION, "assert SEL line");
  330. SCpnt->SCp.phase = PH_SELSTART;
  331. udelay(3); /* wait 2.4us */
  332. nsp_index_write(base, SCSIDATALATCH, BIT(host_id) | BIT(target));
  333. nsp_index_write(base, SCSIBUSCTRL, SCSI_SEL | SCSI_BSY | SCSI_ATN);
  334. udelay(2); /* wait >1.2us */
  335. nsp_index_write(base, SCSIBUSCTRL, SCSI_SEL | SCSI_BSY | SCSI_DATAOUT_ENB | SCSI_ATN);
  336. nsp_index_write(base, SETARBIT, ARBIT_FLAG_CLEAR);
  337. /*udelay(1);*/ /* wait >90ns */
  338. nsp_index_write(base, SCSIBUSCTRL, SCSI_SEL | SCSI_DATAOUT_ENB | SCSI_ATN);
  339. /* check selection timeout */
  340. nsp_start_timer(SCpnt, 1000/51);
  341. data->SelectionTimeOut = 1;
  342. return TRUE;
  343. }
  344. struct nsp_sync_table {
  345. unsigned int min_period;
  346. unsigned int max_period;
  347. unsigned int chip_period;
  348. unsigned int ack_width;
  349. };
  350. static struct nsp_sync_table nsp_sync_table_40M[] = {
  351. {0x0c, 0x0c, 0x1, 0}, /* 20MB 50ns*/
  352. {0x19, 0x19, 0x3, 1}, /* 10MB 100ns*/
  353. {0x1a, 0x25, 0x5, 2}, /* 7.5MB 150ns*/
  354. {0x26, 0x32, 0x7, 3}, /* 5MB 200ns*/
  355. { 0, 0, 0, 0},
  356. };
  357. static struct nsp_sync_table nsp_sync_table_20M[] = {
  358. {0x19, 0x19, 0x1, 0}, /* 10MB 100ns*/
  359. {0x1a, 0x25, 0x2, 0}, /* 7.5MB 150ns*/
  360. {0x26, 0x32, 0x3, 1}, /* 5MB 200ns*/
  361. { 0, 0, 0, 0},
  362. };
  363. /*
  364. * setup synchronous data transfer mode
  365. */
  366. static int nsp_analyze_sdtr(Scsi_Cmnd *SCpnt)
  367. {
  368. unsigned char target = scmd_id(SCpnt);
  369. // unsigned char lun = SCpnt->device->lun;
  370. nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
  371. sync_data *sync = &(data->Sync[target]);
  372. struct nsp_sync_table *sync_table;
  373. unsigned int period, offset;
  374. int i;
  375. nsp_dbg(NSP_DEBUG_SYNC, "in");
  376. period = sync->SyncPeriod;
  377. offset = sync->SyncOffset;
  378. nsp_dbg(NSP_DEBUG_SYNC, "period=0x%x, offset=0x%x", period, offset);
  379. if ((data->ScsiClockDiv & (BIT(0)|BIT(1))) == CLOCK_20M) {
  380. sync_table = nsp_sync_table_20M;
  381. } else {
  382. sync_table = nsp_sync_table_40M;
  383. }
  384. for ( i = 0; sync_table->max_period != 0; i++, sync_table++) {
  385. if ( period >= sync_table->min_period &&
  386. period <= sync_table->max_period ) {
  387. break;
  388. }
  389. }
  390. if (period != 0 && sync_table->max_period == 0) {
  391. /*
  392. * No proper period/offset found
  393. */
  394. nsp_dbg(NSP_DEBUG_SYNC, "no proper period/offset");
  395. sync->SyncPeriod = 0;
  396. sync->SyncOffset = 0;
  397. sync->SyncRegister = 0;
  398. sync->AckWidth = 0;
  399. return FALSE;
  400. }
  401. sync->SyncRegister = (sync_table->chip_period << SYNCREG_PERIOD_SHIFT) |
  402. (offset & SYNCREG_OFFSET_MASK);
  403. sync->AckWidth = sync_table->ack_width;
  404. nsp_dbg(NSP_DEBUG_SYNC, "sync_reg=0x%x, ack_width=0x%x", sync->SyncRegister, sync->AckWidth);
  405. return TRUE;
  406. }
  407. /*
  408. * start ninja hardware timer
  409. */
  410. static void nsp_start_timer(Scsi_Cmnd *SCpnt, int time)
  411. {
  412. unsigned int base = SCpnt->device->host->io_port;
  413. nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
  414. //nsp_dbg(NSP_DEBUG_INTR, "in SCpnt=0x%p, time=%d", SCpnt, time);
  415. data->TimerCount = time;
  416. nsp_index_write(base, TIMERCOUNT, time);
  417. }
  418. /*
  419. * wait for bus phase change
  420. */
  421. static int nsp_negate_signal(Scsi_Cmnd *SCpnt, unsigned char mask, char *str)
  422. {
  423. unsigned int base = SCpnt->device->host->io_port;
  424. unsigned char reg;
  425. int time_out;
  426. //nsp_dbg(NSP_DEBUG_INTR, "in");
  427. time_out = 100;
  428. do {
  429. reg = nsp_index_read(base, SCSIBUSMON);
  430. if (reg == 0xff) {
  431. break;
  432. }
  433. } while ((time_out-- != 0) && (reg & mask) != 0);
  434. if (time_out == 0) {
  435. nsp_msg(KERN_DEBUG, " %s signal off timeut", str);
  436. }
  437. return 0;
  438. }
  439. /*
  440. * expect Ninja Irq
  441. */
  442. static int nsp_expect_signal(Scsi_Cmnd *SCpnt,
  443. unsigned char current_phase,
  444. unsigned char mask)
  445. {
  446. unsigned int base = SCpnt->device->host->io_port;
  447. int time_out;
  448. unsigned char phase, i_src;
  449. //nsp_dbg(NSP_DEBUG_INTR, "current_phase=0x%x, mask=0x%x", current_phase, mask);
  450. time_out = 100;
  451. do {
  452. phase = nsp_index_read(base, SCSIBUSMON);
  453. if (phase == 0xff) {
  454. //nsp_dbg(NSP_DEBUG_INTR, "ret -1");
  455. return -1;
  456. }
  457. i_src = nsp_read(base, IRQSTATUS);
  458. if (i_src & IRQSTATUS_SCSI) {
  459. //nsp_dbg(NSP_DEBUG_INTR, "ret 0 found scsi signal");
  460. return 0;
  461. }
  462. if ((phase & mask) != 0 && (phase & BUSMON_PHASE_MASK) == current_phase) {
  463. //nsp_dbg(NSP_DEBUG_INTR, "ret 1 phase=0x%x", phase);
  464. return 1;
  465. }
  466. } while(time_out-- != 0);
  467. //nsp_dbg(NSP_DEBUG_INTR, "timeout");
  468. return -1;
  469. }
  470. /*
  471. * transfer SCSI message
  472. */
  473. static int nsp_xfer(Scsi_Cmnd *SCpnt, int phase)
  474. {
  475. unsigned int base = SCpnt->device->host->io_port;
  476. nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
  477. char *buf = data->MsgBuffer;
  478. int len = min(MSGBUF_SIZE, data->MsgLen);
  479. int ptr;
  480. int ret;
  481. //nsp_dbg(NSP_DEBUG_DATA_IO, "in");
  482. for (ptr = 0; len > 0; len--, ptr++) {
  483. ret = nsp_expect_signal(SCpnt, phase, BUSMON_REQ);
  484. if (ret <= 0) {
  485. nsp_dbg(NSP_DEBUG_DATA_IO, "xfer quit");
  486. return 0;
  487. }
  488. /* if last byte, negate ATN */
  489. if (len == 1 && SCpnt->SCp.phase == PH_MSG_OUT) {
  490. nsp_index_write(base, SCSIBUSCTRL, AUTODIRECTION | ACKENB);
  491. }
  492. /* read & write message */
  493. if (phase & BUSMON_IO) {
  494. nsp_dbg(NSP_DEBUG_DATA_IO, "read msg");
  495. buf[ptr] = nsp_index_read(base, SCSIDATAWITHACK);
  496. } else {
  497. nsp_dbg(NSP_DEBUG_DATA_IO, "write msg");
  498. nsp_index_write(base, SCSIDATAWITHACK, buf[ptr]);
  499. }
  500. nsp_negate_signal(SCpnt, BUSMON_ACK, "xfer<ack>");
  501. }
  502. return len;
  503. }
  504. /*
  505. * get extra SCSI data from fifo
  506. */
  507. static int nsp_dataphase_bypass(Scsi_Cmnd *SCpnt)
  508. {
  509. nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
  510. unsigned int count;
  511. //nsp_dbg(NSP_DEBUG_DATA_IO, "in");
  512. if (SCpnt->SCp.have_data_in != IO_IN) {
  513. return 0;
  514. }
  515. count = nsp_fifo_count(SCpnt);
  516. if (data->FifoCount == count) {
  517. //nsp_dbg(NSP_DEBUG_DATA_IO, "not use bypass quirk");
  518. return 0;
  519. }
  520. /*
  521. * XXX: NSP_QUIRK
  522. * data phase skip only occures in case of SCSI_LOW_READ
  523. */
  524. nsp_dbg(NSP_DEBUG_DATA_IO, "use bypass quirk");
  525. SCpnt->SCp.phase = PH_DATA;
  526. nsp_pio_read(SCpnt);
  527. nsp_setup_fifo(data, FALSE);
  528. return 0;
  529. }
  530. /*
  531. * accept reselection
  532. */
  533. static int nsp_reselected(Scsi_Cmnd *SCpnt)
  534. {
  535. unsigned int base = SCpnt->device->host->io_port;
  536. unsigned int host_id = SCpnt->device->host->this_id;
  537. //nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
  538. unsigned char bus_reg;
  539. unsigned char id_reg, tmp;
  540. int target;
  541. nsp_dbg(NSP_DEBUG_RESELECTION, "in");
  542. id_reg = nsp_index_read(base, RESELECTID);
  543. tmp = id_reg & (~BIT(host_id));
  544. target = 0;
  545. while(tmp != 0) {
  546. if (tmp & BIT(0)) {
  547. break;
  548. }
  549. tmp >>= 1;
  550. target++;
  551. }
  552. if (scmd_id(SCpnt) != target) {
  553. nsp_msg(KERN_ERR, "XXX: reselect ID must be %d in this implementation.", target);
  554. }
  555. nsp_negate_signal(SCpnt, BUSMON_SEL, "reselect<SEL>");
  556. nsp_nexus(SCpnt);
  557. bus_reg = nsp_index_read(base, SCSIBUSCTRL) & ~(SCSI_BSY | SCSI_ATN);
  558. nsp_index_write(base, SCSIBUSCTRL, bus_reg);
  559. nsp_index_write(base, SCSIBUSCTRL, bus_reg | AUTODIRECTION | ACKENB);
  560. return TRUE;
  561. }
  562. /*
  563. * count how many data transferd
  564. */
  565. static int nsp_fifo_count(Scsi_Cmnd *SCpnt)
  566. {
  567. unsigned int base = SCpnt->device->host->io_port;
  568. unsigned int count;
  569. unsigned int l, m, h, dummy;
  570. nsp_index_write(base, POINTERCLR, POINTER_CLEAR | ACK_COUNTER);
  571. l = nsp_index_read(base, TRANSFERCOUNT);
  572. m = nsp_index_read(base, TRANSFERCOUNT);
  573. h = nsp_index_read(base, TRANSFERCOUNT);
  574. dummy = nsp_index_read(base, TRANSFERCOUNT); /* required this! */
  575. count = (h << 16) | (m << 8) | (l << 0);
  576. //nsp_dbg(NSP_DEBUG_DATA_IO, "count=0x%x", count);
  577. return count;
  578. }
  579. /* fifo size */
  580. #define RFIFO_CRIT 64
  581. #define WFIFO_CRIT 64
  582. /*
  583. * read data in DATA IN phase
  584. */
  585. static void nsp_pio_read(Scsi_Cmnd *SCpnt)
  586. {
  587. unsigned int base = SCpnt->device->host->io_port;
  588. unsigned long mmio_base = SCpnt->device->host->base;
  589. nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
  590. long time_out;
  591. int ocount, res;
  592. unsigned char stat, fifo_stat;
  593. ocount = data->FifoCount;
  594. nsp_dbg(NSP_DEBUG_DATA_IO, "in SCpnt=0x%p resid=%d ocount=%d ptr=0x%p this_residual=%d buffers=0x%p nbuf=%d",
  595. SCpnt, SCpnt->resid, ocount, SCpnt->SCp.ptr, SCpnt->SCp.this_residual, SCpnt->SCp.buffer, SCpnt->SCp.buffers_residual);
  596. time_out = 1000;
  597. while ((time_out-- != 0) &&
  598. (SCpnt->SCp.this_residual > 0 || SCpnt->SCp.buffers_residual > 0 ) ) {
  599. stat = nsp_index_read(base, SCSIBUSMON);
  600. stat &= BUSMON_PHASE_MASK;
  601. res = nsp_fifo_count(SCpnt) - ocount;
  602. //nsp_dbg(NSP_DEBUG_DATA_IO, "ptr=0x%p this=0x%x ocount=0x%x res=0x%x", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, ocount, res);
  603. if (res == 0) { /* if some data avilable ? */
  604. if (stat == BUSPHASE_DATA_IN) { /* phase changed? */
  605. //nsp_dbg(NSP_DEBUG_DATA_IO, " wait for data this=%d", SCpnt->SCp.this_residual);
  606. continue;
  607. } else {
  608. nsp_dbg(NSP_DEBUG_DATA_IO, "phase changed stat=0x%x", stat);
  609. break;
  610. }
  611. }
  612. fifo_stat = nsp_read(base, FIFOSTATUS);
  613. if ((fifo_stat & FIFOSTATUS_FULL_EMPTY) == 0 &&
  614. stat == BUSPHASE_DATA_IN) {
  615. continue;
  616. }
  617. res = min(res, SCpnt->SCp.this_residual);
  618. switch (data->TransferMode) {
  619. case MODE_IO32:
  620. res &= ~(BIT(1)|BIT(0)); /* align 4 */
  621. nsp_fifo32_read(base, SCpnt->SCp.ptr, res >> 2);
  622. break;
  623. case MODE_IO8:
  624. nsp_fifo8_read (base, SCpnt->SCp.ptr, res );
  625. break;
  626. case MODE_MEM32:
  627. res &= ~(BIT(1)|BIT(0)); /* align 4 */
  628. nsp_mmio_fifo32_read(mmio_base, SCpnt->SCp.ptr, res >> 2);
  629. break;
  630. default:
  631. nsp_dbg(NSP_DEBUG_DATA_IO, "unknown read mode");
  632. return;
  633. }
  634. SCpnt->resid -= res;
  635. SCpnt->SCp.ptr += res;
  636. SCpnt->SCp.this_residual -= res;
  637. ocount += res;
  638. //nsp_dbg(NSP_DEBUG_DATA_IO, "ptr=0x%p this_residual=0x%x ocount=0x%x", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, ocount);
  639. /* go to next scatter list if available */
  640. if (SCpnt->SCp.this_residual == 0 &&
  641. SCpnt->SCp.buffers_residual != 0 ) {
  642. //nsp_dbg(NSP_DEBUG_DATA_IO, "scatterlist next timeout=%d", time_out);
  643. SCpnt->SCp.buffers_residual--;
  644. SCpnt->SCp.buffer++;
  645. SCpnt->SCp.ptr = BUFFER_ADDR;
  646. SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
  647. time_out = 1000;
  648. //nsp_dbg(NSP_DEBUG_DATA_IO, "page: 0x%p, off: 0x%x", SCpnt->SCp.buffer->page, SCpnt->SCp.buffer->offset);
  649. }
  650. }
  651. data->FifoCount = ocount;
  652. if (time_out == 0) {
  653. nsp_msg(KERN_DEBUG, "pio read timeout resid=%d this_residual=%d buffers_residual=%d",
  654. SCpnt->resid, SCpnt->SCp.this_residual, SCpnt->SCp.buffers_residual);
  655. }
  656. nsp_dbg(NSP_DEBUG_DATA_IO, "read ocount=0x%x", ocount);
  657. nsp_dbg(NSP_DEBUG_DATA_IO, "r cmd=%d resid=0x%x\n", data->CmdId, SCpnt->resid);
  658. }
  659. /*
  660. * write data in DATA OUT phase
  661. */
  662. static void nsp_pio_write(Scsi_Cmnd *SCpnt)
  663. {
  664. unsigned int base = SCpnt->device->host->io_port;
  665. unsigned long mmio_base = SCpnt->device->host->base;
  666. nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
  667. int time_out;
  668. int ocount, res;
  669. unsigned char stat;
  670. ocount = data->FifoCount;
  671. nsp_dbg(NSP_DEBUG_DATA_IO, "in fifocount=%d ptr=0x%p this_residual=%d buffers=0x%p nbuf=%d resid=0x%x",
  672. data->FifoCount, SCpnt->SCp.ptr, SCpnt->SCp.this_residual, SCpnt->SCp.buffer, SCpnt->SCp.buffers_residual, SCpnt->resid);
  673. time_out = 1000;
  674. while ((time_out-- != 0) &&
  675. (SCpnt->SCp.this_residual > 0 || SCpnt->SCp.buffers_residual > 0)) {
  676. stat = nsp_index_read(base, SCSIBUSMON);
  677. stat &= BUSMON_PHASE_MASK;
  678. if (stat != BUSPHASE_DATA_OUT) {
  679. res = ocount - nsp_fifo_count(SCpnt);
  680. nsp_dbg(NSP_DEBUG_DATA_IO, "phase changed stat=0x%x, res=%d\n", stat, res);
  681. /* Put back pointer */
  682. SCpnt->resid += res;
  683. SCpnt->SCp.ptr -= res;
  684. SCpnt->SCp.this_residual += res;
  685. ocount -= res;
  686. break;
  687. }
  688. res = ocount - nsp_fifo_count(SCpnt);
  689. if (res > 0) { /* write all data? */
  690. nsp_dbg(NSP_DEBUG_DATA_IO, "wait for all data out. ocount=0x%x res=%d", ocount, res);
  691. continue;
  692. }
  693. res = min(SCpnt->SCp.this_residual, WFIFO_CRIT);
  694. //nsp_dbg(NSP_DEBUG_DATA_IO, "ptr=0x%p this=0x%x res=0x%x", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, res);
  695. switch (data->TransferMode) {
  696. case MODE_IO32:
  697. res &= ~(BIT(1)|BIT(0)); /* align 4 */
  698. nsp_fifo32_write(base, SCpnt->SCp.ptr, res >> 2);
  699. break;
  700. case MODE_IO8:
  701. nsp_fifo8_write (base, SCpnt->SCp.ptr, res );
  702. break;
  703. case MODE_MEM32:
  704. res &= ~(BIT(1)|BIT(0)); /* align 4 */
  705. nsp_mmio_fifo32_write(mmio_base, SCpnt->SCp.ptr, res >> 2);
  706. break;
  707. default:
  708. nsp_dbg(NSP_DEBUG_DATA_IO, "unknown write mode");
  709. break;
  710. }
  711. SCpnt->resid -= res;
  712. SCpnt->SCp.ptr += res;
  713. SCpnt->SCp.this_residual -= res;
  714. ocount += res;
  715. /* go to next scatter list if available */
  716. if (SCpnt->SCp.this_residual == 0 &&
  717. SCpnt->SCp.buffers_residual != 0 ) {
  718. //nsp_dbg(NSP_DEBUG_DATA_IO, "scatterlist next");
  719. SCpnt->SCp.buffers_residual--;
  720. SCpnt->SCp.buffer++;
  721. SCpnt->SCp.ptr = BUFFER_ADDR;
  722. SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
  723. time_out = 1000;
  724. }
  725. }
  726. data->FifoCount = ocount;
  727. if (time_out == 0) {
  728. nsp_msg(KERN_DEBUG, "pio write timeout resid=0x%x", SCpnt->resid);
  729. }
  730. nsp_dbg(NSP_DEBUG_DATA_IO, "write ocount=0x%x", ocount);
  731. nsp_dbg(NSP_DEBUG_DATA_IO, "w cmd=%d resid=0x%x\n", data->CmdId, SCpnt->resid);
  732. }
  733. #undef RFIFO_CRIT
  734. #undef WFIFO_CRIT
  735. /*
  736. * setup synchronous/asynchronous data transfer mode
  737. */
  738. static int nsp_nexus(Scsi_Cmnd *SCpnt)
  739. {
  740. unsigned int base = SCpnt->device->host->io_port;
  741. unsigned char target = scmd_id(SCpnt);
  742. // unsigned char lun = SCpnt->device->lun;
  743. nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
  744. sync_data *sync = &(data->Sync[target]);
  745. //nsp_dbg(NSP_DEBUG_DATA_IO, "in SCpnt=0x%p", SCpnt);
  746. /* setup synch transfer registers */
  747. nsp_index_write(base, SYNCREG, sync->SyncRegister);
  748. nsp_index_write(base, ACKWIDTH, sync->AckWidth);
  749. if (SCpnt->use_sg == 0 ||
  750. SCpnt->resid % 4 != 0 ||
  751. SCpnt->resid <= PAGE_SIZE ) {
  752. data->TransferMode = MODE_IO8;
  753. } else if (nsp_burst_mode == BURST_MEM32) {
  754. data->TransferMode = MODE_MEM32;
  755. } else if (nsp_burst_mode == BURST_IO32) {
  756. data->TransferMode = MODE_IO32;
  757. } else {
  758. data->TransferMode = MODE_IO8;
  759. }
  760. /* setup pdma fifo */
  761. nsp_setup_fifo(data, TRUE);
  762. /* clear ack counter */
  763. data->FifoCount = 0;
  764. nsp_index_write(base, POINTERCLR, POINTER_CLEAR |
  765. ACK_COUNTER_CLEAR |
  766. REQ_COUNTER_CLEAR |
  767. HOST_COUNTER_CLEAR);
  768. return 0;
  769. }
  770. #include "nsp_message.c"
  771. /*
  772. * interrupt handler
  773. */
  774. static irqreturn_t nspintr(int irq, void *dev_id)
  775. {
  776. unsigned int base;
  777. unsigned char irq_status, irq_phase, phase;
  778. Scsi_Cmnd *tmpSC;
  779. unsigned char target, lun;
  780. unsigned int *sync_neg;
  781. int i, tmp;
  782. nsp_hw_data *data;
  783. //nsp_dbg(NSP_DEBUG_INTR, "dev_id=0x%p", dev_id);
  784. //nsp_dbg(NSP_DEBUG_INTR, "host=0x%p", ((scsi_info_t *)dev_id)->host);
  785. if ( dev_id != NULL &&
  786. ((scsi_info_t *)dev_id)->host != NULL ) {
  787. scsi_info_t *info = (scsi_info_t *)dev_id;
  788. data = (nsp_hw_data *)info->host->hostdata;
  789. } else {
  790. nsp_dbg(NSP_DEBUG_INTR, "host data wrong");
  791. return IRQ_NONE;
  792. }
  793. //nsp_dbg(NSP_DEBUG_INTR, "&nsp_data_base=0x%p, dev_id=0x%p", &nsp_data_base, dev_id);
  794. base = data->BaseAddress;
  795. //nsp_dbg(NSP_DEBUG_INTR, "base=0x%x", base);
  796. /*
  797. * interrupt check
  798. */
  799. nsp_write(base, IRQCONTROL, IRQCONTROL_IRQDISABLE);
  800. irq_status = nsp_read(base, IRQSTATUS);
  801. //nsp_dbg(NSP_DEBUG_INTR, "irq_status=0x%x", irq_status);
  802. if ((irq_status == 0xff) || ((irq_status & IRQSTATUS_MASK) == 0)) {
  803. nsp_write(base, IRQCONTROL, 0);
  804. //nsp_dbg(NSP_DEBUG_INTR, "no irq/shared irq");
  805. return IRQ_NONE;
  806. }
  807. /* XXX: IMPORTANT
  808. * Do not read an irq_phase register if no scsi phase interrupt.
  809. * Unless, you should lose a scsi phase interrupt.
  810. */
  811. phase = nsp_index_read(base, SCSIBUSMON);
  812. if((irq_status & IRQSTATUS_SCSI) != 0) {
  813. irq_phase = nsp_index_read(base, IRQPHASESENCE);
  814. } else {
  815. irq_phase = 0;
  816. }
  817. //nsp_dbg(NSP_DEBUG_INTR, "irq_phase=0x%x", irq_phase);
  818. /*
  819. * timer interrupt handler (scsi vs timer interrupts)
  820. */
  821. //nsp_dbg(NSP_DEBUG_INTR, "timercount=%d", data->TimerCount);
  822. if (data->TimerCount != 0) {
  823. //nsp_dbg(NSP_DEBUG_INTR, "stop timer");
  824. nsp_index_write(base, TIMERCOUNT, 0);
  825. nsp_index_write(base, TIMERCOUNT, 0);
  826. data->TimerCount = 0;
  827. }
  828. if ((irq_status & IRQSTATUS_MASK) == IRQSTATUS_TIMER &&
  829. data->SelectionTimeOut == 0) {
  830. //nsp_dbg(NSP_DEBUG_INTR, "timer start");
  831. nsp_write(base, IRQCONTROL, IRQCONTROL_TIMER_CLEAR);
  832. return IRQ_HANDLED;
  833. }
  834. nsp_write(base, IRQCONTROL, IRQCONTROL_TIMER_CLEAR | IRQCONTROL_FIFO_CLEAR);
  835. if ((irq_status & IRQSTATUS_SCSI) &&
  836. (irq_phase & SCSI_RESET_IRQ)) {
  837. nsp_msg(KERN_ERR, "bus reset (power off?)");
  838. nsphw_init(data);
  839. nsp_bus_reset(data);
  840. if(data->CurrentSC != NULL) {
  841. tmpSC = data->CurrentSC;
  842. tmpSC->result = (DID_RESET << 16) |
  843. ((tmpSC->SCp.Message & 0xff) << 8) |
  844. ((tmpSC->SCp.Status & 0xff) << 0);
  845. nsp_scsi_done(tmpSC);
  846. }
  847. return IRQ_HANDLED;
  848. }
  849. if (data->CurrentSC == NULL) {
  850. nsp_msg(KERN_ERR, "CurrentSC==NULL irq_status=0x%x phase=0x%x irq_phase=0x%x this can't be happen. reset everything", irq_status, phase, irq_phase);
  851. nsphw_init(data);
  852. nsp_bus_reset(data);
  853. return IRQ_HANDLED;
  854. }
  855. tmpSC = data->CurrentSC;
  856. target = tmpSC->device->id;
  857. lun = tmpSC->device->lun;
  858. sync_neg = &(data->Sync[target].SyncNegotiation);
  859. /*
  860. * parse hardware SCSI irq reasons register
  861. */
  862. if (irq_status & IRQSTATUS_SCSI) {
  863. if (irq_phase & RESELECT_IRQ) {
  864. nsp_dbg(NSP_DEBUG_INTR, "reselect");
  865. nsp_write(base, IRQCONTROL, IRQCONTROL_RESELECT_CLEAR);
  866. if (nsp_reselected(tmpSC) != FALSE) {
  867. return IRQ_HANDLED;
  868. }
  869. }
  870. if ((irq_phase & (PHASE_CHANGE_IRQ | LATCHED_BUS_FREE)) == 0) {
  871. return IRQ_HANDLED;
  872. }
  873. }
  874. //show_phase(tmpSC);
  875. switch(tmpSC->SCp.phase) {
  876. case PH_SELSTART:
  877. // *sync_neg = SYNC_NOT_YET;
  878. if ((phase & BUSMON_BSY) == 0) {
  879. //nsp_dbg(NSP_DEBUG_INTR, "selection count=%d", data->SelectionTimeOut);
  880. if (data->SelectionTimeOut >= NSP_SELTIMEOUT) {
  881. nsp_dbg(NSP_DEBUG_INTR, "selection time out");
  882. data->SelectionTimeOut = 0;
  883. nsp_index_write(base, SCSIBUSCTRL, 0);
  884. tmpSC->result = DID_TIME_OUT << 16;
  885. nsp_scsi_done(tmpSC);
  886. return IRQ_HANDLED;
  887. }
  888. data->SelectionTimeOut += 1;
  889. nsp_start_timer(tmpSC, 1000/51);
  890. return IRQ_HANDLED;
  891. }
  892. /* attention assert */
  893. //nsp_dbg(NSP_DEBUG_INTR, "attention assert");
  894. data->SelectionTimeOut = 0;
  895. tmpSC->SCp.phase = PH_SELECTED;
  896. nsp_index_write(base, SCSIBUSCTRL, SCSI_ATN);
  897. udelay(1);
  898. nsp_index_write(base, SCSIBUSCTRL, SCSI_ATN | AUTODIRECTION | ACKENB);
  899. return IRQ_HANDLED;
  900. break;
  901. case PH_RESELECT:
  902. //nsp_dbg(NSP_DEBUG_INTR, "phase reselect");
  903. // *sync_neg = SYNC_NOT_YET;
  904. if ((phase & BUSMON_PHASE_MASK) != BUSPHASE_MESSAGE_IN) {
  905. tmpSC->result = DID_ABORT << 16;
  906. nsp_scsi_done(tmpSC);
  907. return IRQ_HANDLED;
  908. }
  909. /* fall thru */
  910. default:
  911. if ((irq_status & (IRQSTATUS_SCSI | IRQSTATUS_FIFO)) == 0) {
  912. return IRQ_HANDLED;
  913. }
  914. break;
  915. }
  916. /*
  917. * SCSI sequencer
  918. */
  919. //nsp_dbg(NSP_DEBUG_INTR, "start scsi seq");
  920. /* normal disconnect */
  921. if (((tmpSC->SCp.phase == PH_MSG_IN) || (tmpSC->SCp.phase == PH_MSG_OUT)) &&
  922. (irq_phase & LATCHED_BUS_FREE) != 0 ) {
  923. nsp_dbg(NSP_DEBUG_INTR, "normal disconnect irq_status=0x%x, phase=0x%x, irq_phase=0x%x", irq_status, phase, irq_phase);
  924. //*sync_neg = SYNC_NOT_YET;
  925. if ((tmpSC->SCp.Message == MSG_COMMAND_COMPLETE)) { /* all command complete and return status */
  926. tmpSC->result = (DID_OK << 16) |
  927. ((tmpSC->SCp.Message & 0xff) << 8) |
  928. ((tmpSC->SCp.Status & 0xff) << 0);
  929. nsp_dbg(NSP_DEBUG_INTR, "command complete result=0x%x", tmpSC->result);
  930. nsp_scsi_done(tmpSC);
  931. return IRQ_HANDLED;
  932. }
  933. return IRQ_HANDLED;
  934. }
  935. /* check unexpected bus free state */
  936. if (phase == 0) {
  937. nsp_msg(KERN_DEBUG, "unexpected bus free. irq_status=0x%x, phase=0x%x, irq_phase=0x%x", irq_status, phase, irq_phase);
  938. *sync_neg = SYNC_NG;
  939. tmpSC->result = DID_ERROR << 16;
  940. nsp_scsi_done(tmpSC);
  941. return IRQ_HANDLED;
  942. }
  943. switch (phase & BUSMON_PHASE_MASK) {
  944. case BUSPHASE_COMMAND:
  945. nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_COMMAND");
  946. if ((phase & BUSMON_REQ) == 0) {
  947. nsp_dbg(NSP_DEBUG_INTR, "REQ == 0");
  948. return IRQ_HANDLED;
  949. }
  950. tmpSC->SCp.phase = PH_COMMAND;
  951. nsp_nexus(tmpSC);
  952. /* write scsi command */
  953. nsp_dbg(NSP_DEBUG_INTR, "cmd_len=%d", tmpSC->cmd_len);
  954. nsp_index_write(base, COMMANDCTRL, CLEAR_COMMAND_POINTER);
  955. for (i = 0; i < tmpSC->cmd_len; i++) {
  956. nsp_index_write(base, COMMANDDATA, tmpSC->cmnd[i]);
  957. }
  958. nsp_index_write(base, COMMANDCTRL, CLEAR_COMMAND_POINTER | AUTO_COMMAND_GO);
  959. break;
  960. case BUSPHASE_DATA_OUT:
  961. nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_DATA_OUT");
  962. tmpSC->SCp.phase = PH_DATA;
  963. tmpSC->SCp.have_data_in = IO_OUT;
  964. nsp_pio_write(tmpSC);
  965. break;
  966. case BUSPHASE_DATA_IN:
  967. nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_DATA_IN");
  968. tmpSC->SCp.phase = PH_DATA;
  969. tmpSC->SCp.have_data_in = IO_IN;
  970. nsp_pio_read(tmpSC);
  971. break;
  972. case BUSPHASE_STATUS:
  973. nsp_dataphase_bypass(tmpSC);
  974. nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_STATUS");
  975. tmpSC->SCp.phase = PH_STATUS;
  976. tmpSC->SCp.Status = nsp_index_read(base, SCSIDATAWITHACK);
  977. nsp_dbg(NSP_DEBUG_INTR, "message=0x%x status=0x%x", tmpSC->SCp.Message, tmpSC->SCp.Status);
  978. break;
  979. case BUSPHASE_MESSAGE_OUT:
  980. nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_MESSAGE_OUT");
  981. if ((phase & BUSMON_REQ) == 0) {
  982. goto timer_out;
  983. }
  984. tmpSC->SCp.phase = PH_MSG_OUT;
  985. //*sync_neg = SYNC_NOT_YET;
  986. data->MsgLen = i = 0;
  987. data->MsgBuffer[i] = IDENTIFY(TRUE, lun); i++;
  988. if (*sync_neg == SYNC_NOT_YET) {
  989. data->Sync[target].SyncPeriod = 0;
  990. data->Sync[target].SyncOffset = 0;
  991. /**/
  992. data->MsgBuffer[i] = MSG_EXTENDED; i++;
  993. data->MsgBuffer[i] = 3; i++;
  994. data->MsgBuffer[i] = MSG_EXT_SDTR; i++;
  995. data->MsgBuffer[i] = 0x0c; i++;
  996. data->MsgBuffer[i] = 15; i++;
  997. /**/
  998. }
  999. data->MsgLen = i;
  1000. nsp_analyze_sdtr(tmpSC);
  1001. show_message(data);
  1002. nsp_message_out(tmpSC);
  1003. break;
  1004. case BUSPHASE_MESSAGE_IN:
  1005. nsp_dataphase_bypass(tmpSC);
  1006. nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_MESSAGE_IN");
  1007. if ((phase & BUSMON_REQ) == 0) {
  1008. goto timer_out;
  1009. }
  1010. tmpSC->SCp.phase = PH_MSG_IN;
  1011. nsp_message_in(tmpSC);
  1012. /**/
  1013. if (*sync_neg == SYNC_NOT_YET) {
  1014. //nsp_dbg(NSP_DEBUG_INTR, "sync target=%d,lun=%d",target,lun);
  1015. if (data->MsgLen >= 5 &&
  1016. data->MsgBuffer[0] == MSG_EXTENDED &&
  1017. data->MsgBuffer[1] == 3 &&
  1018. data->MsgBuffer[2] == MSG_EXT_SDTR ) {
  1019. data->Sync[target].SyncPeriod = data->MsgBuffer[3];
  1020. data->Sync[target].SyncOffset = data->MsgBuffer[4];
  1021. //nsp_dbg(NSP_DEBUG_INTR, "sync ok, %d %d", data->MsgBuffer[3], data->MsgBuffer[4]);
  1022. *sync_neg = SYNC_OK;
  1023. } else {
  1024. data->Sync[target].SyncPeriod = 0;
  1025. data->Sync[target].SyncOffset = 0;
  1026. *sync_neg = SYNC_NG;
  1027. }
  1028. nsp_analyze_sdtr(tmpSC);
  1029. }
  1030. /**/
  1031. /* search last messeage byte */
  1032. tmp = -1;
  1033. for (i = 0; i < data->MsgLen; i++) {
  1034. tmp = data->MsgBuffer[i];
  1035. if (data->MsgBuffer[i] == MSG_EXTENDED) {
  1036. i += (1 + data->MsgBuffer[i+1]);
  1037. }
  1038. }
  1039. tmpSC->SCp.Message = tmp;
  1040. nsp_dbg(NSP_DEBUG_INTR, "message=0x%x len=%d", tmpSC->SCp.Message, data->MsgLen);
  1041. show_message(data);
  1042. break;
  1043. case BUSPHASE_SELECT:
  1044. default:
  1045. nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE other");
  1046. break;
  1047. }
  1048. //nsp_dbg(NSP_DEBUG_INTR, "out");
  1049. return IRQ_HANDLED;
  1050. timer_out:
  1051. nsp_start_timer(tmpSC, 1000/102);
  1052. return IRQ_HANDLED;
  1053. }
  1054. #ifdef NSP_DEBUG
  1055. #include "nsp_debug.c"
  1056. #endif /* NSP_DEBUG */
  1057. /*----------------------------------------------------------------*/
  1058. /* look for ninja3 card and init if found */
  1059. /*----------------------------------------------------------------*/
  1060. static struct Scsi_Host *nsp_detect(struct scsi_host_template *sht)
  1061. {
  1062. struct Scsi_Host *host; /* registered host structure */
  1063. nsp_hw_data *data_b = &nsp_data_base, *data;
  1064. nsp_dbg(NSP_DEBUG_INIT, "this_id=%d", sht->this_id);
  1065. #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
  1066. host = scsi_host_alloc(&nsp_driver_template, sizeof(nsp_hw_data));
  1067. #else
  1068. host = scsi_register(sht, sizeof(nsp_hw_data));
  1069. #endif
  1070. if (host == NULL) {
  1071. nsp_dbg(NSP_DEBUG_INIT, "host failed");
  1072. return NULL;
  1073. }
  1074. memcpy(host->hostdata, data_b, sizeof(nsp_hw_data));
  1075. data = (nsp_hw_data *)host->hostdata;
  1076. data->ScsiInfo->host = host;
  1077. #ifdef NSP_DEBUG
  1078. data->CmdId = 0;
  1079. #endif
  1080. nsp_dbg(NSP_DEBUG_INIT, "irq=%d,%d", data_b->IrqNumber, ((nsp_hw_data *)host->hostdata)->IrqNumber);
  1081. host->unique_id = data->BaseAddress;
  1082. host->io_port = data->BaseAddress;
  1083. host->n_io_port = data->NumAddress;
  1084. host->irq = data->IrqNumber;
  1085. host->base = data->MmioAddress;
  1086. spin_lock_init(&(data->Lock));
  1087. snprintf(data->nspinfo,
  1088. sizeof(data->nspinfo),
  1089. "NinjaSCSI-3/32Bi Driver $Revision: 1.23 $ IO:0x%04lx-0x%04lx MMIO(virt addr):0x%04lx IRQ:%02d",
  1090. host->io_port, host->io_port + host->n_io_port - 1,
  1091. host->base,
  1092. host->irq);
  1093. sht->name = data->nspinfo;
  1094. nsp_dbg(NSP_DEBUG_INIT, "end");
  1095. return host; /* detect done. */
  1096. }
  1097. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
  1098. static int nsp_detect_old(struct scsi_host_template *sht)
  1099. {
  1100. if (nsp_detect(sht) == NULL) {
  1101. return 0;
  1102. } else {
  1103. //MOD_INC_USE_COUNT;
  1104. return 1;
  1105. }
  1106. }
  1107. static int nsp_release_old(struct Scsi_Host *shpnt)
  1108. {
  1109. //nsp_hw_data *data = (nsp_hw_data *)shpnt->hostdata;
  1110. /* PCMCIA Card Service dose same things below. */
  1111. /* So we do nothing. */
  1112. //if (shpnt->irq) {
  1113. // free_irq(shpnt->irq, data->ScsiInfo);
  1114. //}
  1115. //if (shpnt->io_port) {
  1116. // release_region(shpnt->io_port, shpnt->n_io_port);
  1117. //}
  1118. //MOD_DEC_USE_COUNT;
  1119. return 0;
  1120. }
  1121. #endif
  1122. /*----------------------------------------------------------------*/
  1123. /* return info string */
  1124. /*----------------------------------------------------------------*/
  1125. static const char *nsp_info(struct Scsi_Host *shpnt)
  1126. {
  1127. nsp_hw_data *data = (nsp_hw_data *)shpnt->hostdata;
  1128. return data->nspinfo;
  1129. }
  1130. #undef SPRINTF
  1131. #define SPRINTF(args...) \
  1132. do { \
  1133. if(length > (pos - buffer)) { \
  1134. pos += snprintf(pos, length - (pos - buffer) + 1, ## args); \
  1135. nsp_dbg(NSP_DEBUG_PROC, "buffer=0x%p pos=0x%p length=%d %d\n", buffer, pos, length, length - (pos - buffer));\
  1136. } \
  1137. } while(0)
  1138. static int
  1139. nsp_proc_info(
  1140. #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
  1141. struct Scsi_Host *host,
  1142. #endif
  1143. char *buffer,
  1144. char **start,
  1145. off_t offset,
  1146. int length,
  1147. #if !(LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
  1148. int hostno,
  1149. #endif
  1150. int inout)
  1151. {
  1152. int id;
  1153. char *pos = buffer;
  1154. int thislength;
  1155. int speed;
  1156. unsigned long flags;
  1157. nsp_hw_data *data;
  1158. #if !(LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
  1159. struct Scsi_Host *host;
  1160. #else
  1161. int hostno;
  1162. #endif
  1163. if (inout) {
  1164. return -EINVAL;
  1165. }
  1166. #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
  1167. hostno = host->host_no;
  1168. #else
  1169. /* search this HBA host */
  1170. host = scsi_host_hn_get(hostno);
  1171. if (host == NULL) {
  1172. return -ESRCH;
  1173. }
  1174. #endif
  1175. data = (nsp_hw_data *)host->hostdata;
  1176. SPRINTF("NinjaSCSI status\n\n");
  1177. SPRINTF("Driver version: $Revision: 1.23 $\n");
  1178. SPRINTF("SCSI host No.: %d\n", hostno);
  1179. SPRINTF("IRQ: %d\n", host->irq);
  1180. SPRINTF("IO: 0x%lx-0x%lx\n", host->io_port, host->io_port + host->n_io_port - 1);
  1181. SPRINTF("MMIO(virtual address): 0x%lx-0x%lx\n", host->base, host->base + data->MmioLength - 1);
  1182. SPRINTF("sg_tablesize: %d\n", host->sg_tablesize);
  1183. SPRINTF("burst transfer mode: ");
  1184. switch (nsp_burst_mode) {
  1185. case BURST_IO8:
  1186. SPRINTF("io8");
  1187. break;
  1188. case BURST_IO32:
  1189. SPRINTF("io32");
  1190. break;
  1191. case BURST_MEM32:
  1192. SPRINTF("mem32");
  1193. break;
  1194. default:
  1195. SPRINTF("???");
  1196. break;
  1197. }
  1198. SPRINTF("\n");
  1199. spin_lock_irqsave(&(data->Lock), flags);
  1200. SPRINTF("CurrentSC: 0x%p\n\n", data->CurrentSC);
  1201. spin_unlock_irqrestore(&(data->Lock), flags);
  1202. SPRINTF("SDTR status\n");
  1203. for(id = 0; id < ARRAY_SIZE(data->Sync); id++) {
  1204. SPRINTF("id %d: ", id);
  1205. if (id == host->this_id) {
  1206. SPRINTF("----- NinjaSCSI-3 host adapter\n");
  1207. continue;
  1208. }
  1209. switch(data->Sync[id].SyncNegotiation) {
  1210. case SYNC_OK:
  1211. SPRINTF(" sync");
  1212. break;
  1213. case SYNC_NG:
  1214. SPRINTF("async");
  1215. break;
  1216. case SYNC_NOT_YET:
  1217. SPRINTF(" none");
  1218. break;
  1219. default:
  1220. SPRINTF("?????");
  1221. break;
  1222. }
  1223. if (data->Sync[id].SyncPeriod != 0) {
  1224. speed = 1000000 / (data->Sync[id].SyncPeriod * 4);
  1225. SPRINTF(" transfer %d.%dMB/s, offset %d",
  1226. speed / 1000,
  1227. speed % 1000,
  1228. data->Sync[id].SyncOffset
  1229. );
  1230. }
  1231. SPRINTF("\n");
  1232. }
  1233. thislength = pos - (buffer + offset);
  1234. if(thislength < 0) {
  1235. *start = NULL;
  1236. return 0;
  1237. }
  1238. thislength = min(thislength, length);
  1239. *start = buffer + offset;
  1240. return thislength;
  1241. }
  1242. #undef SPRINTF
  1243. /*---------------------------------------------------------------*/
  1244. /* error handler */
  1245. /*---------------------------------------------------------------*/
  1246. /*
  1247. static int nsp_eh_abort(Scsi_Cmnd *SCpnt)
  1248. {
  1249. nsp_dbg(NSP_DEBUG_BUSRESET, "SCpnt=0x%p", SCpnt);
  1250. return nsp_eh_bus_reset(SCpnt);
  1251. }*/
  1252. static int nsp_bus_reset(nsp_hw_data *data)
  1253. {
  1254. unsigned int base = data->BaseAddress;
  1255. int i;
  1256. nsp_write(base, IRQCONTROL, IRQCONTROL_ALLMASK);
  1257. nsp_index_write(base, SCSIBUSCTRL, SCSI_RST);
  1258. mdelay(100); /* 100ms */
  1259. nsp_index_write(base, SCSIBUSCTRL, 0);
  1260. for(i = 0; i < 5; i++) {
  1261. nsp_index_read(base, IRQPHASESENCE); /* dummy read */
  1262. }
  1263. nsphw_init_sync(data);
  1264. nsp_write(base, IRQCONTROL, IRQCONTROL_ALLCLEAR);
  1265. return SUCCESS;
  1266. }
  1267. static int nsp_eh_bus_reset(Scsi_Cmnd *SCpnt)
  1268. {
  1269. nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
  1270. nsp_dbg(NSP_DEBUG_BUSRESET, "SCpnt=0x%p", SCpnt);
  1271. return nsp_bus_reset(data);
  1272. }
  1273. static int nsp_eh_host_reset(Scsi_Cmnd *SCpnt)
  1274. {
  1275. nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
  1276. nsp_dbg(NSP_DEBUG_BUSRESET, "in");
  1277. nsphw_init(data);
  1278. return SUCCESS;
  1279. }
  1280. /**********************************************************************
  1281. PCMCIA functions
  1282. **********************************************************************/
  1283. /*======================================================================
  1284. nsp_cs_attach() creates an "instance" of the driver, allocating
  1285. local data structures for one device. The device is registered
  1286. with Card Services.
  1287. The dev_link structure is initialized, but we don't actually
  1288. configure the card at this point -- we wait until we receive a
  1289. card insertion event.
  1290. ======================================================================*/
  1291. static int nsp_cs_probe(struct pcmcia_device *link)
  1292. {
  1293. scsi_info_t *info;
  1294. nsp_hw_data *data = &nsp_data_base;
  1295. int ret;
  1296. nsp_dbg(NSP_DEBUG_INIT, "in");
  1297. /* Create new SCSI device */
  1298. info = kmalloc(sizeof(*info), GFP_KERNEL);
  1299. if (info == NULL) { return -ENOMEM; }
  1300. memset(info, 0, sizeof(*info));
  1301. info->p_dev = link;
  1302. link->priv = info;
  1303. data->ScsiInfo = info;
  1304. nsp_dbg(NSP_DEBUG_INIT, "info=0x%p", info);
  1305. /* The io structure describes IO port mapping */
  1306. link->io.NumPorts1 = 0x10;
  1307. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  1308. link->io.IOAddrLines = 10; /* not used */
  1309. /* Interrupt setup */
  1310. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
  1311. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  1312. /* Interrupt handler */
  1313. link->irq.Handler = &nspintr;
  1314. link->irq.Instance = info;
  1315. link->irq.Attributes |= IRQF_SHARED;
  1316. /* General socket configuration */
  1317. link->conf.Attributes = CONF_ENABLE_IRQ;
  1318. link->conf.IntType = INT_MEMORY_AND_IO;
  1319. link->conf.Present = PRESENT_OPTION;
  1320. ret = nsp_cs_config(link);
  1321. nsp_dbg(NSP_DEBUG_INIT, "link=0x%p", link);
  1322. return ret;
  1323. } /* nsp_cs_attach */
  1324. /*======================================================================
  1325. This deletes a driver "instance". The device is de-registered
  1326. with Card Services. If it has been released, all local data
  1327. structures are freed. Otherwise, the structures will be freed
  1328. when the device is released.
  1329. ======================================================================*/
  1330. static void nsp_cs_detach(struct pcmcia_device *link)
  1331. {
  1332. nsp_dbg(NSP_DEBUG_INIT, "in, link=0x%p", link);
  1333. ((scsi_info_t *)link->priv)->stop = 1;
  1334. nsp_cs_release(link);
  1335. kfree(link->priv);
  1336. link->priv = NULL;
  1337. } /* nsp_cs_detach */
  1338. /*======================================================================
  1339. nsp_cs_config() is scheduled to run after a CARD_INSERTION event
  1340. is received, to configure the PCMCIA socket, and to make the
  1341. ethernet device available to the system.
  1342. ======================================================================*/
  1343. #define CS_CHECK(fn, ret) \
  1344. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  1345. /*====================================================================*/
  1346. static int nsp_cs_config(struct pcmcia_device *link)
  1347. {
  1348. int ret;
  1349. scsi_info_t *info = link->priv;
  1350. tuple_t tuple;
  1351. cisparse_t parse;
  1352. int last_ret, last_fn;
  1353. unsigned char tuple_data[64];
  1354. config_info_t conf;
  1355. win_req_t req;
  1356. memreq_t map;
  1357. cistpl_cftable_entry_t dflt = { 0 };
  1358. struct Scsi_Host *host;
  1359. nsp_hw_data *data = &nsp_data_base;
  1360. #if !(LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,74))
  1361. struct scsi_device *dev;
  1362. dev_node_t **tail, *node;
  1363. #endif
  1364. nsp_dbg(NSP_DEBUG_INIT, "in");
  1365. tuple.DesiredTuple = CISTPL_CONFIG;
  1366. tuple.Attributes = 0;
  1367. tuple.TupleData = tuple_data;
  1368. tuple.TupleDataMax = sizeof(tuple_data);
  1369. tuple.TupleOffset = 0;
  1370. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
  1371. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link, &tuple));
  1372. CS_CHECK(ParseTuple, pcmcia_parse_tuple(link, &tuple, &parse));
  1373. link->conf.ConfigBase = parse.config.base;
  1374. link->conf.Present = parse.config.rmask[0];
  1375. /* Look up the current Vcc */
  1376. CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(link, &conf));
  1377. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  1378. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
  1379. while (1) {
  1380. cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
  1381. if (pcmcia_get_tuple_data(link, &tuple) != 0 ||
  1382. pcmcia_parse_tuple(link, &tuple, &parse) != 0)
  1383. goto next_entry;
  1384. if (cfg->flags & CISTPL_CFTABLE_DEFAULT) { dflt = *cfg; }
  1385. if (cfg->index == 0) { goto next_entry; }
  1386. link->conf.ConfigIndex = cfg->index;
  1387. /* Does this card need audio output? */
  1388. if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
  1389. link->conf.Attributes |= CONF_ENABLE_SPKR;
  1390. link->conf.Status = CCSR_AUDIO_ENA;
  1391. }
  1392. /* Use power settings for Vcc and Vpp if present */
  1393. /* Note that the CIS values need to be rescaled */
  1394. if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
  1395. if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM]/10000) {
  1396. goto next_entry;
  1397. }
  1398. } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
  1399. if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM]/10000) {
  1400. goto next_entry;
  1401. }
  1402. }
  1403. if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM)) {
  1404. link->conf.Vpp =
  1405. cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
  1406. } else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM)) {
  1407. link->conf.Vpp =
  1408. dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
  1409. }
  1410. /* Do we need to allocate an interrupt? */
  1411. if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1) {
  1412. link->conf.Attributes |= CONF_ENABLE_IRQ;
  1413. }
  1414. /* IO window settings */
  1415. link->io.NumPorts1 = link->io.NumPorts2 = 0;
  1416. if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
  1417. cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
  1418. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  1419. if (!(io->flags & CISTPL_IO_8BIT))
  1420. link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
  1421. if (!(io->flags & CISTPL_IO_16BIT))
  1422. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  1423. link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
  1424. link->io.BasePort1 = io->win[0].base;
  1425. link->io.NumPorts1 = io->win[0].len;
  1426. if (io->nwin > 1) {
  1427. link->io.Attributes2 = link->io.Attributes1;
  1428. link->io.BasePort2 = io->win[1].base;
  1429. link->io.NumPorts2 = io->win[1].len;
  1430. }
  1431. /* This reserves IO space but doesn't actually enable it */
  1432. if (pcmcia_request_io(link, &link->io) != 0)
  1433. goto next_entry;
  1434. }
  1435. if ((cfg->mem.nwin > 0) || (dflt.mem.nwin > 0)) {
  1436. cistpl_mem_t *mem =
  1437. (cfg->mem.nwin) ? &cfg->mem : &dflt.mem;
  1438. req.Attributes = WIN_DATA_WIDTH_16|WIN_MEMORY_TYPE_CM;
  1439. req.Attributes |= WIN_ENABLE;
  1440. req.Base = mem->win[0].host_addr;
  1441. req.Size = mem->win[0].len;
  1442. if (req.Size < 0x1000) {
  1443. req.Size = 0x1000;
  1444. }
  1445. req.AccessSpeed = 0;
  1446. if (pcmcia_request_window(&link, &req, &link->win) != 0)
  1447. goto next_entry;
  1448. map.Page = 0; map.CardOffset = mem->win[0].card_addr;
  1449. if (pcmcia_map_mem_page(link->win, &map) != 0)
  1450. goto next_entry;
  1451. data->MmioAddress = (unsigned long)ioremap_nocache(req.Base, req.Size);
  1452. data->MmioLength = req.Size;
  1453. }
  1454. /* If we got this far, we're cool! */
  1455. break;
  1456. next_entry:
  1457. nsp_dbg(NSP_DEBUG_INIT, "next");
  1458. pcmcia_disable_device(link);
  1459. CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(link, &tuple));
  1460. }
  1461. if (link->conf.Attributes & CONF_ENABLE_IRQ) {
  1462. CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
  1463. }
  1464. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
  1465. if (free_ports) {
  1466. if (link->io.BasePort1) {
  1467. release_region(link->io.BasePort1, link->io.NumPorts1);
  1468. }
  1469. if (link->io.BasePort2) {
  1470. release_region(link->io.BasePort2, link->io.NumPorts2);
  1471. }
  1472. }
  1473. /* Set port and IRQ */
  1474. data->BaseAddress = link->io.BasePort1;
  1475. data->NumAddress = link->io.NumPorts1;
  1476. data->IrqNumber = link->irq.AssignedIRQ;
  1477. nsp_dbg(NSP_DEBUG_INIT, "I/O[0x%x+0x%x] IRQ %d",
  1478. data->BaseAddress, data->NumAddress, data->IrqNumber);
  1479. if(nsphw_init(data) == FALSE) {
  1480. goto cs_failed;
  1481. }
  1482. #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,2))
  1483. host = nsp_detect(&nsp_driver_template);
  1484. #else
  1485. scsi_register_host(&nsp_driver_template);
  1486. for (host = scsi_host_get_next(NULL); host != NULL;
  1487. host = scsi_host_get_next(host)) {
  1488. if (host->hostt == &nsp_driver_template) {
  1489. break;
  1490. }
  1491. }
  1492. #endif
  1493. if (host == NULL) {
  1494. nsp_dbg(NSP_DEBUG_INIT, "detect failed");
  1495. goto cs_failed;
  1496. }
  1497. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,74))
  1498. ret = scsi_add_host (host, NULL);
  1499. if (ret)
  1500. goto cs_failed;
  1501. scsi_scan_host(host);
  1502. snprintf(info->node.dev_name, sizeof(info->node.dev_name), "scsi%d", host->host_no);
  1503. link->dev_node = &info->node;
  1504. info->host = host;
  1505. #else
  1506. nsp_dbg(NSP_DEBUG_INIT, "GET_SCSI_INFO");
  1507. tail = &link->dev_node;
  1508. info->ndev = 0;
  1509. nsp_dbg(NSP_DEBUG_INIT, "host=0x%p", host);
  1510. for (dev = host->host_queue; dev != NULL; dev = dev->next) {
  1511. unsigned long id;
  1512. id = (dev->id & 0x0f) + ((dev->lun & 0x0f) << 4) +
  1513. ((dev->channel & 0x0f) << 8) +
  1514. ((dev->host->host_no & 0x0f) << 12);
  1515. node = &info->node[info->ndev];
  1516. node->minor = 0;
  1517. switch (dev->type) {
  1518. case TYPE_TAPE:
  1519. node->major = SCSI_TAPE_MAJOR;
  1520. snprintf(node->dev_name, sizeof(node->dev_name), "st#%04lx", id);
  1521. break;
  1522. case TYPE_DISK:
  1523. case TYPE_MOD:
  1524. node->major = SCSI_DISK0_MAJOR;
  1525. snprintf(node->dev_name, sizeof(node->dev_name), "sd#%04lx", id);
  1526. break;
  1527. case TYPE_ROM:
  1528. case TYPE_WORM:
  1529. node->major = SCSI_CDROM_MAJOR;
  1530. snprintf(node->dev_name, sizeof(node->dev_name), "sr#%04lx", id);
  1531. break;
  1532. default:
  1533. node->major = SCSI_GENERIC_MAJOR;
  1534. snprintf(node->dev_name, sizeof(node->dev_name), "sg#%04lx", id);
  1535. break;
  1536. }
  1537. *tail = node; tail = &node->next;
  1538. info->ndev++;
  1539. info->host = dev->host;
  1540. }
  1541. *tail = NULL;
  1542. if (info->ndev == 0) {
  1543. nsp_msg(KERN_INFO, "no SCSI devices found");
  1544. }
  1545. nsp_dbg(NSP_DEBUG_INIT, "host=0x%p", host);
  1546. #endif
  1547. /* Finally, report what we've done */
  1548. printk(KERN_INFO "nsp_cs: index 0x%02x: ",
  1549. link->conf.ConfigIndex);
  1550. if (link->conf.Vpp) {
  1551. printk(", Vpp %d.%d", link->conf.Vpp/10, link->conf.Vpp%10);
  1552. }
  1553. if (link->conf.Attributes & CONF_ENABLE_IRQ) {
  1554. printk(", irq %d", link->irq.AssignedIRQ);
  1555. }
  1556. if (link->io.NumPorts1) {
  1557. printk(", io 0x%04x-0x%04x", link->io.BasePort1,
  1558. link->io.BasePort1+link->io.NumPorts1-1);
  1559. }
  1560. if (link->io.NumPorts2)
  1561. printk(" & 0x%04x-0x%04x", link->io.BasePort2,
  1562. link->io.BasePort2+link->io.NumPorts2-1);
  1563. if (link->win)
  1564. printk(", mem 0x%06lx-0x%06lx", req.Base,
  1565. req.Base+req.Size-1);
  1566. printk("\n");
  1567. return 0;
  1568. cs_failed:
  1569. nsp_dbg(NSP_DEBUG_INIT, "config fail");
  1570. cs_error(link, last_fn, last_ret);
  1571. nsp_cs_release(link);
  1572. return -ENODEV;
  1573. } /* nsp_cs_config */
  1574. #undef CS_CHECK
  1575. /*======================================================================
  1576. After a card is removed, nsp_cs_release() will unregister the net
  1577. device, and release the PCMCIA configuration. If the device is
  1578. still open, this will be postponed until it is closed.
  1579. ======================================================================*/
  1580. static void nsp_cs_release(struct pcmcia_device *link)
  1581. {
  1582. scsi_info_t *info = link->priv;
  1583. nsp_hw_data *data = NULL;
  1584. if (info->host == NULL) {
  1585. nsp_msg(KERN_DEBUG, "unexpected card release call.");
  1586. } else {
  1587. data = (nsp_hw_data *)info->host->hostdata;
  1588. }
  1589. nsp_dbg(NSP_DEBUG_INIT, "link=0x%p", link);
  1590. /* Unlink the device chain */
  1591. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,2))
  1592. if (info->host != NULL) {
  1593. scsi_remove_host(info->host);
  1594. }
  1595. #else
  1596. scsi_unregister_host(&nsp_driver_template);
  1597. #endif
  1598. link->dev_node = NULL;
  1599. if (link->win) {
  1600. if (data != NULL) {
  1601. iounmap((void *)(data->MmioAddress));
  1602. }
  1603. }
  1604. pcmcia_disable_device(link);
  1605. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,2))
  1606. if (info->host != NULL) {
  1607. scsi_host_put(info->host);
  1608. }
  1609. #endif
  1610. } /* nsp_cs_release */
  1611. static int nsp_cs_suspend(struct pcmcia_device *link)
  1612. {
  1613. scsi_info_t *info = link->priv;
  1614. nsp_hw_data *data;
  1615. nsp_dbg(NSP_DEBUG_INIT, "event: suspend");
  1616. if (info->host != NULL) {
  1617. nsp_msg(KERN_INFO, "clear SDTR status");
  1618. data = (nsp_hw_data *)info->host->hostdata;
  1619. nsphw_init_sync(data);
  1620. }
  1621. info->stop = 1;
  1622. return 0;
  1623. }
  1624. static int nsp_cs_resume(struct pcmcia_device *link)
  1625. {
  1626. scsi_info_t *info = link->priv;
  1627. nsp_hw_data *data;
  1628. nsp_dbg(NSP_DEBUG_INIT, "event: resume");
  1629. info->stop = 0;
  1630. if (info->host != NULL) {
  1631. nsp_msg(KERN_INFO, "reset host and bus");
  1632. data = (nsp_hw_data *)info->host->hostdata;
  1633. nsphw_init (data);
  1634. nsp_bus_reset(data);
  1635. }
  1636. return 0;
  1637. }
  1638. /*======================================================================*
  1639. * module entry point
  1640. *====================================================================*/
  1641. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,68))
  1642. static struct pcmcia_device_id nsp_cs_ids[] = {
  1643. PCMCIA_DEVICE_PROD_ID123("IO DATA", "CBSC16 ", "1", 0x547e66dc, 0x0d63a3fd, 0x51de003a),
  1644. PCMCIA_DEVICE_PROD_ID123("KME ", "SCSI-CARD-001", "1", 0x534c02bc, 0x52008408, 0x51de003a),
  1645. PCMCIA_DEVICE_PROD_ID123("KME ", "SCSI-CARD-002", "1", 0x534c02bc, 0xcb09d5b2, 0x51de003a),
  1646. PCMCIA_DEVICE_PROD_ID123("KME ", "SCSI-CARD-003", "1", 0x534c02bc, 0xbc0ee524, 0x51de003a),
  1647. PCMCIA_DEVICE_PROD_ID123("KME ", "SCSI-CARD-004", "1", 0x534c02bc, 0x226a7087, 0x51de003a),
  1648. PCMCIA_DEVICE_PROD_ID123("WBT", "NinjaSCSI-3", "R1.0", 0xc7ba805f, 0xfdc7c97d, 0x6973710e),
  1649. PCMCIA_DEVICE_PROD_ID123("WORKBIT", "UltraNinja-16", "1", 0x28191418, 0xb70f4b09, 0x51de003a),
  1650. PCMCIA_DEVICE_NULL
  1651. };
  1652. MODULE_DEVICE_TABLE(pcmcia, nsp_cs_ids);
  1653. static struct pcmcia_driver nsp_driver = {
  1654. .owner = THIS_MODULE,
  1655. .drv = {
  1656. .name = "nsp_cs",
  1657. },
  1658. .probe = nsp_cs_probe,
  1659. .remove = nsp_cs_detach,
  1660. .id_table = nsp_cs_ids,
  1661. .suspend = nsp_cs_suspend,
  1662. .resume = nsp_cs_resume,
  1663. };
  1664. #endif
  1665. static int __init nsp_cs_init(void)
  1666. {
  1667. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,68))
  1668. nsp_msg(KERN_INFO, "loading...");
  1669. return pcmcia_register_driver(&nsp_driver);
  1670. #else
  1671. servinfo_t serv;
  1672. nsp_msg(KERN_INFO, "loading...");
  1673. pcmcia_get_card_services_info(&serv);
  1674. if (serv.Revision != CS_RELEASE_CODE) {
  1675. nsp_msg(KERN_DEBUG, "Card Services release does not match!");
  1676. return -EINVAL;
  1677. }
  1678. register_pcmcia_driver(&dev_info, &nsp_cs_attach, &nsp_cs_detach);
  1679. nsp_dbg(NSP_DEBUG_INIT, "out");
  1680. return 0;
  1681. #endif
  1682. }
  1683. static void __exit nsp_cs_exit(void)
  1684. {
  1685. nsp_msg(KERN_INFO, "unloading...");
  1686. pcmcia_unregister_driver(&nsp_driver);
  1687. }
  1688. module_init(nsp_cs_init)
  1689. module_exit(nsp_cs_exit)
  1690. /* end */