nsp_cs.c 58 KB

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