nsp_cs.c 58 KB

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