nsp_cs.c 54 KB

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