nsp32.c 88 KB

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