nsp32.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431
  1. /*
  2. * NinjaSCSI-32Bi Cardbus, NinjaSCSI-32UDE PCI/CardBus SCSI driver
  3. * Copyright (C) 2001, 2002, 2003
  4. * YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>
  5. * GOTO Masanori <gotom@debian.or.jp>, <gotom@debian.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. *
  18. * Revision History:
  19. * 1.0: Initial Release.
  20. * 1.1: Add /proc SDTR status.
  21. * Remove obsolete error handler nsp32_reset.
  22. * Some clean up.
  23. * 1.2: PowerPC (big endian) support.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/string.h>
  29. #include <linux/timer.h>
  30. #include <linux/ioport.h>
  31. #include <linux/major.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/pci.h>
  35. #include <linux/delay.h>
  36. #include <linux/ctype.h>
  37. #include <linux/dma-mapping.h>
  38. #include <asm/dma.h>
  39. #include <asm/io.h>
  40. #include <scsi/scsi.h>
  41. #include <scsi/scsi_cmnd.h>
  42. #include <scsi/scsi_device.h>
  43. #include <scsi/scsi_host.h>
  44. #include <scsi/scsi_ioctl.h>
  45. #include "nsp32.h"
  46. /***********************************************************************
  47. * Module parameters
  48. */
  49. static int trans_mode = 0; /* default: BIOS */
  50. module_param (trans_mode, int, 0);
  51. MODULE_PARM_DESC(trans_mode, "transfer mode (0: BIOS(default) 1: Async 2: Ultra20M");
  52. #define ASYNC_MODE 1
  53. #define ULTRA20M_MODE 2
  54. static bool auto_param = 0; /* default: ON */
  55. module_param (auto_param, bool, 0);
  56. MODULE_PARM_DESC(auto_param, "AutoParameter mode (0: ON(default) 1: OFF)");
  57. static bool disc_priv = 1; /* default: OFF */
  58. module_param (disc_priv, bool, 0);
  59. MODULE_PARM_DESC(disc_priv, "disconnection privilege mode (0: ON 1: OFF(default))");
  60. MODULE_AUTHOR("YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>, GOTO Masanori <gotom@debian.or.jp>");
  61. MODULE_DESCRIPTION("Workbit NinjaSCSI-32Bi/UDE CardBus/PCI SCSI host bus adapter module");
  62. MODULE_LICENSE("GPL");
  63. static const char *nsp32_release_version = "1.2";
  64. /****************************************************************************
  65. * Supported hardware
  66. */
  67. static struct pci_device_id nsp32_pci_table[] = {
  68. {
  69. .vendor = PCI_VENDOR_ID_IODATA,
  70. .device = PCI_DEVICE_ID_NINJASCSI_32BI_CBSC_II,
  71. .subvendor = PCI_ANY_ID,
  72. .subdevice = PCI_ANY_ID,
  73. .driver_data = MODEL_IODATA,
  74. },
  75. {
  76. .vendor = PCI_VENDOR_ID_WORKBIT,
  77. .device = PCI_DEVICE_ID_NINJASCSI_32BI_KME,
  78. .subvendor = PCI_ANY_ID,
  79. .subdevice = PCI_ANY_ID,
  80. .driver_data = MODEL_KME,
  81. },
  82. {
  83. .vendor = PCI_VENDOR_ID_WORKBIT,
  84. .device = PCI_DEVICE_ID_NINJASCSI_32BI_WBT,
  85. .subvendor = PCI_ANY_ID,
  86. .subdevice = PCI_ANY_ID,
  87. .driver_data = MODEL_WORKBIT,
  88. },
  89. {
  90. .vendor = PCI_VENDOR_ID_WORKBIT,
  91. .device = PCI_DEVICE_ID_WORKBIT_STANDARD,
  92. .subvendor = PCI_ANY_ID,
  93. .subdevice = PCI_ANY_ID,
  94. .driver_data = MODEL_PCI_WORKBIT,
  95. },
  96. {
  97. .vendor = PCI_VENDOR_ID_WORKBIT,
  98. .device = PCI_DEVICE_ID_NINJASCSI_32BI_LOGITEC,
  99. .subvendor = PCI_ANY_ID,
  100. .subdevice = PCI_ANY_ID,
  101. .driver_data = MODEL_LOGITEC,
  102. },
  103. {
  104. .vendor = PCI_VENDOR_ID_WORKBIT,
  105. .device = PCI_DEVICE_ID_NINJASCSI_32BIB_LOGITEC,
  106. .subvendor = PCI_ANY_ID,
  107. .subdevice = PCI_ANY_ID,
  108. .driver_data = MODEL_PCI_LOGITEC,
  109. },
  110. {
  111. .vendor = PCI_VENDOR_ID_WORKBIT,
  112. .device = PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO,
  113. .subvendor = PCI_ANY_ID,
  114. .subdevice = PCI_ANY_ID,
  115. .driver_data = MODEL_PCI_MELCO,
  116. },
  117. {
  118. .vendor = PCI_VENDOR_ID_WORKBIT,
  119. .device = PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO_II,
  120. .subvendor = PCI_ANY_ID,
  121. .subdevice = PCI_ANY_ID,
  122. .driver_data = MODEL_PCI_MELCO,
  123. },
  124. {0,0,},
  125. };
  126. MODULE_DEVICE_TABLE(pci, nsp32_pci_table);
  127. static nsp32_hw_data nsp32_data_base; /* probe <-> detect glue */
  128. /*
  129. * Period/AckWidth speed conversion table
  130. *
  131. * Note: This period/ackwidth speed table must be in descending order.
  132. */
  133. static nsp32_sync_table nsp32_sync_table_40M[] = {
  134. /* {PNo, AW, SP, EP, SREQ smpl} Speed(MB/s) Period AckWidth */
  135. {0x1, 0, 0x0c, 0x0c, SMPL_40M}, /* 20.0 : 50ns, 25ns */
  136. {0x2, 0, 0x0d, 0x18, SMPL_40M}, /* 13.3 : 75ns, 25ns */
  137. {0x3, 1, 0x19, 0x19, SMPL_40M}, /* 10.0 : 100ns, 50ns */
  138. {0x4, 1, 0x1a, 0x1f, SMPL_20M}, /* 8.0 : 125ns, 50ns */
  139. {0x5, 2, 0x20, 0x25, SMPL_20M}, /* 6.7 : 150ns, 75ns */
  140. {0x6, 2, 0x26, 0x31, SMPL_20M}, /* 5.7 : 175ns, 75ns */
  141. {0x7, 3, 0x32, 0x32, SMPL_20M}, /* 5.0 : 200ns, 100ns */
  142. {0x8, 3, 0x33, 0x38, SMPL_10M}, /* 4.4 : 225ns, 100ns */
  143. {0x9, 3, 0x39, 0x3e, SMPL_10M}, /* 4.0 : 250ns, 100ns */
  144. };
  145. static nsp32_sync_table nsp32_sync_table_20M[] = {
  146. {0x1, 0, 0x19, 0x19, SMPL_40M}, /* 10.0 : 100ns, 50ns */
  147. {0x2, 0, 0x1a, 0x25, SMPL_20M}, /* 6.7 : 150ns, 50ns */
  148. {0x3, 1, 0x26, 0x32, SMPL_20M}, /* 5.0 : 200ns, 100ns */
  149. {0x4, 1, 0x33, 0x3e, SMPL_10M}, /* 4.0 : 250ns, 100ns */
  150. {0x5, 2, 0x3f, 0x4b, SMPL_10M}, /* 3.3 : 300ns, 150ns */
  151. {0x6, 2, 0x4c, 0x57, SMPL_10M}, /* 2.8 : 350ns, 150ns */
  152. {0x7, 3, 0x58, 0x64, SMPL_10M}, /* 2.5 : 400ns, 200ns */
  153. {0x8, 3, 0x65, 0x70, SMPL_10M}, /* 2.2 : 450ns, 200ns */
  154. {0x9, 3, 0x71, 0x7d, SMPL_10M}, /* 2.0 : 500ns, 200ns */
  155. };
  156. static nsp32_sync_table nsp32_sync_table_pci[] = {
  157. {0x1, 0, 0x0c, 0x0f, SMPL_40M}, /* 16.6 : 60ns, 30ns */
  158. {0x2, 0, 0x10, 0x16, SMPL_40M}, /* 11.1 : 90ns, 30ns */
  159. {0x3, 1, 0x17, 0x1e, SMPL_20M}, /* 8.3 : 120ns, 60ns */
  160. {0x4, 1, 0x1f, 0x25, SMPL_20M}, /* 6.7 : 150ns, 60ns */
  161. {0x5, 2, 0x26, 0x2d, SMPL_20M}, /* 5.6 : 180ns, 90ns */
  162. {0x6, 2, 0x2e, 0x34, SMPL_10M}, /* 4.8 : 210ns, 90ns */
  163. {0x7, 3, 0x35, 0x3c, SMPL_10M}, /* 4.2 : 240ns, 120ns */
  164. {0x8, 3, 0x3d, 0x43, SMPL_10M}, /* 3.7 : 270ns, 120ns */
  165. {0x9, 3, 0x44, 0x4b, SMPL_10M}, /* 3.3 : 300ns, 120ns */
  166. };
  167. /*
  168. * function declaration
  169. */
  170. /* module entry point */
  171. static int nsp32_probe (struct pci_dev *, const struct pci_device_id *);
  172. static void nsp32_remove(struct pci_dev *);
  173. static int __init init_nsp32 (void);
  174. static void __exit exit_nsp32 (void);
  175. /* struct struct scsi_host_template */
  176. static int nsp32_show_info (struct seq_file *, struct Scsi_Host *);
  177. static int nsp32_detect (struct pci_dev *pdev);
  178. static int nsp32_queuecommand(struct Scsi_Host *, struct scsi_cmnd *);
  179. static const char *nsp32_info (struct Scsi_Host *);
  180. static int nsp32_release (struct Scsi_Host *);
  181. /* SCSI error handler */
  182. static int nsp32_eh_abort (struct scsi_cmnd *);
  183. static int nsp32_eh_bus_reset (struct scsi_cmnd *);
  184. static int nsp32_eh_host_reset(struct scsi_cmnd *);
  185. /* generate SCSI message */
  186. static void nsp32_build_identify(struct scsi_cmnd *);
  187. static void nsp32_build_nop (struct scsi_cmnd *);
  188. static void nsp32_build_reject (struct scsi_cmnd *);
  189. static void nsp32_build_sdtr (struct scsi_cmnd *, unsigned char, unsigned char);
  190. /* SCSI message handler */
  191. static int nsp32_busfree_occur(struct scsi_cmnd *, unsigned short);
  192. static void nsp32_msgout_occur (struct scsi_cmnd *);
  193. static void nsp32_msgin_occur (struct scsi_cmnd *, unsigned long, unsigned short);
  194. static int nsp32_setup_sg_table (struct scsi_cmnd *);
  195. static int nsp32_selection_autopara(struct scsi_cmnd *);
  196. static int nsp32_selection_autoscsi(struct scsi_cmnd *);
  197. static void nsp32_scsi_done (struct scsi_cmnd *);
  198. static int nsp32_arbitration (struct scsi_cmnd *, unsigned int);
  199. static int nsp32_reselection (struct scsi_cmnd *, unsigned char);
  200. static void nsp32_adjust_busfree (struct scsi_cmnd *, unsigned int);
  201. static void nsp32_restart_autoscsi (struct scsi_cmnd *, unsigned short);
  202. /* SCSI SDTR */
  203. static void nsp32_analyze_sdtr (struct scsi_cmnd *);
  204. static int nsp32_search_period_entry(nsp32_hw_data *, nsp32_target *, unsigned char);
  205. static void nsp32_set_async (nsp32_hw_data *, nsp32_target *);
  206. static void nsp32_set_max_sync (nsp32_hw_data *, nsp32_target *, unsigned char *, unsigned char *);
  207. static void nsp32_set_sync_entry (nsp32_hw_data *, nsp32_target *, int, unsigned char);
  208. /* SCSI bus status handler */
  209. static void nsp32_wait_req (nsp32_hw_data *, int);
  210. static void nsp32_wait_sack (nsp32_hw_data *, int);
  211. static void nsp32_sack_assert (nsp32_hw_data *);
  212. static void nsp32_sack_negate (nsp32_hw_data *);
  213. static void nsp32_do_bus_reset(nsp32_hw_data *);
  214. /* hardware interrupt handler */
  215. static irqreturn_t do_nsp32_isr(int, void *);
  216. /* initialize hardware */
  217. static int nsp32hw_init(nsp32_hw_data *);
  218. /* EEPROM handler */
  219. static int nsp32_getprom_param (nsp32_hw_data *);
  220. static int nsp32_getprom_at24 (nsp32_hw_data *);
  221. static int nsp32_getprom_c16 (nsp32_hw_data *);
  222. static void nsp32_prom_start (nsp32_hw_data *);
  223. static void nsp32_prom_stop (nsp32_hw_data *);
  224. static int nsp32_prom_read (nsp32_hw_data *, int);
  225. static int nsp32_prom_read_bit (nsp32_hw_data *);
  226. static void nsp32_prom_write_bit(nsp32_hw_data *, int);
  227. static void nsp32_prom_set (nsp32_hw_data *, int, int);
  228. static int nsp32_prom_get (nsp32_hw_data *, int);
  229. /* debug/warning/info message */
  230. static void nsp32_message (const char *, int, char *, char *, ...);
  231. #ifdef NSP32_DEBUG
  232. static void nsp32_dmessage(const char *, int, int, char *, ...);
  233. #endif
  234. /*
  235. * max_sectors is currently limited up to 128.
  236. */
  237. static struct scsi_host_template nsp32_template = {
  238. .proc_name = "nsp32",
  239. .name = "Workbit NinjaSCSI-32Bi/UDE",
  240. .show_info = nsp32_show_info,
  241. .info = nsp32_info,
  242. .queuecommand = nsp32_queuecommand,
  243. .can_queue = 1,
  244. .sg_tablesize = NSP32_SG_SIZE,
  245. .max_sectors = 128,
  246. .this_id = NSP32_HOST_SCSIID,
  247. .use_clustering = DISABLE_CLUSTERING,
  248. .eh_abort_handler = nsp32_eh_abort,
  249. .eh_bus_reset_handler = nsp32_eh_bus_reset,
  250. .eh_host_reset_handler = nsp32_eh_host_reset,
  251. /* .highmem_io = 1, */
  252. };
  253. #include "nsp32_io.h"
  254. /***********************************************************************
  255. * debug, error print
  256. */
  257. #ifndef NSP32_DEBUG
  258. # define NSP32_DEBUG_MASK 0x000000
  259. # define nsp32_msg(type, args...) nsp32_message ("", 0, (type), args)
  260. # define nsp32_dbg(mask, args...) /* */
  261. #else
  262. # define NSP32_DEBUG_MASK 0xffffff
  263. # define nsp32_msg(type, args...) \
  264. nsp32_message (__func__, __LINE__, (type), args)
  265. # define nsp32_dbg(mask, args...) \
  266. nsp32_dmessage(__func__, __LINE__, (mask), args)
  267. #endif
  268. #define NSP32_DEBUG_QUEUECOMMAND BIT(0)
  269. #define NSP32_DEBUG_REGISTER BIT(1)
  270. #define NSP32_DEBUG_AUTOSCSI BIT(2)
  271. #define NSP32_DEBUG_INTR BIT(3)
  272. #define NSP32_DEBUG_SGLIST BIT(4)
  273. #define NSP32_DEBUG_BUSFREE BIT(5)
  274. #define NSP32_DEBUG_CDB_CONTENTS BIT(6)
  275. #define NSP32_DEBUG_RESELECTION BIT(7)
  276. #define NSP32_DEBUG_MSGINOCCUR BIT(8)
  277. #define NSP32_DEBUG_EEPROM BIT(9)
  278. #define NSP32_DEBUG_MSGOUTOCCUR BIT(10)
  279. #define NSP32_DEBUG_BUSRESET BIT(11)
  280. #define NSP32_DEBUG_RESTART BIT(12)
  281. #define NSP32_DEBUG_SYNC BIT(13)
  282. #define NSP32_DEBUG_WAIT BIT(14)
  283. #define NSP32_DEBUG_TARGETFLAG BIT(15)
  284. #define NSP32_DEBUG_PROC BIT(16)
  285. #define NSP32_DEBUG_INIT BIT(17)
  286. #define NSP32_SPECIAL_PRINT_REGISTER BIT(20)
  287. #define NSP32_DEBUG_BUF_LEN 100
  288. static void nsp32_message(const char *func, int line, char *type, char *fmt, ...)
  289. {
  290. va_list args;
  291. char buf[NSP32_DEBUG_BUF_LEN];
  292. va_start(args, fmt);
  293. vsnprintf(buf, sizeof(buf), fmt, args);
  294. va_end(args);
  295. #ifndef NSP32_DEBUG
  296. printk("%snsp32: %s\n", type, buf);
  297. #else
  298. printk("%snsp32: %s (%d): %s\n", type, func, line, buf);
  299. #endif
  300. }
  301. #ifdef NSP32_DEBUG
  302. static void nsp32_dmessage(const char *func, int line, int mask, char *fmt, ...)
  303. {
  304. va_list args;
  305. char buf[NSP32_DEBUG_BUF_LEN];
  306. va_start(args, fmt);
  307. vsnprintf(buf, sizeof(buf), fmt, args);
  308. va_end(args);
  309. if (mask & NSP32_DEBUG_MASK) {
  310. printk("nsp32-debug: 0x%x %s (%d): %s\n", mask, func, line, buf);
  311. }
  312. }
  313. #endif
  314. #ifdef NSP32_DEBUG
  315. # include "nsp32_debug.c"
  316. #else
  317. # define show_command(arg) /* */
  318. # define show_busphase(arg) /* */
  319. # define show_autophase(arg) /* */
  320. #endif
  321. /*
  322. * IDENTIFY Message
  323. */
  324. static void nsp32_build_identify(struct scsi_cmnd *SCpnt)
  325. {
  326. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  327. int pos = data->msgout_len;
  328. int mode = FALSE;
  329. /* XXX: Auto DiscPriv detection is progressing... */
  330. if (disc_priv == 0) {
  331. /* mode = TRUE; */
  332. }
  333. data->msgoutbuf[pos] = IDENTIFY(mode, SCpnt->device->lun); pos++;
  334. data->msgout_len = pos;
  335. }
  336. /*
  337. * SDTR Message Routine
  338. */
  339. static void nsp32_build_sdtr(struct scsi_cmnd *SCpnt,
  340. unsigned char period,
  341. unsigned char offset)
  342. {
  343. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  344. int pos = data->msgout_len;
  345. data->msgoutbuf[pos] = EXTENDED_MESSAGE; pos++;
  346. data->msgoutbuf[pos] = EXTENDED_SDTR_LEN; pos++;
  347. data->msgoutbuf[pos] = EXTENDED_SDTR; pos++;
  348. data->msgoutbuf[pos] = period; pos++;
  349. data->msgoutbuf[pos] = offset; pos++;
  350. data->msgout_len = pos;
  351. }
  352. /*
  353. * No Operation Message
  354. */
  355. static void nsp32_build_nop(struct scsi_cmnd *SCpnt)
  356. {
  357. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  358. int pos = data->msgout_len;
  359. if (pos != 0) {
  360. nsp32_msg(KERN_WARNING,
  361. "Some messages are already contained!");
  362. return;
  363. }
  364. data->msgoutbuf[pos] = NOP; pos++;
  365. data->msgout_len = pos;
  366. }
  367. /*
  368. * Reject Message
  369. */
  370. static void nsp32_build_reject(struct scsi_cmnd *SCpnt)
  371. {
  372. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  373. int pos = data->msgout_len;
  374. data->msgoutbuf[pos] = MESSAGE_REJECT; pos++;
  375. data->msgout_len = pos;
  376. }
  377. /*
  378. * timer
  379. */
  380. #if 0
  381. static void nsp32_start_timer(struct scsi_cmnd *SCpnt, int time)
  382. {
  383. unsigned int base = SCpnt->host->io_port;
  384. nsp32_dbg(NSP32_DEBUG_INTR, "timer=%d", time);
  385. if (time & (~TIMER_CNT_MASK)) {
  386. nsp32_dbg(NSP32_DEBUG_INTR, "timer set overflow");
  387. }
  388. nsp32_write2(base, TIMER_SET, time & TIMER_CNT_MASK);
  389. }
  390. #endif
  391. /*
  392. * set SCSI command and other parameter to asic, and start selection phase
  393. */
  394. static int nsp32_selection_autopara(struct scsi_cmnd *SCpnt)
  395. {
  396. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  397. unsigned int base = SCpnt->device->host->io_port;
  398. unsigned int host_id = SCpnt->device->host->this_id;
  399. unsigned char target = scmd_id(SCpnt);
  400. nsp32_autoparam *param = data->autoparam;
  401. unsigned char phase;
  402. int i, ret;
  403. unsigned int msgout;
  404. u16_le s;
  405. nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "in");
  406. /*
  407. * check bus free
  408. */
  409. phase = nsp32_read1(base, SCSI_BUS_MONITOR);
  410. if (phase != BUSMON_BUS_FREE) {
  411. nsp32_msg(KERN_WARNING, "bus busy");
  412. show_busphase(phase & BUSMON_PHASE_MASK);
  413. SCpnt->result = DID_BUS_BUSY << 16;
  414. return FALSE;
  415. }
  416. /*
  417. * message out
  418. *
  419. * Note: If the range of msgout_len is 1 - 3, fill scsi_msgout.
  420. * over 3 messages needs another routine.
  421. */
  422. if (data->msgout_len == 0) {
  423. nsp32_msg(KERN_ERR, "SCSI MsgOut without any message!");
  424. SCpnt->result = DID_ERROR << 16;
  425. return FALSE;
  426. } else if (data->msgout_len > 0 && data->msgout_len <= 3) {
  427. msgout = 0;
  428. for (i = 0; i < data->msgout_len; i++) {
  429. /*
  430. * the sending order of the message is:
  431. * MCNT 3: MSG#0 -> MSG#1 -> MSG#2
  432. * MCNT 2: MSG#1 -> MSG#2
  433. * MCNT 1: MSG#2
  434. */
  435. msgout >>= 8;
  436. msgout |= ((unsigned int)(data->msgoutbuf[i]) << 24);
  437. }
  438. msgout |= MV_VALID; /* MV valid */
  439. msgout |= (unsigned int)data->msgout_len; /* len */
  440. } else {
  441. /* data->msgout_len > 3 */
  442. msgout = 0;
  443. }
  444. // nsp_dbg(NSP32_DEBUG_AUTOSCSI, "sel time out=0x%x\n", nsp32_read2(base, SEL_TIME_OUT));
  445. // nsp32_write2(base, SEL_TIME_OUT, SEL_TIMEOUT_TIME);
  446. /*
  447. * setup asic parameter
  448. */
  449. memset(param, 0, sizeof(nsp32_autoparam));
  450. /* cdb */
  451. for (i = 0; i < SCpnt->cmd_len; i++) {
  452. param->cdb[4 * i] = SCpnt->cmnd[i];
  453. }
  454. /* outgoing messages */
  455. param->msgout = cpu_to_le32(msgout);
  456. /* syncreg, ackwidth, target id, SREQ sampling rate */
  457. param->syncreg = data->cur_target->syncreg;
  458. param->ackwidth = data->cur_target->ackwidth;
  459. param->target_id = BIT(host_id) | BIT(target);
  460. param->sample_reg = data->cur_target->sample_reg;
  461. // nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "sample rate=0x%x\n", data->cur_target->sample_reg);
  462. /* command control */
  463. param->command_control = cpu_to_le16(CLEAR_CDB_FIFO_POINTER |
  464. AUTOSCSI_START |
  465. AUTO_MSGIN_00_OR_04 |
  466. AUTO_MSGIN_02 |
  467. AUTO_ATN );
  468. /* transfer control */
  469. s = 0;
  470. switch (data->trans_method) {
  471. case NSP32_TRANSFER_BUSMASTER:
  472. s |= BM_START;
  473. break;
  474. case NSP32_TRANSFER_MMIO:
  475. s |= CB_MMIO_MODE;
  476. break;
  477. case NSP32_TRANSFER_PIO:
  478. s |= CB_IO_MODE;
  479. break;
  480. default:
  481. nsp32_msg(KERN_ERR, "unknown trans_method");
  482. break;
  483. }
  484. /*
  485. * OR-ed BLIEND_MODE, FIFO intr is decreased, instead of PCI bus waits.
  486. * For bus master transfer, it's taken off.
  487. */
  488. s |= (TRANSFER_GO | ALL_COUNTER_CLR);
  489. param->transfer_control = cpu_to_le16(s);
  490. /* sg table addr */
  491. param->sgt_pointer = cpu_to_le32(data->cur_lunt->sglun_paddr);
  492. /*
  493. * transfer parameter to ASIC
  494. */
  495. nsp32_write4(base, SGT_ADR, data->auto_paddr);
  496. nsp32_write2(base, COMMAND_CONTROL, CLEAR_CDB_FIFO_POINTER |
  497. AUTO_PARAMETER );
  498. /*
  499. * Check arbitration
  500. */
  501. ret = nsp32_arbitration(SCpnt, base);
  502. return ret;
  503. }
  504. /*
  505. * Selection with AUTO SCSI (without AUTO PARAMETER)
  506. */
  507. static int nsp32_selection_autoscsi(struct scsi_cmnd *SCpnt)
  508. {
  509. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  510. unsigned int base = SCpnt->device->host->io_port;
  511. unsigned int host_id = SCpnt->device->host->this_id;
  512. unsigned char target = scmd_id(SCpnt);
  513. unsigned char phase;
  514. int status;
  515. unsigned short command = 0;
  516. unsigned int msgout = 0;
  517. unsigned short execph;
  518. int i;
  519. nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "in");
  520. /*
  521. * IRQ disable
  522. */
  523. nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
  524. /*
  525. * check bus line
  526. */
  527. phase = nsp32_read1(base, SCSI_BUS_MONITOR);
  528. if(((phase & BUSMON_BSY) == 1) || (phase & BUSMON_SEL) == 1) {
  529. nsp32_msg(KERN_WARNING, "bus busy");
  530. SCpnt->result = DID_BUS_BUSY << 16;
  531. status = 1;
  532. goto out;
  533. }
  534. /*
  535. * clear execph
  536. */
  537. execph = nsp32_read2(base, SCSI_EXECUTE_PHASE);
  538. /*
  539. * clear FIFO counter to set CDBs
  540. */
  541. nsp32_write2(base, COMMAND_CONTROL, CLEAR_CDB_FIFO_POINTER);
  542. /*
  543. * set CDB0 - CDB15
  544. */
  545. for (i = 0; i < SCpnt->cmd_len; i++) {
  546. nsp32_write1(base, COMMAND_DATA, SCpnt->cmnd[i]);
  547. }
  548. nsp32_dbg(NSP32_DEBUG_CDB_CONTENTS, "CDB[0]=[0x%x]", SCpnt->cmnd[0]);
  549. /*
  550. * set SCSIOUT LATCH(initiator)/TARGET(target) (OR-ed) ID
  551. */
  552. nsp32_write1(base, SCSI_OUT_LATCH_TARGET_ID, BIT(host_id) | BIT(target));
  553. /*
  554. * set SCSI MSGOUT REG
  555. *
  556. * Note: If the range of msgout_len is 1 - 3, fill scsi_msgout.
  557. * over 3 messages needs another routine.
  558. */
  559. if (data->msgout_len == 0) {
  560. nsp32_msg(KERN_ERR, "SCSI MsgOut without any message!");
  561. SCpnt->result = DID_ERROR << 16;
  562. status = 1;
  563. goto out;
  564. } else if (data->msgout_len > 0 && data->msgout_len <= 3) {
  565. msgout = 0;
  566. for (i = 0; i < data->msgout_len; i++) {
  567. /*
  568. * the sending order of the message is:
  569. * MCNT 3: MSG#0 -> MSG#1 -> MSG#2
  570. * MCNT 2: MSG#1 -> MSG#2
  571. * MCNT 1: MSG#2
  572. */
  573. msgout >>= 8;
  574. msgout |= ((unsigned int)(data->msgoutbuf[i]) << 24);
  575. }
  576. msgout |= MV_VALID; /* MV valid */
  577. msgout |= (unsigned int)data->msgout_len; /* len */
  578. nsp32_write4(base, SCSI_MSG_OUT, msgout);
  579. } else {
  580. /* data->msgout_len > 3 */
  581. nsp32_write4(base, SCSI_MSG_OUT, 0);
  582. }
  583. /*
  584. * set selection timeout(= 250ms)
  585. */
  586. nsp32_write2(base, SEL_TIME_OUT, SEL_TIMEOUT_TIME);
  587. /*
  588. * set SREQ hazard killer sampling rate
  589. *
  590. * TODO: sample_rate (BASE+0F) is 0 when internal clock = 40MHz.
  591. * check other internal clock!
  592. */
  593. nsp32_write1(base, SREQ_SMPL_RATE, data->cur_target->sample_reg);
  594. /*
  595. * clear Arbit
  596. */
  597. nsp32_write1(base, SET_ARBIT, ARBIT_CLEAR);
  598. /*
  599. * set SYNCREG
  600. * Don't set BM_START_ADR before setting this register.
  601. */
  602. nsp32_write1(base, SYNC_REG, data->cur_target->syncreg);
  603. /*
  604. * set ACKWIDTH
  605. */
  606. nsp32_write1(base, ACK_WIDTH, data->cur_target->ackwidth);
  607. nsp32_dbg(NSP32_DEBUG_AUTOSCSI,
  608. "syncreg=0x%x, ackwidth=0x%x, sgtpaddr=0x%x, id=0x%x",
  609. nsp32_read1(base, SYNC_REG), nsp32_read1(base, ACK_WIDTH),
  610. nsp32_read4(base, SGT_ADR), nsp32_read1(base, SCSI_OUT_LATCH_TARGET_ID));
  611. nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "msgout_len=%d, msgout=0x%x",
  612. data->msgout_len, msgout);
  613. /*
  614. * set SGT ADDR (physical address)
  615. */
  616. nsp32_write4(base, SGT_ADR, data->cur_lunt->sglun_paddr);
  617. /*
  618. * set TRANSFER CONTROL REG
  619. */
  620. command = 0;
  621. command |= (TRANSFER_GO | ALL_COUNTER_CLR);
  622. if (data->trans_method & NSP32_TRANSFER_BUSMASTER) {
  623. if (scsi_bufflen(SCpnt) > 0) {
  624. command |= BM_START;
  625. }
  626. } else if (data->trans_method & NSP32_TRANSFER_MMIO) {
  627. command |= CB_MMIO_MODE;
  628. } else if (data->trans_method & NSP32_TRANSFER_PIO) {
  629. command |= CB_IO_MODE;
  630. }
  631. nsp32_write2(base, TRANSFER_CONTROL, command);
  632. /*
  633. * start AUTO SCSI, kick off arbitration
  634. */
  635. command = (CLEAR_CDB_FIFO_POINTER |
  636. AUTOSCSI_START |
  637. AUTO_MSGIN_00_OR_04 |
  638. AUTO_MSGIN_02 |
  639. AUTO_ATN );
  640. nsp32_write2(base, COMMAND_CONTROL, command);
  641. /*
  642. * Check arbitration
  643. */
  644. status = nsp32_arbitration(SCpnt, base);
  645. out:
  646. /*
  647. * IRQ enable
  648. */
  649. nsp32_write2(base, IRQ_CONTROL, 0);
  650. return status;
  651. }
  652. /*
  653. * Arbitration Status Check
  654. *
  655. * Note: Arbitration counter is waited during ARBIT_GO is not lifting.
  656. * Using udelay(1) consumes CPU time and system time, but
  657. * arbitration delay time is defined minimal 2.4us in SCSI
  658. * specification, thus udelay works as coarse grained wait timer.
  659. */
  660. static int nsp32_arbitration(struct scsi_cmnd *SCpnt, unsigned int base)
  661. {
  662. unsigned char arbit;
  663. int status = TRUE;
  664. int time = 0;
  665. do {
  666. arbit = nsp32_read1(base, ARBIT_STATUS);
  667. time++;
  668. } while ((arbit & (ARBIT_WIN | ARBIT_FAIL)) == 0 &&
  669. (time <= ARBIT_TIMEOUT_TIME));
  670. nsp32_dbg(NSP32_DEBUG_AUTOSCSI,
  671. "arbit: 0x%x, delay time: %d", arbit, time);
  672. if (arbit & ARBIT_WIN) {
  673. /* Arbitration succeeded */
  674. SCpnt->result = DID_OK << 16;
  675. nsp32_index_write1(base, EXT_PORT, LED_ON); /* PCI LED on */
  676. } else if (arbit & ARBIT_FAIL) {
  677. /* Arbitration failed */
  678. SCpnt->result = DID_BUS_BUSY << 16;
  679. status = FALSE;
  680. } else {
  681. /*
  682. * unknown error or ARBIT_GO timeout,
  683. * something lock up! guess no connection.
  684. */
  685. nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "arbit timeout");
  686. SCpnt->result = DID_NO_CONNECT << 16;
  687. status = FALSE;
  688. }
  689. /*
  690. * clear Arbit
  691. */
  692. nsp32_write1(base, SET_ARBIT, ARBIT_CLEAR);
  693. return status;
  694. }
  695. /*
  696. * reselection
  697. *
  698. * Note: This reselection routine is called from msgin_occur,
  699. * reselection target id&lun must be already set.
  700. * SCSI-2 says IDENTIFY implies RESTORE_POINTER operation.
  701. */
  702. static int nsp32_reselection(struct scsi_cmnd *SCpnt, unsigned char newlun)
  703. {
  704. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  705. unsigned int host_id = SCpnt->device->host->this_id;
  706. unsigned int base = SCpnt->device->host->io_port;
  707. unsigned char tmpid, newid;
  708. nsp32_dbg(NSP32_DEBUG_RESELECTION, "enter");
  709. /*
  710. * calculate reselected SCSI ID
  711. */
  712. tmpid = nsp32_read1(base, RESELECT_ID);
  713. tmpid &= (~BIT(host_id));
  714. newid = 0;
  715. while (tmpid) {
  716. if (tmpid & 1) {
  717. break;
  718. }
  719. tmpid >>= 1;
  720. newid++;
  721. }
  722. /*
  723. * If reselected New ID:LUN is not existed
  724. * or current nexus is not existed, unexpected
  725. * reselection is occurred. Send reject message.
  726. */
  727. if (newid >= ARRAY_SIZE(data->lunt) || newlun >= ARRAY_SIZE(data->lunt[0])) {
  728. nsp32_msg(KERN_WARNING, "unknown id/lun");
  729. return FALSE;
  730. } else if(data->lunt[newid][newlun].SCpnt == NULL) {
  731. nsp32_msg(KERN_WARNING, "no SCSI command is processing");
  732. return FALSE;
  733. }
  734. data->cur_id = newid;
  735. data->cur_lun = newlun;
  736. data->cur_target = &(data->target[newid]);
  737. data->cur_lunt = &(data->lunt[newid][newlun]);
  738. /* reset SACK/SavedACK counter (or ALL clear?) */
  739. nsp32_write4(base, CLR_COUNTER, CLRCOUNTER_ALLMASK);
  740. return TRUE;
  741. }
  742. /*
  743. * nsp32_setup_sg_table - build scatter gather list for transfer data
  744. * with bus master.
  745. *
  746. * Note: NinjaSCSI-32Bi/UDE bus master can not transfer over 64KB at a time.
  747. */
  748. static int nsp32_setup_sg_table(struct scsi_cmnd *SCpnt)
  749. {
  750. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  751. struct scatterlist *sg;
  752. nsp32_sgtable *sgt = data->cur_lunt->sglun->sgt;
  753. int num, i;
  754. u32_le l;
  755. if (sgt == NULL) {
  756. nsp32_dbg(NSP32_DEBUG_SGLIST, "SGT == null");
  757. return FALSE;
  758. }
  759. num = scsi_dma_map(SCpnt);
  760. if (!num)
  761. return TRUE;
  762. else if (num < 0)
  763. return FALSE;
  764. else {
  765. scsi_for_each_sg(SCpnt, sg, num, i) {
  766. /*
  767. * Build nsp32_sglist, substitute sg dma addresses.
  768. */
  769. sgt[i].addr = cpu_to_le32(sg_dma_address(sg));
  770. sgt[i].len = cpu_to_le32(sg_dma_len(sg));
  771. if (le32_to_cpu(sgt[i].len) > 0x10000) {
  772. nsp32_msg(KERN_ERR,
  773. "can't transfer over 64KB at a time, size=0x%lx", le32_to_cpu(sgt[i].len));
  774. return FALSE;
  775. }
  776. nsp32_dbg(NSP32_DEBUG_SGLIST,
  777. "num 0x%x : addr 0x%lx len 0x%lx",
  778. i,
  779. le32_to_cpu(sgt[i].addr),
  780. le32_to_cpu(sgt[i].len ));
  781. }
  782. /* set end mark */
  783. l = le32_to_cpu(sgt[num-1].len);
  784. sgt[num-1].len = cpu_to_le32(l | SGTEND);
  785. }
  786. return TRUE;
  787. }
  788. static int nsp32_queuecommand_lck(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  789. {
  790. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  791. nsp32_target *target;
  792. nsp32_lunt *cur_lunt;
  793. int ret;
  794. nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
  795. "enter. target: 0x%x LUN: 0x%llx cmnd: 0x%x cmndlen: 0x%x "
  796. "use_sg: 0x%x reqbuf: 0x%lx reqlen: 0x%x",
  797. SCpnt->device->id, SCpnt->device->lun, SCpnt->cmnd[0], SCpnt->cmd_len,
  798. scsi_sg_count(SCpnt), scsi_sglist(SCpnt), scsi_bufflen(SCpnt));
  799. if (data->CurrentSC != NULL) {
  800. nsp32_msg(KERN_ERR, "Currentsc != NULL. Cancel this command request");
  801. data->CurrentSC = NULL;
  802. SCpnt->result = DID_NO_CONNECT << 16;
  803. done(SCpnt);
  804. return 0;
  805. }
  806. /* check target ID is not same as this initiator ID */
  807. if (scmd_id(SCpnt) == SCpnt->device->host->this_id) {
  808. nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND, "target==host???");
  809. SCpnt->result = DID_BAD_TARGET << 16;
  810. done(SCpnt);
  811. return 0;
  812. }
  813. /* check target LUN is allowable value */
  814. if (SCpnt->device->lun >= MAX_LUN) {
  815. nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND, "no more lun");
  816. SCpnt->result = DID_BAD_TARGET << 16;
  817. done(SCpnt);
  818. return 0;
  819. }
  820. show_command(SCpnt);
  821. SCpnt->scsi_done = done;
  822. data->CurrentSC = SCpnt;
  823. SCpnt->SCp.Status = CHECK_CONDITION;
  824. SCpnt->SCp.Message = 0;
  825. scsi_set_resid(SCpnt, scsi_bufflen(SCpnt));
  826. SCpnt->SCp.ptr = (char *)scsi_sglist(SCpnt);
  827. SCpnt->SCp.this_residual = scsi_bufflen(SCpnt);
  828. SCpnt->SCp.buffer = NULL;
  829. SCpnt->SCp.buffers_residual = 0;
  830. /* initialize data */
  831. data->msgout_len = 0;
  832. data->msgin_len = 0;
  833. cur_lunt = &(data->lunt[SCpnt->device->id][SCpnt->device->lun]);
  834. cur_lunt->SCpnt = SCpnt;
  835. cur_lunt->save_datp = 0;
  836. cur_lunt->msgin03 = FALSE;
  837. data->cur_lunt = cur_lunt;
  838. data->cur_id = SCpnt->device->id;
  839. data->cur_lun = SCpnt->device->lun;
  840. ret = nsp32_setup_sg_table(SCpnt);
  841. if (ret == FALSE) {
  842. nsp32_msg(KERN_ERR, "SGT fail");
  843. SCpnt->result = DID_ERROR << 16;
  844. nsp32_scsi_done(SCpnt);
  845. return 0;
  846. }
  847. /* Build IDENTIFY */
  848. nsp32_build_identify(SCpnt);
  849. /*
  850. * If target is the first time to transfer after the reset
  851. * (target don't have SDTR_DONE and SDTR_INITIATOR), sync
  852. * message SDTR is needed to do synchronous transfer.
  853. */
  854. target = &data->target[scmd_id(SCpnt)];
  855. data->cur_target = target;
  856. if (!(target->sync_flag & (SDTR_DONE | SDTR_INITIATOR | SDTR_TARGET))) {
  857. unsigned char period, offset;
  858. if (trans_mode != ASYNC_MODE) {
  859. nsp32_set_max_sync(data, target, &period, &offset);
  860. nsp32_build_sdtr(SCpnt, period, offset);
  861. target->sync_flag |= SDTR_INITIATOR;
  862. } else {
  863. nsp32_set_async(data, target);
  864. target->sync_flag |= SDTR_DONE;
  865. }
  866. nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
  867. "SDTR: entry: %d start_period: 0x%x offset: 0x%x\n",
  868. target->limit_entry, period, offset);
  869. } else if (target->sync_flag & SDTR_INITIATOR) {
  870. /*
  871. * It was negotiating SDTR with target, sending from the
  872. * initiator, but there are no chance to remove this flag.
  873. * Set async because we don't get proper negotiation.
  874. */
  875. nsp32_set_async(data, target);
  876. target->sync_flag &= ~SDTR_INITIATOR;
  877. target->sync_flag |= SDTR_DONE;
  878. nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
  879. "SDTR_INITIATOR: fall back to async");
  880. } else if (target->sync_flag & SDTR_TARGET) {
  881. /*
  882. * It was negotiating SDTR with target, sending from target,
  883. * but there are no chance to remove this flag. Set async
  884. * because we don't get proper negotiation.
  885. */
  886. nsp32_set_async(data, target);
  887. target->sync_flag &= ~SDTR_TARGET;
  888. target->sync_flag |= SDTR_DONE;
  889. nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
  890. "Unknown SDTR from target is reached, fall back to async.");
  891. }
  892. nsp32_dbg(NSP32_DEBUG_TARGETFLAG,
  893. "target: %d sync_flag: 0x%x syncreg: 0x%x ackwidth: 0x%x",
  894. SCpnt->device->id, target->sync_flag, target->syncreg,
  895. target->ackwidth);
  896. /* Selection */
  897. if (auto_param == 0) {
  898. ret = nsp32_selection_autopara(SCpnt);
  899. } else {
  900. ret = nsp32_selection_autoscsi(SCpnt);
  901. }
  902. if (ret != TRUE) {
  903. nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND, "selection fail");
  904. nsp32_scsi_done(SCpnt);
  905. }
  906. return 0;
  907. }
  908. static DEF_SCSI_QCMD(nsp32_queuecommand)
  909. /* initialize asic */
  910. static int nsp32hw_init(nsp32_hw_data *data)
  911. {
  912. unsigned int base = data->BaseAddress;
  913. unsigned short irq_stat;
  914. unsigned long lc_reg;
  915. unsigned char power;
  916. lc_reg = nsp32_index_read4(base, CFG_LATE_CACHE);
  917. if ((lc_reg & 0xff00) == 0) {
  918. lc_reg |= (0x20 << 8);
  919. nsp32_index_write2(base, CFG_LATE_CACHE, lc_reg & 0xffff);
  920. }
  921. nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
  922. nsp32_write2(base, TRANSFER_CONTROL, 0);
  923. nsp32_write4(base, BM_CNT, 0);
  924. nsp32_write2(base, SCSI_EXECUTE_PHASE, 0);
  925. do {
  926. irq_stat = nsp32_read2(base, IRQ_STATUS);
  927. nsp32_dbg(NSP32_DEBUG_INIT, "irq_stat 0x%x", irq_stat);
  928. } while (irq_stat & IRQSTATUS_ANY_IRQ);
  929. /*
  930. * Fill FIFO_FULL_SHLD, FIFO_EMPTY_SHLD. Below parameter is
  931. * designated by specification.
  932. */
  933. if ((data->trans_method & NSP32_TRANSFER_PIO) ||
  934. (data->trans_method & NSP32_TRANSFER_MMIO)) {
  935. nsp32_index_write1(base, FIFO_FULL_SHLD_COUNT, 0x40);
  936. nsp32_index_write1(base, FIFO_EMPTY_SHLD_COUNT, 0x40);
  937. } else if (data->trans_method & NSP32_TRANSFER_BUSMASTER) {
  938. nsp32_index_write1(base, FIFO_FULL_SHLD_COUNT, 0x10);
  939. nsp32_index_write1(base, FIFO_EMPTY_SHLD_COUNT, 0x60);
  940. } else {
  941. nsp32_dbg(NSP32_DEBUG_INIT, "unknown transfer mode");
  942. }
  943. nsp32_dbg(NSP32_DEBUG_INIT, "full 0x%x emp 0x%x",
  944. nsp32_index_read1(base, FIFO_FULL_SHLD_COUNT),
  945. nsp32_index_read1(base, FIFO_EMPTY_SHLD_COUNT));
  946. nsp32_index_write1(base, CLOCK_DIV, data->clock);
  947. nsp32_index_write1(base, BM_CYCLE, MEMRD_CMD1 | SGT_AUTO_PARA_MEMED_CMD);
  948. nsp32_write1(base, PARITY_CONTROL, 0); /* parity check is disable */
  949. /*
  950. * initialize MISC_WRRD register
  951. *
  952. * Note: Designated parameters is obeyed as following:
  953. * MISC_SCSI_DIRECTION_DETECTOR_SELECT: It must be set.
  954. * MISC_MASTER_TERMINATION_SELECT: It must be set.
  955. * MISC_BMREQ_NEGATE_TIMING_SEL: It should be set.
  956. * MISC_AUTOSEL_TIMING_SEL: It should be set.
  957. * MISC_BMSTOP_CHANGE2_NONDATA_PHASE: It should be set.
  958. * MISC_DELAYED_BMSTART: It's selected for safety.
  959. *
  960. * Note: If MISC_BMSTOP_CHANGE2_NONDATA_PHASE is set, then
  961. * we have to set TRANSFERCONTROL_BM_START as 0 and set
  962. * appropriate value before restarting bus master transfer.
  963. */
  964. nsp32_index_write2(base, MISC_WR,
  965. (SCSI_DIRECTION_DETECTOR_SELECT |
  966. DELAYED_BMSTART |
  967. MASTER_TERMINATION_SELECT |
  968. BMREQ_NEGATE_TIMING_SEL |
  969. AUTOSEL_TIMING_SEL |
  970. BMSTOP_CHANGE2_NONDATA_PHASE));
  971. nsp32_index_write1(base, TERM_PWR_CONTROL, 0);
  972. power = nsp32_index_read1(base, TERM_PWR_CONTROL);
  973. if (!(power & SENSE)) {
  974. nsp32_msg(KERN_INFO, "term power on");
  975. nsp32_index_write1(base, TERM_PWR_CONTROL, BPWR);
  976. }
  977. nsp32_write2(base, TIMER_SET, TIMER_STOP);
  978. nsp32_write2(base, TIMER_SET, TIMER_STOP); /* Required 2 times */
  979. nsp32_write1(base, SYNC_REG, 0);
  980. nsp32_write1(base, ACK_WIDTH, 0);
  981. nsp32_write2(base, SEL_TIME_OUT, SEL_TIMEOUT_TIME);
  982. /*
  983. * enable to select designated IRQ (except for
  984. * IRQSELECT_SERR, IRQSELECT_PERR, IRQSELECT_BMCNTERR)
  985. */
  986. nsp32_index_write2(base, IRQ_SELECT, IRQSELECT_TIMER_IRQ |
  987. IRQSELECT_SCSIRESET_IRQ |
  988. IRQSELECT_FIFO_SHLD_IRQ |
  989. IRQSELECT_RESELECT_IRQ |
  990. IRQSELECT_PHASE_CHANGE_IRQ |
  991. IRQSELECT_AUTO_SCSI_SEQ_IRQ |
  992. // IRQSELECT_BMCNTERR_IRQ |
  993. IRQSELECT_TARGET_ABORT_IRQ |
  994. IRQSELECT_MASTER_ABORT_IRQ );
  995. nsp32_write2(base, IRQ_CONTROL, 0);
  996. /* PCI LED off */
  997. nsp32_index_write1(base, EXT_PORT_DDR, LED_OFF);
  998. nsp32_index_write1(base, EXT_PORT, LED_OFF);
  999. return TRUE;
  1000. }
  1001. /* interrupt routine */
  1002. static irqreturn_t do_nsp32_isr(int irq, void *dev_id)
  1003. {
  1004. nsp32_hw_data *data = dev_id;
  1005. unsigned int base = data->BaseAddress;
  1006. struct scsi_cmnd *SCpnt = data->CurrentSC;
  1007. unsigned short auto_stat, irq_stat, trans_stat;
  1008. unsigned char busmon, busphase;
  1009. unsigned long flags;
  1010. int ret;
  1011. int handled = 0;
  1012. struct Scsi_Host *host = data->Host;
  1013. spin_lock_irqsave(host->host_lock, flags);
  1014. /*
  1015. * IRQ check, then enable IRQ mask
  1016. */
  1017. irq_stat = nsp32_read2(base, IRQ_STATUS);
  1018. nsp32_dbg(NSP32_DEBUG_INTR,
  1019. "enter IRQ: %d, IRQstatus: 0x%x", irq, irq_stat);
  1020. /* is this interrupt comes from Ninja asic? */
  1021. if ((irq_stat & IRQSTATUS_ANY_IRQ) == 0) {
  1022. nsp32_dbg(NSP32_DEBUG_INTR, "shared interrupt: irq other 0x%x", irq_stat);
  1023. goto out2;
  1024. }
  1025. handled = 1;
  1026. nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
  1027. busmon = nsp32_read1(base, SCSI_BUS_MONITOR);
  1028. busphase = busmon & BUSMON_PHASE_MASK;
  1029. trans_stat = nsp32_read2(base, TRANSFER_STATUS);
  1030. if ((irq_stat == 0xffff) && (trans_stat == 0xffff)) {
  1031. nsp32_msg(KERN_INFO, "card disconnect");
  1032. if (data->CurrentSC != NULL) {
  1033. nsp32_msg(KERN_INFO, "clean up current SCSI command");
  1034. SCpnt->result = DID_BAD_TARGET << 16;
  1035. nsp32_scsi_done(SCpnt);
  1036. }
  1037. goto out;
  1038. }
  1039. /* Timer IRQ */
  1040. if (irq_stat & IRQSTATUS_TIMER_IRQ) {
  1041. nsp32_dbg(NSP32_DEBUG_INTR, "timer stop");
  1042. nsp32_write2(base, TIMER_SET, TIMER_STOP);
  1043. goto out;
  1044. }
  1045. /* SCSI reset */
  1046. if (irq_stat & IRQSTATUS_SCSIRESET_IRQ) {
  1047. nsp32_msg(KERN_INFO, "detected someone do bus reset");
  1048. nsp32_do_bus_reset(data);
  1049. if (SCpnt != NULL) {
  1050. SCpnt->result = DID_RESET << 16;
  1051. nsp32_scsi_done(SCpnt);
  1052. }
  1053. goto out;
  1054. }
  1055. if (SCpnt == NULL) {
  1056. nsp32_msg(KERN_WARNING, "SCpnt==NULL this can't be happened");
  1057. nsp32_msg(KERN_WARNING, "irq_stat=0x%x trans_stat=0x%x", irq_stat, trans_stat);
  1058. goto out;
  1059. }
  1060. /*
  1061. * AutoSCSI Interrupt.
  1062. * Note: This interrupt is occurred when AutoSCSI is finished. Then
  1063. * check SCSIEXECUTEPHASE, and do appropriate action. Each phases are
  1064. * recorded when AutoSCSI sequencer has been processed.
  1065. */
  1066. if(irq_stat & IRQSTATUS_AUTOSCSI_IRQ) {
  1067. /* getting SCSI executed phase */
  1068. auto_stat = nsp32_read2(base, SCSI_EXECUTE_PHASE);
  1069. nsp32_write2(base, SCSI_EXECUTE_PHASE, 0);
  1070. /* Selection Timeout, go busfree phase. */
  1071. if (auto_stat & SELECTION_TIMEOUT) {
  1072. nsp32_dbg(NSP32_DEBUG_INTR,
  1073. "selection timeout occurred");
  1074. SCpnt->result = DID_TIME_OUT << 16;
  1075. nsp32_scsi_done(SCpnt);
  1076. goto out;
  1077. }
  1078. if (auto_stat & MSGOUT_PHASE) {
  1079. /*
  1080. * MsgOut phase was processed.
  1081. * If MSG_IN_OCCUER is not set, then MsgOut phase is
  1082. * completed. Thus, msgout_len must reset. Otherwise,
  1083. * nothing to do here. If MSG_OUT_OCCUER is occurred,
  1084. * then we will encounter the condition and check.
  1085. */
  1086. if (!(auto_stat & MSG_IN_OCCUER) &&
  1087. (data->msgout_len <= 3)) {
  1088. /*
  1089. * !MSG_IN_OCCUER && msgout_len <=3
  1090. * ---> AutoSCSI with MSGOUTreg is processed.
  1091. */
  1092. data->msgout_len = 0;
  1093. };
  1094. nsp32_dbg(NSP32_DEBUG_INTR, "MsgOut phase processed");
  1095. }
  1096. if ((auto_stat & DATA_IN_PHASE) &&
  1097. (scsi_get_resid(SCpnt) > 0) &&
  1098. ((nsp32_read2(base, FIFO_REST_CNT) & FIFO_REST_MASK) != 0)) {
  1099. printk( "auto+fifo\n");
  1100. //nsp32_pio_read(SCpnt);
  1101. }
  1102. if (auto_stat & (DATA_IN_PHASE | DATA_OUT_PHASE)) {
  1103. /* DATA_IN_PHASE/DATA_OUT_PHASE was processed. */
  1104. nsp32_dbg(NSP32_DEBUG_INTR,
  1105. "Data in/out phase processed");
  1106. /* read BMCNT, SGT pointer addr */
  1107. nsp32_dbg(NSP32_DEBUG_INTR, "BMCNT=0x%lx",
  1108. nsp32_read4(base, BM_CNT));
  1109. nsp32_dbg(NSP32_DEBUG_INTR, "addr=0x%lx",
  1110. nsp32_read4(base, SGT_ADR));
  1111. nsp32_dbg(NSP32_DEBUG_INTR, "SACK=0x%lx",
  1112. nsp32_read4(base, SACK_CNT));
  1113. nsp32_dbg(NSP32_DEBUG_INTR, "SSACK=0x%lx",
  1114. nsp32_read4(base, SAVED_SACK_CNT));
  1115. scsi_set_resid(SCpnt, 0); /* all data transferred! */
  1116. }
  1117. /*
  1118. * MsgIn Occur
  1119. */
  1120. if (auto_stat & MSG_IN_OCCUER) {
  1121. nsp32_msgin_occur(SCpnt, irq_stat, auto_stat);
  1122. }
  1123. /*
  1124. * MsgOut Occur
  1125. */
  1126. if (auto_stat & MSG_OUT_OCCUER) {
  1127. nsp32_msgout_occur(SCpnt);
  1128. }
  1129. /*
  1130. * Bus Free Occur
  1131. */
  1132. if (auto_stat & BUS_FREE_OCCUER) {
  1133. ret = nsp32_busfree_occur(SCpnt, auto_stat);
  1134. if (ret == TRUE) {
  1135. goto out;
  1136. }
  1137. }
  1138. if (auto_stat & STATUS_PHASE) {
  1139. /*
  1140. * Read CSB and substitute CSB for SCpnt->result
  1141. * to save status phase stutas byte.
  1142. * scsi error handler checks host_byte (DID_*:
  1143. * low level driver to indicate status), then checks
  1144. * status_byte (SCSI status byte).
  1145. */
  1146. SCpnt->result = (int)nsp32_read1(base, SCSI_CSB_IN);
  1147. }
  1148. if (auto_stat & ILLEGAL_PHASE) {
  1149. /* Illegal phase is detected. SACK is not back. */
  1150. nsp32_msg(KERN_WARNING,
  1151. "AUTO SCSI ILLEGAL PHASE OCCUR!!!!");
  1152. /* TODO: currently we don't have any action... bus reset? */
  1153. /*
  1154. * To send back SACK, assert, wait, and negate.
  1155. */
  1156. nsp32_sack_assert(data);
  1157. nsp32_wait_req(data, NEGATE);
  1158. nsp32_sack_negate(data);
  1159. }
  1160. if (auto_stat & COMMAND_PHASE) {
  1161. /* nothing to do */
  1162. nsp32_dbg(NSP32_DEBUG_INTR, "Command phase processed");
  1163. }
  1164. if (auto_stat & AUTOSCSI_BUSY) {
  1165. /* AutoSCSI is running */
  1166. }
  1167. show_autophase(auto_stat);
  1168. }
  1169. /* FIFO_SHLD_IRQ */
  1170. if (irq_stat & IRQSTATUS_FIFO_SHLD_IRQ) {
  1171. nsp32_dbg(NSP32_DEBUG_INTR, "FIFO IRQ");
  1172. switch(busphase) {
  1173. case BUSPHASE_DATA_OUT:
  1174. nsp32_dbg(NSP32_DEBUG_INTR, "fifo/write");
  1175. //nsp32_pio_write(SCpnt);
  1176. break;
  1177. case BUSPHASE_DATA_IN:
  1178. nsp32_dbg(NSP32_DEBUG_INTR, "fifo/read");
  1179. //nsp32_pio_read(SCpnt);
  1180. break;
  1181. case BUSPHASE_STATUS:
  1182. nsp32_dbg(NSP32_DEBUG_INTR, "fifo/status");
  1183. SCpnt->SCp.Status = nsp32_read1(base, SCSI_CSB_IN);
  1184. break;
  1185. default:
  1186. nsp32_dbg(NSP32_DEBUG_INTR, "fifo/other phase");
  1187. nsp32_dbg(NSP32_DEBUG_INTR, "irq_stat=0x%x trans_stat=0x%x", irq_stat, trans_stat);
  1188. show_busphase(busphase);
  1189. break;
  1190. }
  1191. goto out;
  1192. }
  1193. /* Phase Change IRQ */
  1194. if (irq_stat & IRQSTATUS_PHASE_CHANGE_IRQ) {
  1195. nsp32_dbg(NSP32_DEBUG_INTR, "phase change IRQ");
  1196. switch(busphase) {
  1197. case BUSPHASE_MESSAGE_IN:
  1198. nsp32_dbg(NSP32_DEBUG_INTR, "phase chg/msg in");
  1199. nsp32_msgin_occur(SCpnt, irq_stat, 0);
  1200. break;
  1201. default:
  1202. nsp32_msg(KERN_WARNING, "phase chg/other phase?");
  1203. nsp32_msg(KERN_WARNING, "irq_stat=0x%x trans_stat=0x%x\n",
  1204. irq_stat, trans_stat);
  1205. show_busphase(busphase);
  1206. break;
  1207. }
  1208. goto out;
  1209. }
  1210. /* PCI_IRQ */
  1211. if (irq_stat & IRQSTATUS_PCI_IRQ) {
  1212. nsp32_dbg(NSP32_DEBUG_INTR, "PCI IRQ occurred");
  1213. /* Do nothing */
  1214. }
  1215. /* BMCNTERR_IRQ */
  1216. if (irq_stat & IRQSTATUS_BMCNTERR_IRQ) {
  1217. nsp32_msg(KERN_ERR, "Received unexpected BMCNTERR IRQ! ");
  1218. /*
  1219. * TODO: To be implemented improving bus master
  1220. * transfer reliability when BMCNTERR is occurred in
  1221. * AutoSCSI phase described in specification.
  1222. */
  1223. }
  1224. #if 0
  1225. nsp32_dbg(NSP32_DEBUG_INTR,
  1226. "irq_stat=0x%x trans_stat=0x%x", irq_stat, trans_stat);
  1227. show_busphase(busphase);
  1228. #endif
  1229. out:
  1230. /* disable IRQ mask */
  1231. nsp32_write2(base, IRQ_CONTROL, 0);
  1232. out2:
  1233. spin_unlock_irqrestore(host->host_lock, flags);
  1234. nsp32_dbg(NSP32_DEBUG_INTR, "exit");
  1235. return IRQ_RETVAL(handled);
  1236. }
  1237. static int nsp32_show_info(struct seq_file *m, struct Scsi_Host *host)
  1238. {
  1239. unsigned long flags;
  1240. nsp32_hw_data *data;
  1241. int hostno;
  1242. unsigned int base;
  1243. unsigned char mode_reg;
  1244. int id, speed;
  1245. long model;
  1246. hostno = host->host_no;
  1247. data = (nsp32_hw_data *)host->hostdata;
  1248. base = host->io_port;
  1249. seq_puts(m, "NinjaSCSI-32 status\n\n");
  1250. seq_printf(m, "Driver version: %s, $Revision: 1.33 $\n", nsp32_release_version);
  1251. seq_printf(m, "SCSI host No.: %d\n", hostno);
  1252. seq_printf(m, "IRQ: %d\n", host->irq);
  1253. seq_printf(m, "IO: 0x%lx-0x%lx\n", host->io_port, host->io_port + host->n_io_port - 1);
  1254. seq_printf(m, "MMIO(virtual address): 0x%lx-0x%lx\n", host->base, host->base + data->MmioLength - 1);
  1255. seq_printf(m, "sg_tablesize: %d\n", host->sg_tablesize);
  1256. seq_printf(m, "Chip revision: 0x%x\n", (nsp32_read2(base, INDEX_REG) >> 8) & 0xff);
  1257. mode_reg = nsp32_index_read1(base, CHIP_MODE);
  1258. model = data->pci_devid->driver_data;
  1259. #ifdef CONFIG_PM
  1260. seq_printf(m, "Power Management: %s\n", (mode_reg & OPTF) ? "yes" : "no");
  1261. #endif
  1262. seq_printf(m, "OEM: %ld, %s\n", (mode_reg & (OEM0|OEM1)), nsp32_model[model]);
  1263. spin_lock_irqsave(&(data->Lock), flags);
  1264. seq_printf(m, "CurrentSC: 0x%p\n\n", data->CurrentSC);
  1265. spin_unlock_irqrestore(&(data->Lock), flags);
  1266. seq_puts(m, "SDTR status\n");
  1267. for (id = 0; id < ARRAY_SIZE(data->target); id++) {
  1268. seq_printf(m, "id %d: ", id);
  1269. if (id == host->this_id) {
  1270. seq_puts(m, "----- NinjaSCSI-32 host adapter\n");
  1271. continue;
  1272. }
  1273. if (data->target[id].sync_flag == SDTR_DONE) {
  1274. if (data->target[id].period == 0 &&
  1275. data->target[id].offset == ASYNC_OFFSET ) {
  1276. seq_puts(m, "async");
  1277. } else {
  1278. seq_puts(m, " sync");
  1279. }
  1280. } else {
  1281. seq_puts(m, " none");
  1282. }
  1283. if (data->target[id].period != 0) {
  1284. speed = 1000000 / (data->target[id].period * 4);
  1285. seq_printf(m, " transfer %d.%dMB/s, offset %d",
  1286. speed / 1000,
  1287. speed % 1000,
  1288. data->target[id].offset
  1289. );
  1290. }
  1291. seq_putc(m, '\n');
  1292. }
  1293. return 0;
  1294. }
  1295. /*
  1296. * Reset parameters and call scsi_done for data->cur_lunt.
  1297. * Be careful setting SCpnt->result = DID_* before calling this function.
  1298. */
  1299. static void nsp32_scsi_done(struct scsi_cmnd *SCpnt)
  1300. {
  1301. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  1302. unsigned int base = SCpnt->device->host->io_port;
  1303. scsi_dma_unmap(SCpnt);
  1304. /*
  1305. * clear TRANSFERCONTROL_BM_START
  1306. */
  1307. nsp32_write2(base, TRANSFER_CONTROL, 0);
  1308. nsp32_write4(base, BM_CNT, 0);
  1309. /*
  1310. * call scsi_done
  1311. */
  1312. (*SCpnt->scsi_done)(SCpnt);
  1313. /*
  1314. * reset parameters
  1315. */
  1316. data->cur_lunt->SCpnt = NULL;
  1317. data->cur_lunt = NULL;
  1318. data->cur_target = NULL;
  1319. data->CurrentSC = NULL;
  1320. }
  1321. /*
  1322. * Bus Free Occur
  1323. *
  1324. * Current Phase is BUSFREE. AutoSCSI is automatically execute BUSFREE phase
  1325. * with ACK reply when below condition is matched:
  1326. * MsgIn 00: Command Complete.
  1327. * MsgIn 02: Save Data Pointer.
  1328. * MsgIn 04: Diconnect.
  1329. * In other case, unexpected BUSFREE is detected.
  1330. */
  1331. static int nsp32_busfree_occur(struct scsi_cmnd *SCpnt, unsigned short execph)
  1332. {
  1333. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  1334. unsigned int base = SCpnt->device->host->io_port;
  1335. nsp32_dbg(NSP32_DEBUG_BUSFREE, "enter execph=0x%x", execph);
  1336. show_autophase(execph);
  1337. nsp32_write4(base, BM_CNT, 0);
  1338. nsp32_write2(base, TRANSFER_CONTROL, 0);
  1339. /*
  1340. * MsgIn 02: Save Data Pointer
  1341. *
  1342. * VALID:
  1343. * Save Data Pointer is received. Adjust pointer.
  1344. *
  1345. * NO-VALID:
  1346. * SCSI-3 says if Save Data Pointer is not received, then we restart
  1347. * processing and we can't adjust any SCSI data pointer in next data
  1348. * phase.
  1349. */
  1350. if (execph & MSGIN_02_VALID) {
  1351. nsp32_dbg(NSP32_DEBUG_BUSFREE, "MsgIn02_Valid");
  1352. /*
  1353. * Check sack_cnt/saved_sack_cnt, then adjust sg table if
  1354. * needed.
  1355. */
  1356. if (!(execph & MSGIN_00_VALID) &&
  1357. ((execph & DATA_IN_PHASE) || (execph & DATA_OUT_PHASE))) {
  1358. unsigned int sacklen, s_sacklen;
  1359. /*
  1360. * Read SACK count and SAVEDSACK count, then compare.
  1361. */
  1362. sacklen = nsp32_read4(base, SACK_CNT );
  1363. s_sacklen = nsp32_read4(base, SAVED_SACK_CNT);
  1364. /*
  1365. * If SAVEDSACKCNT == 0, it means SavedDataPointer is
  1366. * come after data transferring.
  1367. */
  1368. if (s_sacklen > 0) {
  1369. /*
  1370. * Comparing between sack and savedsack to
  1371. * check the condition of AutoMsgIn03.
  1372. *
  1373. * If they are same, set msgin03 == TRUE,
  1374. * COMMANDCONTROL_AUTO_MSGIN_03 is enabled at
  1375. * reselection. On the other hand, if they
  1376. * aren't same, set msgin03 == FALSE, and
  1377. * COMMANDCONTROL_AUTO_MSGIN_03 is disabled at
  1378. * reselection.
  1379. */
  1380. if (sacklen != s_sacklen) {
  1381. data->cur_lunt->msgin03 = FALSE;
  1382. } else {
  1383. data->cur_lunt->msgin03 = TRUE;
  1384. }
  1385. nsp32_adjust_busfree(SCpnt, s_sacklen);
  1386. }
  1387. }
  1388. /* This value has not substitude with valid value yet... */
  1389. //data->cur_lunt->save_datp = data->cur_datp;
  1390. } else {
  1391. /*
  1392. * no processing.
  1393. */
  1394. }
  1395. if (execph & MSGIN_03_VALID) {
  1396. /* MsgIn03 was valid to be processed. No need processing. */
  1397. }
  1398. /*
  1399. * target SDTR check
  1400. */
  1401. if (data->cur_target->sync_flag & SDTR_INITIATOR) {
  1402. /*
  1403. * SDTR negotiation pulled by the initiator has not
  1404. * finished yet. Fall back to ASYNC mode.
  1405. */
  1406. nsp32_set_async(data, data->cur_target);
  1407. data->cur_target->sync_flag &= ~SDTR_INITIATOR;
  1408. data->cur_target->sync_flag |= SDTR_DONE;
  1409. } else if (data->cur_target->sync_flag & SDTR_TARGET) {
  1410. /*
  1411. * SDTR negotiation pulled by the target has been
  1412. * negotiating.
  1413. */
  1414. if (execph & (MSGIN_00_VALID | MSGIN_04_VALID)) {
  1415. /*
  1416. * If valid message is received, then
  1417. * negotiation is succeeded.
  1418. */
  1419. } else {
  1420. /*
  1421. * On the contrary, if unexpected bus free is
  1422. * occurred, then negotiation is failed. Fall
  1423. * back to ASYNC mode.
  1424. */
  1425. nsp32_set_async(data, data->cur_target);
  1426. }
  1427. data->cur_target->sync_flag &= ~SDTR_TARGET;
  1428. data->cur_target->sync_flag |= SDTR_DONE;
  1429. }
  1430. /*
  1431. * It is always ensured by SCSI standard that initiator
  1432. * switches into Bus Free Phase after
  1433. * receiving message 00 (Command Complete), 04 (Disconnect).
  1434. * It's the reason that processing here is valid.
  1435. */
  1436. if (execph & MSGIN_00_VALID) {
  1437. /* MsgIn 00: Command Complete */
  1438. nsp32_dbg(NSP32_DEBUG_BUSFREE, "command complete");
  1439. SCpnt->SCp.Status = nsp32_read1(base, SCSI_CSB_IN);
  1440. SCpnt->SCp.Message = 0;
  1441. nsp32_dbg(NSP32_DEBUG_BUSFREE,
  1442. "normal end stat=0x%x resid=0x%x\n",
  1443. SCpnt->SCp.Status, scsi_get_resid(SCpnt));
  1444. SCpnt->result = (DID_OK << 16) |
  1445. (SCpnt->SCp.Message << 8) |
  1446. (SCpnt->SCp.Status << 0);
  1447. nsp32_scsi_done(SCpnt);
  1448. /* All operation is done */
  1449. return TRUE;
  1450. } else if (execph & MSGIN_04_VALID) {
  1451. /* MsgIn 04: Disconnect */
  1452. SCpnt->SCp.Status = nsp32_read1(base, SCSI_CSB_IN);
  1453. SCpnt->SCp.Message = 4;
  1454. nsp32_dbg(NSP32_DEBUG_BUSFREE, "disconnect");
  1455. return TRUE;
  1456. } else {
  1457. /* Unexpected bus free */
  1458. nsp32_msg(KERN_WARNING, "unexpected bus free occurred");
  1459. /* DID_ERROR? */
  1460. //SCpnt->result = (DID_OK << 16) | (SCpnt->SCp.Message << 8) | (SCpnt->SCp.Status << 0);
  1461. SCpnt->result = DID_ERROR << 16;
  1462. nsp32_scsi_done(SCpnt);
  1463. return TRUE;
  1464. }
  1465. return FALSE;
  1466. }
  1467. /*
  1468. * nsp32_adjust_busfree - adjusting SG table
  1469. *
  1470. * Note: This driver adjust the SG table using SCSI ACK
  1471. * counter instead of BMCNT counter!
  1472. */
  1473. static void nsp32_adjust_busfree(struct scsi_cmnd *SCpnt, unsigned int s_sacklen)
  1474. {
  1475. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  1476. int old_entry = data->cur_entry;
  1477. int new_entry;
  1478. int sg_num = data->cur_lunt->sg_num;
  1479. nsp32_sgtable *sgt = data->cur_lunt->sglun->sgt;
  1480. unsigned int restlen, sentlen;
  1481. u32_le len, addr;
  1482. nsp32_dbg(NSP32_DEBUG_SGLIST, "old resid=0x%x", scsi_get_resid(SCpnt));
  1483. /* adjust saved SACK count with 4 byte start address boundary */
  1484. s_sacklen -= le32_to_cpu(sgt[old_entry].addr) & 3;
  1485. /*
  1486. * calculate new_entry from sack count and each sgt[].len
  1487. * calculate the byte which is intent to send
  1488. */
  1489. sentlen = 0;
  1490. for (new_entry = old_entry; new_entry < sg_num; new_entry++) {
  1491. sentlen += (le32_to_cpu(sgt[new_entry].len) & ~SGTEND);
  1492. if (sentlen > s_sacklen) {
  1493. break;
  1494. }
  1495. }
  1496. /* all sgt is processed */
  1497. if (new_entry == sg_num) {
  1498. goto last;
  1499. }
  1500. if (sentlen == s_sacklen) {
  1501. /* XXX: confirm it's ok or not */
  1502. /* In this case, it's ok because we are at
  1503. the head element of the sg. restlen is correctly calculated. */
  1504. }
  1505. /* calculate the rest length for transferring */
  1506. restlen = sentlen - s_sacklen;
  1507. /* update adjusting current SG table entry */
  1508. len = le32_to_cpu(sgt[new_entry].len);
  1509. addr = le32_to_cpu(sgt[new_entry].addr);
  1510. addr += (len - restlen);
  1511. sgt[new_entry].addr = cpu_to_le32(addr);
  1512. sgt[new_entry].len = cpu_to_le32(restlen);
  1513. /* set cur_entry with new_entry */
  1514. data->cur_entry = new_entry;
  1515. return;
  1516. last:
  1517. if (scsi_get_resid(SCpnt) < sentlen) {
  1518. nsp32_msg(KERN_ERR, "resid underflow");
  1519. }
  1520. scsi_set_resid(SCpnt, scsi_get_resid(SCpnt) - sentlen);
  1521. nsp32_dbg(NSP32_DEBUG_SGLIST, "new resid=0x%x", scsi_get_resid(SCpnt));
  1522. /* update hostdata and lun */
  1523. return;
  1524. }
  1525. /*
  1526. * It's called MsgOut phase occur.
  1527. * NinjaSCSI-32Bi/UDE automatically processes up to 3 messages in
  1528. * message out phase. It, however, has more than 3 messages,
  1529. * HBA creates the interrupt and we have to process by hand.
  1530. */
  1531. static void nsp32_msgout_occur(struct scsi_cmnd *SCpnt)
  1532. {
  1533. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  1534. unsigned int base = SCpnt->device->host->io_port;
  1535. //unsigned short command;
  1536. long new_sgtp;
  1537. int i;
  1538. nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR,
  1539. "enter: msgout_len: 0x%x", data->msgout_len);
  1540. /*
  1541. * If MsgOut phase is occurred without having any
  1542. * message, then No_Operation is sent (SCSI-2).
  1543. */
  1544. if (data->msgout_len == 0) {
  1545. nsp32_build_nop(SCpnt);
  1546. }
  1547. /*
  1548. * Set SGTP ADDR current entry for restarting AUTOSCSI,
  1549. * because SGTP is incremented next point.
  1550. * There is few statement in the specification...
  1551. */
  1552. new_sgtp = data->cur_lunt->sglun_paddr +
  1553. (data->cur_lunt->cur_entry * sizeof(nsp32_sgtable));
  1554. /*
  1555. * send messages
  1556. */
  1557. for (i = 0; i < data->msgout_len; i++) {
  1558. nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR,
  1559. "%d : 0x%x", i, data->msgoutbuf[i]);
  1560. /*
  1561. * Check REQ is asserted.
  1562. */
  1563. nsp32_wait_req(data, ASSERT);
  1564. if (i == (data->msgout_len - 1)) {
  1565. /*
  1566. * If the last message, set the AutoSCSI restart
  1567. * before send back the ack message. AutoSCSI
  1568. * restart automatically negate ATN signal.
  1569. */
  1570. //command = (AUTO_MSGIN_00_OR_04 | AUTO_MSGIN_02);
  1571. //nsp32_restart_autoscsi(SCpnt, command);
  1572. nsp32_write2(base, COMMAND_CONTROL,
  1573. (CLEAR_CDB_FIFO_POINTER |
  1574. AUTO_COMMAND_PHASE |
  1575. AUTOSCSI_RESTART |
  1576. AUTO_MSGIN_00_OR_04 |
  1577. AUTO_MSGIN_02 ));
  1578. }
  1579. /*
  1580. * Write data with SACK, then wait sack is
  1581. * automatically negated.
  1582. */
  1583. nsp32_write1(base, SCSI_DATA_WITH_ACK, data->msgoutbuf[i]);
  1584. nsp32_wait_sack(data, NEGATE);
  1585. nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR, "bus: 0x%x\n",
  1586. nsp32_read1(base, SCSI_BUS_MONITOR));
  1587. };
  1588. data->msgout_len = 0;
  1589. nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR, "exit");
  1590. }
  1591. /*
  1592. * Restart AutoSCSI
  1593. *
  1594. * Note: Restarting AutoSCSI needs set:
  1595. * SYNC_REG, ACK_WIDTH, SGT_ADR, TRANSFER_CONTROL
  1596. */
  1597. static void nsp32_restart_autoscsi(struct scsi_cmnd *SCpnt, unsigned short command)
  1598. {
  1599. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  1600. unsigned int base = data->BaseAddress;
  1601. unsigned short transfer = 0;
  1602. nsp32_dbg(NSP32_DEBUG_RESTART, "enter");
  1603. if (data->cur_target == NULL || data->cur_lunt == NULL) {
  1604. nsp32_msg(KERN_ERR, "Target or Lun is invalid");
  1605. }
  1606. /*
  1607. * set SYNC_REG
  1608. * Don't set BM_START_ADR before setting this register.
  1609. */
  1610. nsp32_write1(base, SYNC_REG, data->cur_target->syncreg);
  1611. /*
  1612. * set ACKWIDTH
  1613. */
  1614. nsp32_write1(base, ACK_WIDTH, data->cur_target->ackwidth);
  1615. /*
  1616. * set SREQ hazard killer sampling rate
  1617. */
  1618. nsp32_write1(base, SREQ_SMPL_RATE, data->cur_target->sample_reg);
  1619. /*
  1620. * set SGT ADDR (physical address)
  1621. */
  1622. nsp32_write4(base, SGT_ADR, data->cur_lunt->sglun_paddr);
  1623. /*
  1624. * set TRANSFER CONTROL REG
  1625. */
  1626. transfer = 0;
  1627. transfer |= (TRANSFER_GO | ALL_COUNTER_CLR);
  1628. if (data->trans_method & NSP32_TRANSFER_BUSMASTER) {
  1629. if (scsi_bufflen(SCpnt) > 0) {
  1630. transfer |= BM_START;
  1631. }
  1632. } else if (data->trans_method & NSP32_TRANSFER_MMIO) {
  1633. transfer |= CB_MMIO_MODE;
  1634. } else if (data->trans_method & NSP32_TRANSFER_PIO) {
  1635. transfer |= CB_IO_MODE;
  1636. }
  1637. nsp32_write2(base, TRANSFER_CONTROL, transfer);
  1638. /*
  1639. * restart AutoSCSI
  1640. *
  1641. * TODO: COMMANDCONTROL_AUTO_COMMAND_PHASE is needed ?
  1642. */
  1643. command |= (CLEAR_CDB_FIFO_POINTER |
  1644. AUTO_COMMAND_PHASE |
  1645. AUTOSCSI_RESTART );
  1646. nsp32_write2(base, COMMAND_CONTROL, command);
  1647. nsp32_dbg(NSP32_DEBUG_RESTART, "exit");
  1648. }
  1649. /*
  1650. * cannot run automatically message in occur
  1651. */
  1652. static void nsp32_msgin_occur(struct scsi_cmnd *SCpnt,
  1653. unsigned long irq_status,
  1654. unsigned short execph)
  1655. {
  1656. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  1657. unsigned int base = SCpnt->device->host->io_port;
  1658. unsigned char msg;
  1659. unsigned char msgtype;
  1660. unsigned char newlun;
  1661. unsigned short command = 0;
  1662. int msgclear = TRUE;
  1663. long new_sgtp;
  1664. int ret;
  1665. /*
  1666. * read first message
  1667. * Use SCSIDATA_W_ACK instead of SCSIDATAIN, because the procedure
  1668. * of Message-In have to be processed before sending back SCSI ACK.
  1669. */
  1670. msg = nsp32_read1(base, SCSI_DATA_IN);
  1671. data->msginbuf[(unsigned char)data->msgin_len] = msg;
  1672. msgtype = data->msginbuf[0];
  1673. nsp32_dbg(NSP32_DEBUG_MSGINOCCUR,
  1674. "enter: msglen: 0x%x msgin: 0x%x msgtype: 0x%x",
  1675. data->msgin_len, msg, msgtype);
  1676. /*
  1677. * TODO: We need checking whether bus phase is message in?
  1678. */
  1679. /*
  1680. * assert SCSI ACK
  1681. */
  1682. nsp32_sack_assert(data);
  1683. /*
  1684. * processing IDENTIFY
  1685. */
  1686. if (msgtype & 0x80) {
  1687. if (!(irq_status & IRQSTATUS_RESELECT_OCCUER)) {
  1688. /* Invalid (non reselect) phase */
  1689. goto reject;
  1690. }
  1691. newlun = msgtype & 0x1f; /* TODO: SPI-3 compliant? */
  1692. ret = nsp32_reselection(SCpnt, newlun);
  1693. if (ret == TRUE) {
  1694. goto restart;
  1695. } else {
  1696. goto reject;
  1697. }
  1698. }
  1699. /*
  1700. * processing messages except for IDENTIFY
  1701. *
  1702. * TODO: Messages are all SCSI-2 terminology. SCSI-3 compliance is TODO.
  1703. */
  1704. switch (msgtype) {
  1705. /*
  1706. * 1-byte message
  1707. */
  1708. case COMMAND_COMPLETE:
  1709. case DISCONNECT:
  1710. /*
  1711. * These messages should not be occurred.
  1712. * They should be processed on AutoSCSI sequencer.
  1713. */
  1714. nsp32_msg(KERN_WARNING,
  1715. "unexpected message of AutoSCSI MsgIn: 0x%x", msg);
  1716. break;
  1717. case RESTORE_POINTERS:
  1718. /*
  1719. * AutoMsgIn03 is disabled, and HBA gets this message.
  1720. */
  1721. if ((execph & DATA_IN_PHASE) || (execph & DATA_OUT_PHASE)) {
  1722. unsigned int s_sacklen;
  1723. s_sacklen = nsp32_read4(base, SAVED_SACK_CNT);
  1724. if ((execph & MSGIN_02_VALID) && (s_sacklen > 0)) {
  1725. nsp32_adjust_busfree(SCpnt, s_sacklen);
  1726. } else {
  1727. /* No need to rewrite SGT */
  1728. }
  1729. }
  1730. data->cur_lunt->msgin03 = FALSE;
  1731. /* Update with the new value */
  1732. /* reset SACK/SavedACK counter (or ALL clear?) */
  1733. nsp32_write4(base, CLR_COUNTER, CLRCOUNTER_ALLMASK);
  1734. /*
  1735. * set new sg pointer
  1736. */
  1737. new_sgtp = data->cur_lunt->sglun_paddr +
  1738. (data->cur_lunt->cur_entry * sizeof(nsp32_sgtable));
  1739. nsp32_write4(base, SGT_ADR, new_sgtp);
  1740. break;
  1741. case SAVE_POINTERS:
  1742. /*
  1743. * These messages should not be occurred.
  1744. * They should be processed on AutoSCSI sequencer.
  1745. */
  1746. nsp32_msg (KERN_WARNING,
  1747. "unexpected message of AutoSCSI MsgIn: SAVE_POINTERS");
  1748. break;
  1749. case MESSAGE_REJECT:
  1750. /* If previous message_out is sending SDTR, and get
  1751. message_reject from target, SDTR negotiation is failed */
  1752. if (data->cur_target->sync_flag &
  1753. (SDTR_INITIATOR | SDTR_TARGET)) {
  1754. /*
  1755. * Current target is negotiating SDTR, but it's
  1756. * failed. Fall back to async transfer mode, and set
  1757. * SDTR_DONE.
  1758. */
  1759. nsp32_set_async(data, data->cur_target);
  1760. data->cur_target->sync_flag &= ~SDTR_INITIATOR;
  1761. data->cur_target->sync_flag |= SDTR_DONE;
  1762. }
  1763. break;
  1764. case LINKED_CMD_COMPLETE:
  1765. case LINKED_FLG_CMD_COMPLETE:
  1766. /* queue tag is not supported currently */
  1767. nsp32_msg (KERN_WARNING,
  1768. "unsupported message: 0x%x", msgtype);
  1769. break;
  1770. case INITIATE_RECOVERY:
  1771. /* staring ECA (Extended Contingent Allegiance) state. */
  1772. /* This message is declined in SPI2 or later. */
  1773. goto reject;
  1774. /*
  1775. * 2-byte message
  1776. */
  1777. case SIMPLE_QUEUE_TAG:
  1778. case 0x23:
  1779. /*
  1780. * 0x23: Ignore_Wide_Residue is not declared in scsi.h.
  1781. * No support is needed.
  1782. */
  1783. if (data->msgin_len >= 1) {
  1784. goto reject;
  1785. }
  1786. /* current position is 1-byte of 2 byte */
  1787. msgclear = FALSE;
  1788. break;
  1789. /*
  1790. * extended message
  1791. */
  1792. case EXTENDED_MESSAGE:
  1793. if (data->msgin_len < 1) {
  1794. /*
  1795. * Current position does not reach 2-byte
  1796. * (2-byte is extended message length).
  1797. */
  1798. msgclear = FALSE;
  1799. break;
  1800. }
  1801. if ((data->msginbuf[1] + 1) > data->msgin_len) {
  1802. /*
  1803. * Current extended message has msginbuf[1] + 2
  1804. * (msgin_len starts counting from 0, so buf[1] + 1).
  1805. * If current message position is not finished,
  1806. * continue receiving message.
  1807. */
  1808. msgclear = FALSE;
  1809. break;
  1810. }
  1811. /*
  1812. * Reach here means regular length of each type of
  1813. * extended messages.
  1814. */
  1815. switch (data->msginbuf[2]) {
  1816. case EXTENDED_MODIFY_DATA_POINTER:
  1817. /* TODO */
  1818. goto reject; /* not implemented yet */
  1819. break;
  1820. case EXTENDED_SDTR:
  1821. /*
  1822. * Exchange this message between initiator and target.
  1823. */
  1824. if (data->msgin_len != EXTENDED_SDTR_LEN + 1) {
  1825. /*
  1826. * received inappropriate message.
  1827. */
  1828. goto reject;
  1829. break;
  1830. }
  1831. nsp32_analyze_sdtr(SCpnt);
  1832. break;
  1833. case EXTENDED_EXTENDED_IDENTIFY:
  1834. /* SCSI-I only, not supported. */
  1835. goto reject; /* not implemented yet */
  1836. break;
  1837. case EXTENDED_WDTR:
  1838. goto reject; /* not implemented yet */
  1839. break;
  1840. default:
  1841. goto reject;
  1842. }
  1843. break;
  1844. default:
  1845. goto reject;
  1846. }
  1847. restart:
  1848. if (msgclear == TRUE) {
  1849. data->msgin_len = 0;
  1850. /*
  1851. * If restarting AutoSCSI, but there are some message to out
  1852. * (msgout_len > 0), set AutoATN, and set SCSIMSGOUT as 0
  1853. * (MV_VALID = 0). When commandcontrol is written with
  1854. * AutoSCSI restart, at the same time MsgOutOccur should be
  1855. * happened (however, such situation is really possible...?).
  1856. */
  1857. if (data->msgout_len > 0) {
  1858. nsp32_write4(base, SCSI_MSG_OUT, 0);
  1859. command |= AUTO_ATN;
  1860. }
  1861. /*
  1862. * restart AutoSCSI
  1863. * If it's failed, COMMANDCONTROL_AUTO_COMMAND_PHASE is needed.
  1864. */
  1865. command |= (AUTO_MSGIN_00_OR_04 | AUTO_MSGIN_02);
  1866. /*
  1867. * If current msgin03 is TRUE, then flag on.
  1868. */
  1869. if (data->cur_lunt->msgin03 == TRUE) {
  1870. command |= AUTO_MSGIN_03;
  1871. }
  1872. data->cur_lunt->msgin03 = FALSE;
  1873. } else {
  1874. data->msgin_len++;
  1875. }
  1876. /*
  1877. * restart AutoSCSI
  1878. */
  1879. nsp32_restart_autoscsi(SCpnt, command);
  1880. /*
  1881. * wait SCSI REQ negate for REQ-ACK handshake
  1882. */
  1883. nsp32_wait_req(data, NEGATE);
  1884. /*
  1885. * negate SCSI ACK
  1886. */
  1887. nsp32_sack_negate(data);
  1888. nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "exit");
  1889. return;
  1890. reject:
  1891. nsp32_msg(KERN_WARNING,
  1892. "invalid or unsupported MessageIn, rejected. "
  1893. "current msg: 0x%x (len: 0x%x), processing msg: 0x%x",
  1894. msg, data->msgin_len, msgtype);
  1895. nsp32_build_reject(SCpnt);
  1896. data->msgin_len = 0;
  1897. goto restart;
  1898. }
  1899. /*
  1900. *
  1901. */
  1902. static void nsp32_analyze_sdtr(struct scsi_cmnd *SCpnt)
  1903. {
  1904. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  1905. nsp32_target *target = data->cur_target;
  1906. nsp32_sync_table *synct;
  1907. unsigned char get_period = data->msginbuf[3];
  1908. unsigned char get_offset = data->msginbuf[4];
  1909. int entry;
  1910. int syncnum;
  1911. nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "enter");
  1912. synct = data->synct;
  1913. syncnum = data->syncnum;
  1914. /*
  1915. * If this inititor sent the SDTR message, then target responds SDTR,
  1916. * initiator SYNCREG, ACKWIDTH from SDTR parameter.
  1917. * Messages are not appropriate, then send back reject message.
  1918. * If initiator did not send the SDTR, but target sends SDTR,
  1919. * initiator calculator the appropriate parameter and send back SDTR.
  1920. */
  1921. if (target->sync_flag & SDTR_INITIATOR) {
  1922. /*
  1923. * Initiator sent SDTR, the target responds and
  1924. * send back negotiation SDTR.
  1925. */
  1926. nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "target responds SDTR");
  1927. target->sync_flag &= ~SDTR_INITIATOR;
  1928. target->sync_flag |= SDTR_DONE;
  1929. /*
  1930. * offset:
  1931. */
  1932. if (get_offset > SYNC_OFFSET) {
  1933. /*
  1934. * Negotiation is failed, the target send back
  1935. * unexpected offset value.
  1936. */
  1937. goto reject;
  1938. }
  1939. if (get_offset == ASYNC_OFFSET) {
  1940. /*
  1941. * Negotiation is succeeded, the target want
  1942. * to fall back into asynchronous transfer mode.
  1943. */
  1944. goto async;
  1945. }
  1946. /*
  1947. * period:
  1948. * Check whether sync period is too short. If too short,
  1949. * fall back to async mode. If it's ok, then investigate
  1950. * the received sync period. If sync period is acceptable
  1951. * between sync table start_period and end_period, then
  1952. * set this I_T nexus as sent offset and period.
  1953. * If it's not acceptable, send back reject and fall back
  1954. * to async mode.
  1955. */
  1956. if (get_period < data->synct[0].period_num) {
  1957. /*
  1958. * Negotiation is failed, the target send back
  1959. * unexpected period value.
  1960. */
  1961. goto reject;
  1962. }
  1963. entry = nsp32_search_period_entry(data, target, get_period);
  1964. if (entry < 0) {
  1965. /*
  1966. * Target want to use long period which is not
  1967. * acceptable NinjaSCSI-32Bi/UDE.
  1968. */
  1969. goto reject;
  1970. }
  1971. /*
  1972. * Set new sync table and offset in this I_T nexus.
  1973. */
  1974. nsp32_set_sync_entry(data, target, entry, get_offset);
  1975. } else {
  1976. /* Target send SDTR to initiator. */
  1977. nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "target send SDTR");
  1978. target->sync_flag |= SDTR_INITIATOR;
  1979. /* offset: */
  1980. if (get_offset > SYNC_OFFSET) {
  1981. /* send back as SYNC_OFFSET */
  1982. get_offset = SYNC_OFFSET;
  1983. }
  1984. /* period: */
  1985. if (get_period < data->synct[0].period_num) {
  1986. get_period = data->synct[0].period_num;
  1987. }
  1988. entry = nsp32_search_period_entry(data, target, get_period);
  1989. if (get_offset == ASYNC_OFFSET || entry < 0) {
  1990. nsp32_set_async(data, target);
  1991. nsp32_build_sdtr(SCpnt, 0, ASYNC_OFFSET);
  1992. } else {
  1993. nsp32_set_sync_entry(data, target, entry, get_offset);
  1994. nsp32_build_sdtr(SCpnt, get_period, get_offset);
  1995. }
  1996. }
  1997. target->period = get_period;
  1998. nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "exit");
  1999. return;
  2000. reject:
  2001. /*
  2002. * If the current message is unacceptable, send back to the target
  2003. * with reject message.
  2004. */
  2005. nsp32_build_reject(SCpnt);
  2006. async:
  2007. nsp32_set_async(data, target); /* set as ASYNC transfer mode */
  2008. target->period = 0;
  2009. nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "exit: set async");
  2010. return;
  2011. }
  2012. /*
  2013. * Search config entry number matched in sync_table from given
  2014. * target and speed period value. If failed to search, return negative value.
  2015. */
  2016. static int nsp32_search_period_entry(nsp32_hw_data *data,
  2017. nsp32_target *target,
  2018. unsigned char period)
  2019. {
  2020. int i;
  2021. if (target->limit_entry >= data->syncnum) {
  2022. nsp32_msg(KERN_ERR, "limit_entry exceeds syncnum!");
  2023. target->limit_entry = 0;
  2024. }
  2025. for (i = target->limit_entry; i < data->syncnum; i++) {
  2026. if (period >= data->synct[i].start_period &&
  2027. period <= data->synct[i].end_period) {
  2028. break;
  2029. }
  2030. }
  2031. /*
  2032. * Check given period value is over the sync_table value.
  2033. * If so, return max value.
  2034. */
  2035. if (i == data->syncnum) {
  2036. i = -1;
  2037. }
  2038. return i;
  2039. }
  2040. /*
  2041. * target <-> initiator use ASYNC transfer
  2042. */
  2043. static void nsp32_set_async(nsp32_hw_data *data, nsp32_target *target)
  2044. {
  2045. unsigned char period = data->synct[target->limit_entry].period_num;
  2046. target->offset = ASYNC_OFFSET;
  2047. target->period = 0;
  2048. target->syncreg = TO_SYNCREG(period, ASYNC_OFFSET);
  2049. target->ackwidth = 0;
  2050. target->sample_reg = 0;
  2051. nsp32_dbg(NSP32_DEBUG_SYNC, "set async");
  2052. }
  2053. /*
  2054. * target <-> initiator use maximum SYNC transfer
  2055. */
  2056. static void nsp32_set_max_sync(nsp32_hw_data *data,
  2057. nsp32_target *target,
  2058. unsigned char *period,
  2059. unsigned char *offset)
  2060. {
  2061. unsigned char period_num, ackwidth;
  2062. period_num = data->synct[target->limit_entry].period_num;
  2063. *period = data->synct[target->limit_entry].start_period;
  2064. ackwidth = data->synct[target->limit_entry].ackwidth;
  2065. *offset = SYNC_OFFSET;
  2066. target->syncreg = TO_SYNCREG(period_num, *offset);
  2067. target->ackwidth = ackwidth;
  2068. target->offset = *offset;
  2069. target->sample_reg = 0; /* disable SREQ sampling */
  2070. }
  2071. /*
  2072. * target <-> initiator use entry number speed
  2073. */
  2074. static void nsp32_set_sync_entry(nsp32_hw_data *data,
  2075. nsp32_target *target,
  2076. int entry,
  2077. unsigned char offset)
  2078. {
  2079. unsigned char period, ackwidth, sample_rate;
  2080. period = data->synct[entry].period_num;
  2081. ackwidth = data->synct[entry].ackwidth;
  2082. offset = offset;
  2083. sample_rate = data->synct[entry].sample_rate;
  2084. target->syncreg = TO_SYNCREG(period, offset);
  2085. target->ackwidth = ackwidth;
  2086. target->offset = offset;
  2087. target->sample_reg = sample_rate | SAMPLING_ENABLE;
  2088. nsp32_dbg(NSP32_DEBUG_SYNC, "set sync");
  2089. }
  2090. /*
  2091. * It waits until SCSI REQ becomes assertion or negation state.
  2092. *
  2093. * Note: If nsp32_msgin_occur is called, we asserts SCSI ACK. Then
  2094. * connected target responds SCSI REQ negation. We have to wait
  2095. * SCSI REQ becomes negation in order to negate SCSI ACK signal for
  2096. * REQ-ACK handshake.
  2097. */
  2098. static void nsp32_wait_req(nsp32_hw_data *data, int state)
  2099. {
  2100. unsigned int base = data->BaseAddress;
  2101. int wait_time = 0;
  2102. unsigned char bus, req_bit;
  2103. if (!((state == ASSERT) || (state == NEGATE))) {
  2104. nsp32_msg(KERN_ERR, "unknown state designation");
  2105. }
  2106. /* REQ is BIT(5) */
  2107. req_bit = (state == ASSERT ? BUSMON_REQ : 0);
  2108. do {
  2109. bus = nsp32_read1(base, SCSI_BUS_MONITOR);
  2110. if ((bus & BUSMON_REQ) == req_bit) {
  2111. nsp32_dbg(NSP32_DEBUG_WAIT,
  2112. "wait_time: %d", wait_time);
  2113. return;
  2114. }
  2115. udelay(1);
  2116. wait_time++;
  2117. } while (wait_time < REQSACK_TIMEOUT_TIME);
  2118. nsp32_msg(KERN_WARNING, "wait REQ timeout, req_bit: 0x%x", req_bit);
  2119. }
  2120. /*
  2121. * It waits until SCSI SACK becomes assertion or negation state.
  2122. */
  2123. static void nsp32_wait_sack(nsp32_hw_data *data, int state)
  2124. {
  2125. unsigned int base = data->BaseAddress;
  2126. int wait_time = 0;
  2127. unsigned char bus, ack_bit;
  2128. if (!((state == ASSERT) || (state == NEGATE))) {
  2129. nsp32_msg(KERN_ERR, "unknown state designation");
  2130. }
  2131. /* ACK is BIT(4) */
  2132. ack_bit = (state == ASSERT ? BUSMON_ACK : 0);
  2133. do {
  2134. bus = nsp32_read1(base, SCSI_BUS_MONITOR);
  2135. if ((bus & BUSMON_ACK) == ack_bit) {
  2136. nsp32_dbg(NSP32_DEBUG_WAIT,
  2137. "wait_time: %d", wait_time);
  2138. return;
  2139. }
  2140. udelay(1);
  2141. wait_time++;
  2142. } while (wait_time < REQSACK_TIMEOUT_TIME);
  2143. nsp32_msg(KERN_WARNING, "wait SACK timeout, ack_bit: 0x%x", ack_bit);
  2144. }
  2145. /*
  2146. * assert SCSI ACK
  2147. *
  2148. * Note: SCSI ACK assertion needs with ACKENB=1, AUTODIRECTION=1.
  2149. */
  2150. static void nsp32_sack_assert(nsp32_hw_data *data)
  2151. {
  2152. unsigned int base = data->BaseAddress;
  2153. unsigned char busctrl;
  2154. busctrl = nsp32_read1(base, SCSI_BUS_CONTROL);
  2155. busctrl |= (BUSCTL_ACK | AUTODIRECTION | ACKENB);
  2156. nsp32_write1(base, SCSI_BUS_CONTROL, busctrl);
  2157. }
  2158. /*
  2159. * negate SCSI ACK
  2160. */
  2161. static void nsp32_sack_negate(nsp32_hw_data *data)
  2162. {
  2163. unsigned int base = data->BaseAddress;
  2164. unsigned char busctrl;
  2165. busctrl = nsp32_read1(base, SCSI_BUS_CONTROL);
  2166. busctrl &= ~BUSCTL_ACK;
  2167. nsp32_write1(base, SCSI_BUS_CONTROL, busctrl);
  2168. }
  2169. /*
  2170. * Note: n_io_port is defined as 0x7f because I/O register port is
  2171. * assigned as:
  2172. * 0x800-0x8ff: memory mapped I/O port
  2173. * 0x900-0xbff: (map same 0x800-0x8ff I/O port image repeatedly)
  2174. * 0xc00-0xfff: CardBus status registers
  2175. */
  2176. static int nsp32_detect(struct pci_dev *pdev)
  2177. {
  2178. struct Scsi_Host *host; /* registered host structure */
  2179. struct resource *res;
  2180. nsp32_hw_data *data;
  2181. int ret;
  2182. int i, j;
  2183. nsp32_dbg(NSP32_DEBUG_REGISTER, "enter");
  2184. /*
  2185. * register this HBA as SCSI device
  2186. */
  2187. host = scsi_host_alloc(&nsp32_template, sizeof(nsp32_hw_data));
  2188. if (host == NULL) {
  2189. nsp32_msg (KERN_ERR, "failed to scsi register");
  2190. goto err;
  2191. }
  2192. /*
  2193. * set nsp32_hw_data
  2194. */
  2195. data = (nsp32_hw_data *)host->hostdata;
  2196. memcpy(data, &nsp32_data_base, sizeof(nsp32_hw_data));
  2197. host->irq = data->IrqNumber;
  2198. host->io_port = data->BaseAddress;
  2199. host->unique_id = data->BaseAddress;
  2200. host->n_io_port = data->NumAddress;
  2201. host->base = (unsigned long)data->MmioAddress;
  2202. data->Host = host;
  2203. spin_lock_init(&(data->Lock));
  2204. data->cur_lunt = NULL;
  2205. data->cur_target = NULL;
  2206. /*
  2207. * Bus master transfer mode is supported currently.
  2208. */
  2209. data->trans_method = NSP32_TRANSFER_BUSMASTER;
  2210. /*
  2211. * Set clock div, CLOCK_4 (HBA has own external clock, and
  2212. * dividing * 100ns/4).
  2213. * Currently CLOCK_4 has only tested, not for CLOCK_2/PCICLK yet.
  2214. */
  2215. data->clock = CLOCK_4;
  2216. /*
  2217. * Select appropriate nsp32_sync_table and set I_CLOCKDIV.
  2218. */
  2219. switch (data->clock) {
  2220. case CLOCK_4:
  2221. /* If data->clock is CLOCK_4, then select 40M sync table. */
  2222. data->synct = nsp32_sync_table_40M;
  2223. data->syncnum = ARRAY_SIZE(nsp32_sync_table_40M);
  2224. break;
  2225. case CLOCK_2:
  2226. /* If data->clock is CLOCK_2, then select 20M sync table. */
  2227. data->synct = nsp32_sync_table_20M;
  2228. data->syncnum = ARRAY_SIZE(nsp32_sync_table_20M);
  2229. break;
  2230. case PCICLK:
  2231. /* If data->clock is PCICLK, then select pci sync table. */
  2232. data->synct = nsp32_sync_table_pci;
  2233. data->syncnum = ARRAY_SIZE(nsp32_sync_table_pci);
  2234. break;
  2235. default:
  2236. nsp32_msg(KERN_WARNING,
  2237. "Invalid clock div is selected, set CLOCK_4.");
  2238. /* Use default value CLOCK_4 */
  2239. data->clock = CLOCK_4;
  2240. data->synct = nsp32_sync_table_40M;
  2241. data->syncnum = ARRAY_SIZE(nsp32_sync_table_40M);
  2242. }
  2243. /*
  2244. * setup nsp32_lunt
  2245. */
  2246. /*
  2247. * setup DMA
  2248. */
  2249. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
  2250. nsp32_msg (KERN_ERR, "failed to set PCI DMA mask");
  2251. goto scsi_unregister;
  2252. }
  2253. /*
  2254. * allocate autoparam DMA resource.
  2255. */
  2256. data->autoparam = pci_alloc_consistent(pdev, sizeof(nsp32_autoparam), &(data->auto_paddr));
  2257. if (data->autoparam == NULL) {
  2258. nsp32_msg(KERN_ERR, "failed to allocate DMA memory");
  2259. goto scsi_unregister;
  2260. }
  2261. /*
  2262. * allocate scatter-gather DMA resource.
  2263. */
  2264. data->sg_list = pci_alloc_consistent(pdev, NSP32_SG_TABLE_SIZE,
  2265. &(data->sg_paddr));
  2266. if (data->sg_list == NULL) {
  2267. nsp32_msg(KERN_ERR, "failed to allocate DMA memory");
  2268. goto free_autoparam;
  2269. }
  2270. for (i = 0; i < ARRAY_SIZE(data->lunt); i++) {
  2271. for (j = 0; j < ARRAY_SIZE(data->lunt[0]); j++) {
  2272. int offset = i * ARRAY_SIZE(data->lunt[0]) + j;
  2273. nsp32_lunt tmp = {
  2274. .SCpnt = NULL,
  2275. .save_datp = 0,
  2276. .msgin03 = FALSE,
  2277. .sg_num = 0,
  2278. .cur_entry = 0,
  2279. .sglun = &(data->sg_list[offset]),
  2280. .sglun_paddr = data->sg_paddr + (offset * sizeof(nsp32_sglun)),
  2281. };
  2282. data->lunt[i][j] = tmp;
  2283. }
  2284. }
  2285. /*
  2286. * setup target
  2287. */
  2288. for (i = 0; i < ARRAY_SIZE(data->target); i++) {
  2289. nsp32_target *target = &(data->target[i]);
  2290. target->limit_entry = 0;
  2291. target->sync_flag = 0;
  2292. nsp32_set_async(data, target);
  2293. }
  2294. /*
  2295. * EEPROM check
  2296. */
  2297. ret = nsp32_getprom_param(data);
  2298. if (ret == FALSE) {
  2299. data->resettime = 3; /* default 3 */
  2300. }
  2301. /*
  2302. * setup HBA
  2303. */
  2304. nsp32hw_init(data);
  2305. snprintf(data->info_str, sizeof(data->info_str),
  2306. "NinjaSCSI-32Bi/UDE: irq %d, io 0x%lx+0x%x",
  2307. host->irq, host->io_port, host->n_io_port);
  2308. /*
  2309. * SCSI bus reset
  2310. *
  2311. * Note: It's important to reset SCSI bus in initialization phase.
  2312. * NinjaSCSI-32Bi/UDE HBA EEPROM seems to exchange SDTR when
  2313. * system is coming up, so SCSI devices connected to HBA is set as
  2314. * un-asynchronous mode. It brings the merit that this HBA is
  2315. * ready to start synchronous transfer without any preparation,
  2316. * but we are difficult to control transfer speed. In addition,
  2317. * it prevents device transfer speed from effecting EEPROM start-up
  2318. * SDTR. NinjaSCSI-32Bi/UDE has the feature if EEPROM is set as
  2319. * Auto Mode, then FAST-10M is selected when SCSI devices are
  2320. * connected same or more than 4 devices. It should be avoided
  2321. * depending on this specification. Thus, resetting the SCSI bus
  2322. * restores all connected SCSI devices to asynchronous mode, then
  2323. * this driver set SDTR safely later, and we can control all SCSI
  2324. * device transfer mode.
  2325. */
  2326. nsp32_do_bus_reset(data);
  2327. ret = request_irq(host->irq, do_nsp32_isr, IRQF_SHARED, "nsp32", data);
  2328. if (ret < 0) {
  2329. nsp32_msg(KERN_ERR, "Unable to allocate IRQ for NinjaSCSI32 "
  2330. "SCSI PCI controller. Interrupt: %d", host->irq);
  2331. goto free_sg_list;
  2332. }
  2333. /*
  2334. * PCI IO register
  2335. */
  2336. res = request_region(host->io_port, host->n_io_port, "nsp32");
  2337. if (res == NULL) {
  2338. nsp32_msg(KERN_ERR,
  2339. "I/O region 0x%lx+0x%lx is already used",
  2340. data->BaseAddress, data->NumAddress);
  2341. goto free_irq;
  2342. }
  2343. ret = scsi_add_host(host, &pdev->dev);
  2344. if (ret) {
  2345. nsp32_msg(KERN_ERR, "failed to add scsi host");
  2346. goto free_region;
  2347. }
  2348. scsi_scan_host(host);
  2349. pci_set_drvdata(pdev, host);
  2350. return 0;
  2351. free_region:
  2352. release_region(host->io_port, host->n_io_port);
  2353. free_irq:
  2354. free_irq(host->irq, data);
  2355. free_sg_list:
  2356. pci_free_consistent(pdev, NSP32_SG_TABLE_SIZE,
  2357. data->sg_list, data->sg_paddr);
  2358. free_autoparam:
  2359. pci_free_consistent(pdev, sizeof(nsp32_autoparam),
  2360. data->autoparam, data->auto_paddr);
  2361. scsi_unregister:
  2362. scsi_host_put(host);
  2363. err:
  2364. return 1;
  2365. }
  2366. static int nsp32_release(struct Scsi_Host *host)
  2367. {
  2368. nsp32_hw_data *data = (nsp32_hw_data *)host->hostdata;
  2369. if (data->autoparam) {
  2370. pci_free_consistent(data->Pci, sizeof(nsp32_autoparam),
  2371. data->autoparam, data->auto_paddr);
  2372. }
  2373. if (data->sg_list) {
  2374. pci_free_consistent(data->Pci, NSP32_SG_TABLE_SIZE,
  2375. data->sg_list, data->sg_paddr);
  2376. }
  2377. if (host->irq) {
  2378. free_irq(host->irq, data);
  2379. }
  2380. if (host->io_port && host->n_io_port) {
  2381. release_region(host->io_port, host->n_io_port);
  2382. }
  2383. if (data->MmioAddress) {
  2384. iounmap(data->MmioAddress);
  2385. }
  2386. return 0;
  2387. }
  2388. static const char *nsp32_info(struct Scsi_Host *shpnt)
  2389. {
  2390. nsp32_hw_data *data = (nsp32_hw_data *)shpnt->hostdata;
  2391. return data->info_str;
  2392. }
  2393. /****************************************************************************
  2394. * error handler
  2395. */
  2396. static int nsp32_eh_abort(struct scsi_cmnd *SCpnt)
  2397. {
  2398. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  2399. unsigned int base = SCpnt->device->host->io_port;
  2400. nsp32_msg(KERN_WARNING, "abort");
  2401. if (data->cur_lunt->SCpnt == NULL) {
  2402. nsp32_dbg(NSP32_DEBUG_BUSRESET, "abort failed");
  2403. return FAILED;
  2404. }
  2405. if (data->cur_target->sync_flag & (SDTR_INITIATOR | SDTR_TARGET)) {
  2406. /* reset SDTR negotiation */
  2407. data->cur_target->sync_flag = 0;
  2408. nsp32_set_async(data, data->cur_target);
  2409. }
  2410. nsp32_write2(base, TRANSFER_CONTROL, 0);
  2411. nsp32_write2(base, BM_CNT, 0);
  2412. SCpnt->result = DID_ABORT << 16;
  2413. nsp32_scsi_done(SCpnt);
  2414. nsp32_dbg(NSP32_DEBUG_BUSRESET, "abort success");
  2415. return SUCCESS;
  2416. }
  2417. static int nsp32_eh_bus_reset(struct scsi_cmnd *SCpnt)
  2418. {
  2419. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  2420. unsigned int base = SCpnt->device->host->io_port;
  2421. spin_lock_irq(SCpnt->device->host->host_lock);
  2422. nsp32_msg(KERN_INFO, "Bus Reset");
  2423. nsp32_dbg(NSP32_DEBUG_BUSRESET, "SCpnt=0x%x", SCpnt);
  2424. nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
  2425. nsp32_do_bus_reset(data);
  2426. nsp32_write2(base, IRQ_CONTROL, 0);
  2427. spin_unlock_irq(SCpnt->device->host->host_lock);
  2428. return SUCCESS; /* SCSI bus reset is succeeded at any time. */
  2429. }
  2430. static void nsp32_do_bus_reset(nsp32_hw_data *data)
  2431. {
  2432. unsigned int base = data->BaseAddress;
  2433. unsigned short intrdat;
  2434. int i;
  2435. nsp32_dbg(NSP32_DEBUG_BUSRESET, "in");
  2436. /*
  2437. * stop all transfer
  2438. * clear TRANSFERCONTROL_BM_START
  2439. * clear counter
  2440. */
  2441. nsp32_write2(base, TRANSFER_CONTROL, 0);
  2442. nsp32_write4(base, BM_CNT, 0);
  2443. nsp32_write4(base, CLR_COUNTER, CLRCOUNTER_ALLMASK);
  2444. /*
  2445. * fall back to asynchronous transfer mode
  2446. * initialize SDTR negotiation flag
  2447. */
  2448. for (i = 0; i < ARRAY_SIZE(data->target); i++) {
  2449. nsp32_target *target = &data->target[i];
  2450. target->sync_flag = 0;
  2451. nsp32_set_async(data, target);
  2452. }
  2453. /*
  2454. * reset SCSI bus
  2455. */
  2456. nsp32_write1(base, SCSI_BUS_CONTROL, BUSCTL_RST);
  2457. mdelay(RESET_HOLD_TIME / 1000);
  2458. nsp32_write1(base, SCSI_BUS_CONTROL, 0);
  2459. for(i = 0; i < 5; i++) {
  2460. intrdat = nsp32_read2(base, IRQ_STATUS); /* dummy read */
  2461. nsp32_dbg(NSP32_DEBUG_BUSRESET, "irq:1: 0x%x", intrdat);
  2462. }
  2463. data->CurrentSC = NULL;
  2464. }
  2465. static int nsp32_eh_host_reset(struct scsi_cmnd *SCpnt)
  2466. {
  2467. struct Scsi_Host *host = SCpnt->device->host;
  2468. unsigned int base = SCpnt->device->host->io_port;
  2469. nsp32_hw_data *data = (nsp32_hw_data *)host->hostdata;
  2470. nsp32_msg(KERN_INFO, "Host Reset");
  2471. nsp32_dbg(NSP32_DEBUG_BUSRESET, "SCpnt=0x%x", SCpnt);
  2472. spin_lock_irq(SCpnt->device->host->host_lock);
  2473. nsp32hw_init(data);
  2474. nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
  2475. nsp32_do_bus_reset(data);
  2476. nsp32_write2(base, IRQ_CONTROL, 0);
  2477. spin_unlock_irq(SCpnt->device->host->host_lock);
  2478. return SUCCESS; /* Host reset is succeeded at any time. */
  2479. }
  2480. /**************************************************************************
  2481. * EEPROM handler
  2482. */
  2483. /*
  2484. * getting EEPROM parameter
  2485. */
  2486. static int nsp32_getprom_param(nsp32_hw_data *data)
  2487. {
  2488. int vendor = data->pci_devid->vendor;
  2489. int device = data->pci_devid->device;
  2490. int ret, val, i;
  2491. /*
  2492. * EEPROM checking.
  2493. */
  2494. ret = nsp32_prom_read(data, 0x7e);
  2495. if (ret != 0x55) {
  2496. nsp32_msg(KERN_INFO, "No EEPROM detected: 0x%x", ret);
  2497. return FALSE;
  2498. }
  2499. ret = nsp32_prom_read(data, 0x7f);
  2500. if (ret != 0xaa) {
  2501. nsp32_msg(KERN_INFO, "Invalid number: 0x%x", ret);
  2502. return FALSE;
  2503. }
  2504. /*
  2505. * check EEPROM type
  2506. */
  2507. if (vendor == PCI_VENDOR_ID_WORKBIT &&
  2508. device == PCI_DEVICE_ID_WORKBIT_STANDARD) {
  2509. ret = nsp32_getprom_c16(data);
  2510. } else if (vendor == PCI_VENDOR_ID_WORKBIT &&
  2511. device == PCI_DEVICE_ID_NINJASCSI_32BIB_LOGITEC) {
  2512. ret = nsp32_getprom_at24(data);
  2513. } else if (vendor == PCI_VENDOR_ID_WORKBIT &&
  2514. device == PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO ) {
  2515. ret = nsp32_getprom_at24(data);
  2516. } else {
  2517. nsp32_msg(KERN_WARNING, "Unknown EEPROM");
  2518. ret = FALSE;
  2519. }
  2520. /* for debug : SPROM data full checking */
  2521. for (i = 0; i <= 0x1f; i++) {
  2522. val = nsp32_prom_read(data, i);
  2523. nsp32_dbg(NSP32_DEBUG_EEPROM,
  2524. "rom address 0x%x : 0x%x", i, val);
  2525. }
  2526. return ret;
  2527. }
  2528. /*
  2529. * AT24C01A (Logitec: LHA-600S), AT24C02 (Melco Buffalo: IFC-USLP) data map:
  2530. *
  2531. * ROMADDR
  2532. * 0x00 - 0x06 : Device Synchronous Transfer Period (SCSI ID 0 - 6)
  2533. * Value 0x0: ASYNC, 0x0c: Ultra-20M, 0x19: Fast-10M
  2534. * 0x07 : HBA Synchronous Transfer Period
  2535. * Value 0: AutoSync, 1: Manual Setting
  2536. * 0x08 - 0x0f : Not Used? (0x0)
  2537. * 0x10 : Bus Termination
  2538. * Value 0: Auto[ON], 1: ON, 2: OFF
  2539. * 0x11 : Not Used? (0)
  2540. * 0x12 : Bus Reset Delay Time (0x03)
  2541. * 0x13 : Bootable CD Support
  2542. * Value 0: Disable, 1: Enable
  2543. * 0x14 : Device Scan
  2544. * Bit 7 6 5 4 3 2 1 0
  2545. * | <----------------->
  2546. * | SCSI ID: Value 0: Skip, 1: YES
  2547. * |-> Value 0: ALL scan, Value 1: Manual
  2548. * 0x15 - 0x1b : Not Used? (0)
  2549. * 0x1c : Constant? (0x01) (clock div?)
  2550. * 0x1d - 0x7c : Not Used (0xff)
  2551. * 0x7d : Not Used? (0xff)
  2552. * 0x7e : Constant (0x55), Validity signature
  2553. * 0x7f : Constant (0xaa), Validity signature
  2554. */
  2555. static int nsp32_getprom_at24(nsp32_hw_data *data)
  2556. {
  2557. int ret, i;
  2558. int auto_sync;
  2559. nsp32_target *target;
  2560. int entry;
  2561. /*
  2562. * Reset time which is designated by EEPROM.
  2563. *
  2564. * TODO: Not used yet.
  2565. */
  2566. data->resettime = nsp32_prom_read(data, 0x12);
  2567. /*
  2568. * HBA Synchronous Transfer Period
  2569. *
  2570. * Note: auto_sync = 0: auto, 1: manual. Ninja SCSI HBA spec says
  2571. * that if auto_sync is 0 (auto), and connected SCSI devices are
  2572. * same or lower than 3, then transfer speed is set as ULTRA-20M.
  2573. * On the contrary if connected SCSI devices are same or higher
  2574. * than 4, then transfer speed is set as FAST-10M.
  2575. *
  2576. * I break this rule. The number of connected SCSI devices are
  2577. * only ignored. If auto_sync is 0 (auto), then transfer speed is
  2578. * forced as ULTRA-20M.
  2579. */
  2580. ret = nsp32_prom_read(data, 0x07);
  2581. switch (ret) {
  2582. case 0:
  2583. auto_sync = TRUE;
  2584. break;
  2585. case 1:
  2586. auto_sync = FALSE;
  2587. break;
  2588. default:
  2589. nsp32_msg(KERN_WARNING,
  2590. "Unsupported Auto Sync mode. Fall back to manual mode.");
  2591. auto_sync = TRUE;
  2592. }
  2593. if (trans_mode == ULTRA20M_MODE) {
  2594. auto_sync = TRUE;
  2595. }
  2596. /*
  2597. * each device Synchronous Transfer Period
  2598. */
  2599. for (i = 0; i < NSP32_HOST_SCSIID; i++) {
  2600. target = &data->target[i];
  2601. if (auto_sync == TRUE) {
  2602. target->limit_entry = 0; /* set as ULTRA20M */
  2603. } else {
  2604. ret = nsp32_prom_read(data, i);
  2605. entry = nsp32_search_period_entry(data, target, ret);
  2606. if (entry < 0) {
  2607. /* search failed... set maximum speed */
  2608. entry = 0;
  2609. }
  2610. target->limit_entry = entry;
  2611. }
  2612. }
  2613. return TRUE;
  2614. }
  2615. /*
  2616. * C16 110 (I-O Data: SC-NBD) data map:
  2617. *
  2618. * ROMADDR
  2619. * 0x00 - 0x06 : Device Synchronous Transfer Period (SCSI ID 0 - 6)
  2620. * Value 0x0: 20MB/S, 0x1: 10MB/S, 0x2: 5MB/S, 0x3: ASYNC
  2621. * 0x07 : 0 (HBA Synchronous Transfer Period: Auto Sync)
  2622. * 0x08 - 0x0f : Not Used? (0x0)
  2623. * 0x10 : Transfer Mode
  2624. * Value 0: PIO, 1: Busmater
  2625. * 0x11 : Bus Reset Delay Time (0x00-0x20)
  2626. * 0x12 : Bus Termination
  2627. * Value 0: Disable, 1: Enable
  2628. * 0x13 - 0x19 : Disconnection
  2629. * Value 0: Disable, 1: Enable
  2630. * 0x1a - 0x7c : Not Used? (0)
  2631. * 0x7d : Not Used? (0xf8)
  2632. * 0x7e : Constant (0x55), Validity signature
  2633. * 0x7f : Constant (0xaa), Validity signature
  2634. */
  2635. static int nsp32_getprom_c16(nsp32_hw_data *data)
  2636. {
  2637. int ret, i;
  2638. nsp32_target *target;
  2639. int entry, val;
  2640. /*
  2641. * Reset time which is designated by EEPROM.
  2642. *
  2643. * TODO: Not used yet.
  2644. */
  2645. data->resettime = nsp32_prom_read(data, 0x11);
  2646. /*
  2647. * each device Synchronous Transfer Period
  2648. */
  2649. for (i = 0; i < NSP32_HOST_SCSIID; i++) {
  2650. target = &data->target[i];
  2651. ret = nsp32_prom_read(data, i);
  2652. switch (ret) {
  2653. case 0: /* 20MB/s */
  2654. val = 0x0c;
  2655. break;
  2656. case 1: /* 10MB/s */
  2657. val = 0x19;
  2658. break;
  2659. case 2: /* 5MB/s */
  2660. val = 0x32;
  2661. break;
  2662. case 3: /* ASYNC */
  2663. val = 0x00;
  2664. break;
  2665. default: /* default 20MB/s */
  2666. val = 0x0c;
  2667. break;
  2668. }
  2669. entry = nsp32_search_period_entry(data, target, val);
  2670. if (entry < 0 || trans_mode == ULTRA20M_MODE) {
  2671. /* search failed... set maximum speed */
  2672. entry = 0;
  2673. }
  2674. target->limit_entry = entry;
  2675. }
  2676. return TRUE;
  2677. }
  2678. /*
  2679. * Atmel AT24C01A (drived in 5V) serial EEPROM routines
  2680. */
  2681. static int nsp32_prom_read(nsp32_hw_data *data, int romaddr)
  2682. {
  2683. int i, val;
  2684. /* start condition */
  2685. nsp32_prom_start(data);
  2686. /* device address */
  2687. nsp32_prom_write_bit(data, 1); /* 1 */
  2688. nsp32_prom_write_bit(data, 0); /* 0 */
  2689. nsp32_prom_write_bit(data, 1); /* 1 */
  2690. nsp32_prom_write_bit(data, 0); /* 0 */
  2691. nsp32_prom_write_bit(data, 0); /* A2: 0 (GND) */
  2692. nsp32_prom_write_bit(data, 0); /* A1: 0 (GND) */
  2693. nsp32_prom_write_bit(data, 0); /* A0: 0 (GND) */
  2694. /* R/W: W for dummy write */
  2695. nsp32_prom_write_bit(data, 0);
  2696. /* ack */
  2697. nsp32_prom_write_bit(data, 0);
  2698. /* word address */
  2699. for (i = 7; i >= 0; i--) {
  2700. nsp32_prom_write_bit(data, ((romaddr >> i) & 1));
  2701. }
  2702. /* ack */
  2703. nsp32_prom_write_bit(data, 0);
  2704. /* start condition */
  2705. nsp32_prom_start(data);
  2706. /* device address */
  2707. nsp32_prom_write_bit(data, 1); /* 1 */
  2708. nsp32_prom_write_bit(data, 0); /* 0 */
  2709. nsp32_prom_write_bit(data, 1); /* 1 */
  2710. nsp32_prom_write_bit(data, 0); /* 0 */
  2711. nsp32_prom_write_bit(data, 0); /* A2: 0 (GND) */
  2712. nsp32_prom_write_bit(data, 0); /* A1: 0 (GND) */
  2713. nsp32_prom_write_bit(data, 0); /* A0: 0 (GND) */
  2714. /* R/W: R */
  2715. nsp32_prom_write_bit(data, 1);
  2716. /* ack */
  2717. nsp32_prom_write_bit(data, 0);
  2718. /* data... */
  2719. val = 0;
  2720. for (i = 7; i >= 0; i--) {
  2721. val += (nsp32_prom_read_bit(data) << i);
  2722. }
  2723. /* no ack */
  2724. nsp32_prom_write_bit(data, 1);
  2725. /* stop condition */
  2726. nsp32_prom_stop(data);
  2727. return val;
  2728. }
  2729. static void nsp32_prom_set(nsp32_hw_data *data, int bit, int val)
  2730. {
  2731. int base = data->BaseAddress;
  2732. int tmp;
  2733. tmp = nsp32_index_read1(base, SERIAL_ROM_CTL);
  2734. if (val == 0) {
  2735. tmp &= ~bit;
  2736. } else {
  2737. tmp |= bit;
  2738. }
  2739. nsp32_index_write1(base, SERIAL_ROM_CTL, tmp);
  2740. udelay(10);
  2741. }
  2742. static int nsp32_prom_get(nsp32_hw_data *data, int bit)
  2743. {
  2744. int base = data->BaseAddress;
  2745. int tmp, ret;
  2746. if (bit != SDA) {
  2747. nsp32_msg(KERN_ERR, "return value is not appropriate");
  2748. return 0;
  2749. }
  2750. tmp = nsp32_index_read1(base, SERIAL_ROM_CTL) & bit;
  2751. if (tmp == 0) {
  2752. ret = 0;
  2753. } else {
  2754. ret = 1;
  2755. }
  2756. udelay(10);
  2757. return ret;
  2758. }
  2759. static void nsp32_prom_start (nsp32_hw_data *data)
  2760. {
  2761. /* start condition */
  2762. nsp32_prom_set(data, SCL, 1);
  2763. nsp32_prom_set(data, SDA, 1);
  2764. nsp32_prom_set(data, ENA, 1); /* output mode */
  2765. nsp32_prom_set(data, SDA, 0); /* keeping SCL=1 and transiting
  2766. * SDA 1->0 is start condition */
  2767. nsp32_prom_set(data, SCL, 0);
  2768. }
  2769. static void nsp32_prom_stop (nsp32_hw_data *data)
  2770. {
  2771. /* stop condition */
  2772. nsp32_prom_set(data, SCL, 1);
  2773. nsp32_prom_set(data, SDA, 0);
  2774. nsp32_prom_set(data, ENA, 1); /* output mode */
  2775. nsp32_prom_set(data, SDA, 1);
  2776. nsp32_prom_set(data, SCL, 0);
  2777. }
  2778. static void nsp32_prom_write_bit(nsp32_hw_data *data, int val)
  2779. {
  2780. /* write */
  2781. nsp32_prom_set(data, SDA, val);
  2782. nsp32_prom_set(data, SCL, 1 );
  2783. nsp32_prom_set(data, SCL, 0 );
  2784. }
  2785. static int nsp32_prom_read_bit(nsp32_hw_data *data)
  2786. {
  2787. int val;
  2788. /* read */
  2789. nsp32_prom_set(data, ENA, 0); /* input mode */
  2790. nsp32_prom_set(data, SCL, 1);
  2791. val = nsp32_prom_get(data, SDA);
  2792. nsp32_prom_set(data, SCL, 0);
  2793. nsp32_prom_set(data, ENA, 1); /* output mode */
  2794. return val;
  2795. }
  2796. /**************************************************************************
  2797. * Power Management
  2798. */
  2799. #ifdef CONFIG_PM
  2800. /* Device suspended */
  2801. static int nsp32_suspend(struct pci_dev *pdev, pm_message_t state)
  2802. {
  2803. struct Scsi_Host *host = pci_get_drvdata(pdev);
  2804. nsp32_msg(KERN_INFO, "pci-suspend: pdev=0x%p, state=%ld, slot=%s, host=0x%p", pdev, state, pci_name(pdev), host);
  2805. pci_save_state (pdev);
  2806. pci_disable_device (pdev);
  2807. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2808. return 0;
  2809. }
  2810. /* Device woken up */
  2811. static int nsp32_resume(struct pci_dev *pdev)
  2812. {
  2813. struct Scsi_Host *host = pci_get_drvdata(pdev);
  2814. nsp32_hw_data *data = (nsp32_hw_data *)host->hostdata;
  2815. unsigned short reg;
  2816. nsp32_msg(KERN_INFO, "pci-resume: pdev=0x%p, slot=%s, host=0x%p", pdev, pci_name(pdev), host);
  2817. pci_set_power_state(pdev, PCI_D0);
  2818. pci_enable_wake (pdev, PCI_D0, 0);
  2819. pci_restore_state (pdev);
  2820. reg = nsp32_read2(data->BaseAddress, INDEX_REG);
  2821. nsp32_msg(KERN_INFO, "io=0x%x reg=0x%x", data->BaseAddress, reg);
  2822. if (reg == 0xffff) {
  2823. nsp32_msg(KERN_INFO, "missing device. abort resume.");
  2824. return 0;
  2825. }
  2826. nsp32hw_init (data);
  2827. nsp32_do_bus_reset(data);
  2828. nsp32_msg(KERN_INFO, "resume success");
  2829. return 0;
  2830. }
  2831. #endif
  2832. /************************************************************************
  2833. * PCI/Cardbus probe/remove routine
  2834. */
  2835. static int nsp32_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  2836. {
  2837. int ret;
  2838. nsp32_hw_data *data = &nsp32_data_base;
  2839. nsp32_dbg(NSP32_DEBUG_REGISTER, "enter");
  2840. ret = pci_enable_device(pdev);
  2841. if (ret) {
  2842. nsp32_msg(KERN_ERR, "failed to enable pci device");
  2843. return ret;
  2844. }
  2845. data->Pci = pdev;
  2846. data->pci_devid = id;
  2847. data->IrqNumber = pdev->irq;
  2848. data->BaseAddress = pci_resource_start(pdev, 0);
  2849. data->NumAddress = pci_resource_len (pdev, 0);
  2850. data->MmioAddress = pci_ioremap_bar(pdev, 1);
  2851. data->MmioLength = pci_resource_len (pdev, 1);
  2852. pci_set_master(pdev);
  2853. ret = nsp32_detect(pdev);
  2854. nsp32_msg(KERN_INFO, "irq: %i mmio: %p+0x%lx slot: %s model: %s",
  2855. pdev->irq,
  2856. data->MmioAddress, data->MmioLength,
  2857. pci_name(pdev),
  2858. nsp32_model[id->driver_data]);
  2859. nsp32_dbg(NSP32_DEBUG_REGISTER, "exit %d", ret);
  2860. return ret;
  2861. }
  2862. static void nsp32_remove(struct pci_dev *pdev)
  2863. {
  2864. struct Scsi_Host *host = pci_get_drvdata(pdev);
  2865. nsp32_dbg(NSP32_DEBUG_REGISTER, "enter");
  2866. scsi_remove_host(host);
  2867. nsp32_release(host);
  2868. scsi_host_put(host);
  2869. }
  2870. static struct pci_driver nsp32_driver = {
  2871. .name = "nsp32",
  2872. .id_table = nsp32_pci_table,
  2873. .probe = nsp32_probe,
  2874. .remove = nsp32_remove,
  2875. #ifdef CONFIG_PM
  2876. .suspend = nsp32_suspend,
  2877. .resume = nsp32_resume,
  2878. #endif
  2879. };
  2880. /*********************************************************************
  2881. * Moule entry point
  2882. */
  2883. static int __init init_nsp32(void) {
  2884. nsp32_msg(KERN_INFO, "loading...");
  2885. return pci_register_driver(&nsp32_driver);
  2886. }
  2887. static void __exit exit_nsp32(void) {
  2888. nsp32_msg(KERN_INFO, "unloading...");
  2889. pci_unregister_driver(&nsp32_driver);
  2890. }
  2891. module_init(init_nsp32);
  2892. module_exit(exit_nsp32);
  2893. /* end */