cciss.c 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577
  1. /*
  2. * Disk Array driver for HP Smart Array controllers.
  3. * (C) Copyright 2000, 2007 Hewlett-Packard Development Company, L.P.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. * 02111-1307, USA.
  18. *
  19. * Questions/Comments/Bugfixes to iss_storagedev@hp.com
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/types.h>
  25. #include <linux/pci.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/delay.h>
  30. #include <linux/major.h>
  31. #include <linux/fs.h>
  32. #include <linux/bio.h>
  33. #include <linux/blkpg.h>
  34. #include <linux/timer.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/init.h>
  38. #include <linux/jiffies.h>
  39. #include <linux/hdreg.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/compat.h>
  42. #include <linux/mutex.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/io.h>
  45. #include <linux/dma-mapping.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/genhd.h>
  48. #include <linux/completion.h>
  49. #include <scsi/scsi.h>
  50. #include <scsi/sg.h>
  51. #include <scsi/scsi_ioctl.h>
  52. #include <linux/cdrom.h>
  53. #include <linux/scatterlist.h>
  54. #include <linux/kthread.h>
  55. #define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
  56. #define DRIVER_NAME "HP CISS Driver (v 3.6.20)"
  57. #define DRIVER_VERSION CCISS_DRIVER_VERSION(3, 6, 20)
  58. /* Embedded module documentation macros - see modules.h */
  59. MODULE_AUTHOR("Hewlett-Packard Company");
  60. MODULE_DESCRIPTION("Driver for HP Smart Array Controllers");
  61. MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
  62. " SA6i P600 P800 P400 P400i E200 E200i E500 P700m"
  63. " Smart Array G2 Series SAS/SATA Controllers");
  64. MODULE_VERSION("3.6.20");
  65. MODULE_LICENSE("GPL");
  66. #include "cciss_cmd.h"
  67. #include "cciss.h"
  68. #include <linux/cciss_ioctl.h>
  69. /* define the PCI info for the cards we can control */
  70. static const struct pci_device_id cciss_pci_device_id[] = {
  71. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS, 0x0E11, 0x4070},
  72. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4080},
  73. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4082},
  74. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4083},
  75. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x4091},
  76. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409A},
  77. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409B},
  78. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409C},
  79. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409D},
  80. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSA, 0x103C, 0x3225},
  81. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3223},
  82. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3234},
  83. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3235},
  84. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3211},
  85. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3212},
  86. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3213},
  87. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3214},
  88. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3215},
  89. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3237},
  90. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x323D},
  91. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
  92. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
  93. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
  94. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
  95. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
  96. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A},
  97. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B},
  98. {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  99. PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
  100. {0,}
  101. };
  102. MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
  103. /* board_id = Subsystem Device ID & Vendor ID
  104. * product = Marketing Name for the board
  105. * access = Address of the struct of function pointers
  106. */
  107. static struct board_type products[] = {
  108. {0x40700E11, "Smart Array 5300", &SA5_access},
  109. {0x40800E11, "Smart Array 5i", &SA5B_access},
  110. {0x40820E11, "Smart Array 532", &SA5B_access},
  111. {0x40830E11, "Smart Array 5312", &SA5B_access},
  112. {0x409A0E11, "Smart Array 641", &SA5_access},
  113. {0x409B0E11, "Smart Array 642", &SA5_access},
  114. {0x409C0E11, "Smart Array 6400", &SA5_access},
  115. {0x409D0E11, "Smart Array 6400 EM", &SA5_access},
  116. {0x40910E11, "Smart Array 6i", &SA5_access},
  117. {0x3225103C, "Smart Array P600", &SA5_access},
  118. {0x3223103C, "Smart Array P800", &SA5_access},
  119. {0x3234103C, "Smart Array P400", &SA5_access},
  120. {0x3235103C, "Smart Array P400i", &SA5_access},
  121. {0x3211103C, "Smart Array E200i", &SA5_access},
  122. {0x3212103C, "Smart Array E200", &SA5_access},
  123. {0x3213103C, "Smart Array E200i", &SA5_access},
  124. {0x3214103C, "Smart Array E200i", &SA5_access},
  125. {0x3215103C, "Smart Array E200i", &SA5_access},
  126. {0x3237103C, "Smart Array E500", &SA5_access},
  127. {0x323D103C, "Smart Array P700m", &SA5_access},
  128. {0x3241103C, "Smart Array P212", &SA5_access},
  129. {0x3243103C, "Smart Array P410", &SA5_access},
  130. {0x3245103C, "Smart Array P410i", &SA5_access},
  131. {0x3247103C, "Smart Array P411", &SA5_access},
  132. {0x3249103C, "Smart Array P812", &SA5_access},
  133. {0x324A103C, "Smart Array P712m", &SA5_access},
  134. {0x324B103C, "Smart Array P711m", &SA5_access},
  135. {0xFFFF103C, "Unknown Smart Array", &SA5_access},
  136. };
  137. /* How long to wait (in milliseconds) for board to go into simple mode */
  138. #define MAX_CONFIG_WAIT 30000
  139. #define MAX_IOCTL_CONFIG_WAIT 1000
  140. /*define how many times we will try a command because of bus resets */
  141. #define MAX_CMD_RETRIES 3
  142. #define MAX_CTLR 32
  143. /* Originally cciss driver only supports 8 major numbers */
  144. #define MAX_CTLR_ORIG 8
  145. static ctlr_info_t *hba[MAX_CTLR];
  146. static struct task_struct *cciss_scan_thread;
  147. static DEFINE_MUTEX(scan_mutex);
  148. static LIST_HEAD(scan_q);
  149. static void do_cciss_request(struct request_queue *q);
  150. static irqreturn_t do_cciss_intr(int irq, void *dev_id);
  151. static int cciss_open(struct block_device *bdev, fmode_t mode);
  152. static int cciss_release(struct gendisk *disk, fmode_t mode);
  153. static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
  154. unsigned int cmd, unsigned long arg);
  155. static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo);
  156. static int cciss_revalidate(struct gendisk *disk);
  157. static int rebuild_lun_table(ctlr_info_t *h, int first_time, int via_ioctl);
  158. static int deregister_disk(ctlr_info_t *h, int drv_index,
  159. int clear_all, int via_ioctl);
  160. static void cciss_read_capacity(int ctlr, int logvol, int withirq,
  161. sector_t *total_size, unsigned int *block_size);
  162. static void cciss_read_capacity_16(int ctlr, int logvol, int withirq,
  163. sector_t *total_size, unsigned int *block_size);
  164. static void cciss_geometry_inquiry(int ctlr, int logvol,
  165. int withirq, sector_t total_size,
  166. unsigned int block_size, InquiryData_struct *inq_buff,
  167. drive_info_struct *drv);
  168. static void __devinit cciss_interrupt_mode(ctlr_info_t *, struct pci_dev *,
  169. __u32);
  170. static void start_io(ctlr_info_t *h);
  171. static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size,
  172. __u8 page_code, unsigned char *scsi3addr, int cmd_type);
  173. static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size,
  174. __u8 page_code, unsigned char scsi3addr[],
  175. int cmd_type);
  176. static int sendcmd_withirq_core(ctlr_info_t *h, CommandList_struct *c,
  177. int attempt_retry);
  178. static int process_sendcmd_error(ctlr_info_t *h, CommandList_struct *c);
  179. static void fail_all_cmds(unsigned long ctlr);
  180. static int add_to_scan_list(struct ctlr_info *h);
  181. static int scan_thread(void *data);
  182. static int check_for_unit_attention(ctlr_info_t *h, CommandList_struct *c);
  183. static void cciss_hba_release(struct device *dev);
  184. static void cciss_device_release(struct device *dev);
  185. static void cciss_free_gendisk(ctlr_info_t *h, int drv_index);
  186. #ifdef CONFIG_PROC_FS
  187. static void cciss_procinit(int i);
  188. #else
  189. static void cciss_procinit(int i)
  190. {
  191. }
  192. #endif /* CONFIG_PROC_FS */
  193. #ifdef CONFIG_COMPAT
  194. static int cciss_compat_ioctl(struct block_device *, fmode_t,
  195. unsigned, unsigned long);
  196. #endif
  197. static const struct block_device_operations cciss_fops = {
  198. .owner = THIS_MODULE,
  199. .open = cciss_open,
  200. .release = cciss_release,
  201. .locked_ioctl = cciss_ioctl,
  202. .getgeo = cciss_getgeo,
  203. #ifdef CONFIG_COMPAT
  204. .compat_ioctl = cciss_compat_ioctl,
  205. #endif
  206. .revalidate_disk = cciss_revalidate,
  207. };
  208. /*
  209. * Enqueuing and dequeuing functions for cmdlists.
  210. */
  211. static inline void addQ(struct hlist_head *list, CommandList_struct *c)
  212. {
  213. hlist_add_head(&c->list, list);
  214. }
  215. static inline void removeQ(CommandList_struct *c)
  216. {
  217. /*
  218. * After kexec/dump some commands might still
  219. * be in flight, which the firmware will try
  220. * to complete. Resetting the firmware doesn't work
  221. * with old fw revisions, so we have to mark
  222. * them off as 'stale' to prevent the driver from
  223. * falling over.
  224. */
  225. if (WARN_ON(hlist_unhashed(&c->list))) {
  226. c->cmd_type = CMD_MSG_STALE;
  227. return;
  228. }
  229. hlist_del_init(&c->list);
  230. }
  231. #include "cciss_scsi.c" /* For SCSI tape support */
  232. #ifdef CONFIG_PROC_FS
  233. /*
  234. * Report information about this controller.
  235. */
  236. #define ENG_GIG 1000000000
  237. #define ENG_GIG_FACTOR (ENG_GIG/512)
  238. #define ENGAGE_SCSI "engage scsi"
  239. static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
  240. "UNKNOWN"
  241. };
  242. #define RAID_UNKNOWN (sizeof(raid_label) / sizeof(raid_label[0])-1)
  243. static struct proc_dir_entry *proc_cciss;
  244. static void cciss_seq_show_header(struct seq_file *seq)
  245. {
  246. ctlr_info_t *h = seq->private;
  247. seq_printf(seq, "%s: HP %s Controller\n"
  248. "Board ID: 0x%08lx\n"
  249. "Firmware Version: %c%c%c%c\n"
  250. "IRQ: %d\n"
  251. "Logical drives: %d\n"
  252. "Current Q depth: %d\n"
  253. "Current # commands on controller: %d\n"
  254. "Max Q depth since init: %d\n"
  255. "Max # commands on controller since init: %d\n"
  256. "Max SG entries since init: %d\n",
  257. h->devname,
  258. h->product_name,
  259. (unsigned long)h->board_id,
  260. h->firm_ver[0], h->firm_ver[1], h->firm_ver[2],
  261. h->firm_ver[3], (unsigned int)h->intr[SIMPLE_MODE_INT],
  262. h->num_luns,
  263. h->Qdepth, h->commands_outstanding,
  264. h->maxQsinceinit, h->max_outstanding, h->maxSG);
  265. #ifdef CONFIG_CISS_SCSI_TAPE
  266. cciss_seq_tape_report(seq, h->ctlr);
  267. #endif /* CONFIG_CISS_SCSI_TAPE */
  268. }
  269. static void *cciss_seq_start(struct seq_file *seq, loff_t *pos)
  270. {
  271. ctlr_info_t *h = seq->private;
  272. unsigned ctlr = h->ctlr;
  273. unsigned long flags;
  274. /* prevent displaying bogus info during configuration
  275. * or deconfiguration of a logical volume
  276. */
  277. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  278. if (h->busy_configuring) {
  279. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  280. return ERR_PTR(-EBUSY);
  281. }
  282. h->busy_configuring = 1;
  283. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  284. if (*pos == 0)
  285. cciss_seq_show_header(seq);
  286. return pos;
  287. }
  288. static int cciss_seq_show(struct seq_file *seq, void *v)
  289. {
  290. sector_t vol_sz, vol_sz_frac;
  291. ctlr_info_t *h = seq->private;
  292. unsigned ctlr = h->ctlr;
  293. loff_t *pos = v;
  294. drive_info_struct *drv = &h->drv[*pos];
  295. if (*pos > h->highest_lun)
  296. return 0;
  297. if (drv->heads == 0)
  298. return 0;
  299. vol_sz = drv->nr_blocks;
  300. vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
  301. vol_sz_frac *= 100;
  302. sector_div(vol_sz_frac, ENG_GIG_FACTOR);
  303. if (drv->raid_level < 0 || drv->raid_level > RAID_UNKNOWN)
  304. drv->raid_level = RAID_UNKNOWN;
  305. seq_printf(seq, "cciss/c%dd%d:"
  306. "\t%4u.%02uGB\tRAID %s\n",
  307. ctlr, (int) *pos, (int)vol_sz, (int)vol_sz_frac,
  308. raid_label[drv->raid_level]);
  309. return 0;
  310. }
  311. static void *cciss_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  312. {
  313. ctlr_info_t *h = seq->private;
  314. if (*pos > h->highest_lun)
  315. return NULL;
  316. *pos += 1;
  317. return pos;
  318. }
  319. static void cciss_seq_stop(struct seq_file *seq, void *v)
  320. {
  321. ctlr_info_t *h = seq->private;
  322. /* Only reset h->busy_configuring if we succeeded in setting
  323. * it during cciss_seq_start. */
  324. if (v == ERR_PTR(-EBUSY))
  325. return;
  326. h->busy_configuring = 0;
  327. }
  328. static const struct seq_operations cciss_seq_ops = {
  329. .start = cciss_seq_start,
  330. .show = cciss_seq_show,
  331. .next = cciss_seq_next,
  332. .stop = cciss_seq_stop,
  333. };
  334. static int cciss_seq_open(struct inode *inode, struct file *file)
  335. {
  336. int ret = seq_open(file, &cciss_seq_ops);
  337. struct seq_file *seq = file->private_data;
  338. if (!ret)
  339. seq->private = PDE(inode)->data;
  340. return ret;
  341. }
  342. static ssize_t
  343. cciss_proc_write(struct file *file, const char __user *buf,
  344. size_t length, loff_t *ppos)
  345. {
  346. int err;
  347. char *buffer;
  348. #ifndef CONFIG_CISS_SCSI_TAPE
  349. return -EINVAL;
  350. #endif
  351. if (!buf || length > PAGE_SIZE - 1)
  352. return -EINVAL;
  353. buffer = (char *)__get_free_page(GFP_KERNEL);
  354. if (!buffer)
  355. return -ENOMEM;
  356. err = -EFAULT;
  357. if (copy_from_user(buffer, buf, length))
  358. goto out;
  359. buffer[length] = '\0';
  360. #ifdef CONFIG_CISS_SCSI_TAPE
  361. if (strncmp(ENGAGE_SCSI, buffer, sizeof ENGAGE_SCSI - 1) == 0) {
  362. struct seq_file *seq = file->private_data;
  363. ctlr_info_t *h = seq->private;
  364. int rc;
  365. rc = cciss_engage_scsi(h->ctlr);
  366. if (rc != 0)
  367. err = -rc;
  368. else
  369. err = length;
  370. } else
  371. #endif /* CONFIG_CISS_SCSI_TAPE */
  372. err = -EINVAL;
  373. /* might be nice to have "disengage" too, but it's not
  374. safely possible. (only 1 module use count, lock issues.) */
  375. out:
  376. free_page((unsigned long)buffer);
  377. return err;
  378. }
  379. static struct file_operations cciss_proc_fops = {
  380. .owner = THIS_MODULE,
  381. .open = cciss_seq_open,
  382. .read = seq_read,
  383. .llseek = seq_lseek,
  384. .release = seq_release,
  385. .write = cciss_proc_write,
  386. };
  387. static void __devinit cciss_procinit(int i)
  388. {
  389. struct proc_dir_entry *pde;
  390. if (proc_cciss == NULL)
  391. proc_cciss = proc_mkdir("driver/cciss", NULL);
  392. if (!proc_cciss)
  393. return;
  394. pde = proc_create_data(hba[i]->devname, S_IWUSR | S_IRUSR | S_IRGRP |
  395. S_IROTH, proc_cciss,
  396. &cciss_proc_fops, hba[i]);
  397. }
  398. #endif /* CONFIG_PROC_FS */
  399. #define MAX_PRODUCT_NAME_LEN 19
  400. #define to_hba(n) container_of(n, struct ctlr_info, dev)
  401. static ssize_t host_store_rescan(struct device *dev,
  402. struct device_attribute *attr,
  403. const char *buf, size_t count)
  404. {
  405. struct ctlr_info *h = to_hba(dev);
  406. add_to_scan_list(h);
  407. wake_up_process(cciss_scan_thread);
  408. wait_for_completion_interruptible(&h->scan_wait);
  409. return count;
  410. }
  411. DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
  412. static ssize_t dev_show_unique_id(struct device *dev,
  413. struct device_attribute *attr,
  414. char *buf)
  415. {
  416. drive_info_struct *drv = dev_get_drvdata(dev);
  417. struct ctlr_info *h = to_hba(drv->dev->parent);
  418. __u8 sn[16];
  419. unsigned long flags;
  420. int ret = 0;
  421. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  422. if (h->busy_configuring)
  423. ret = -EBUSY;
  424. else
  425. memcpy(sn, drv->serial_no, sizeof(sn));
  426. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  427. if (ret)
  428. return ret;
  429. else
  430. return snprintf(buf, 16 * 2 + 2,
  431. "%02X%02X%02X%02X%02X%02X%02X%02X"
  432. "%02X%02X%02X%02X%02X%02X%02X%02X\n",
  433. sn[0], sn[1], sn[2], sn[3],
  434. sn[4], sn[5], sn[6], sn[7],
  435. sn[8], sn[9], sn[10], sn[11],
  436. sn[12], sn[13], sn[14], sn[15]);
  437. }
  438. DEVICE_ATTR(unique_id, S_IRUGO, dev_show_unique_id, NULL);
  439. static ssize_t dev_show_vendor(struct device *dev,
  440. struct device_attribute *attr,
  441. char *buf)
  442. {
  443. drive_info_struct *drv = dev_get_drvdata(dev);
  444. struct ctlr_info *h = to_hba(drv->dev->parent);
  445. char vendor[VENDOR_LEN + 1];
  446. unsigned long flags;
  447. int ret = 0;
  448. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  449. if (h->busy_configuring)
  450. ret = -EBUSY;
  451. else
  452. memcpy(vendor, drv->vendor, VENDOR_LEN + 1);
  453. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  454. if (ret)
  455. return ret;
  456. else
  457. return snprintf(buf, sizeof(vendor) + 1, "%s\n", drv->vendor);
  458. }
  459. DEVICE_ATTR(vendor, S_IRUGO, dev_show_vendor, NULL);
  460. static ssize_t dev_show_model(struct device *dev,
  461. struct device_attribute *attr,
  462. char *buf)
  463. {
  464. drive_info_struct *drv = dev_get_drvdata(dev);
  465. struct ctlr_info *h = to_hba(drv->dev->parent);
  466. char model[MODEL_LEN + 1];
  467. unsigned long flags;
  468. int ret = 0;
  469. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  470. if (h->busy_configuring)
  471. ret = -EBUSY;
  472. else
  473. memcpy(model, drv->model, MODEL_LEN + 1);
  474. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  475. if (ret)
  476. return ret;
  477. else
  478. return snprintf(buf, sizeof(model) + 1, "%s\n", drv->model);
  479. }
  480. DEVICE_ATTR(model, S_IRUGO, dev_show_model, NULL);
  481. static ssize_t dev_show_rev(struct device *dev,
  482. struct device_attribute *attr,
  483. char *buf)
  484. {
  485. drive_info_struct *drv = dev_get_drvdata(dev);
  486. struct ctlr_info *h = to_hba(drv->dev->parent);
  487. char rev[REV_LEN + 1];
  488. unsigned long flags;
  489. int ret = 0;
  490. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  491. if (h->busy_configuring)
  492. ret = -EBUSY;
  493. else
  494. memcpy(rev, drv->rev, REV_LEN + 1);
  495. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  496. if (ret)
  497. return ret;
  498. else
  499. return snprintf(buf, sizeof(rev) + 1, "%s\n", drv->rev);
  500. }
  501. DEVICE_ATTR(rev, S_IRUGO, dev_show_rev, NULL);
  502. static ssize_t cciss_show_lunid(struct device *dev,
  503. struct device_attribute *attr, char *buf)
  504. {
  505. drive_info_struct *drv = dev_get_drvdata(dev);
  506. struct ctlr_info *h = to_hba(drv->dev->parent);
  507. unsigned long flags;
  508. unsigned char lunid[8];
  509. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  510. if (h->busy_configuring) {
  511. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  512. return -EBUSY;
  513. }
  514. if (!drv->heads) {
  515. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  516. return -ENOTTY;
  517. }
  518. memcpy(lunid, drv->LunID, sizeof(lunid));
  519. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  520. return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  521. lunid[0], lunid[1], lunid[2], lunid[3],
  522. lunid[4], lunid[5], lunid[6], lunid[7]);
  523. }
  524. DEVICE_ATTR(lunid, S_IRUGO, cciss_show_lunid, NULL);
  525. static ssize_t cciss_show_raid_level(struct device *dev,
  526. struct device_attribute *attr, char *buf)
  527. {
  528. drive_info_struct *drv = dev_get_drvdata(dev);
  529. struct ctlr_info *h = to_hba(drv->dev->parent);
  530. int raid;
  531. unsigned long flags;
  532. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  533. if (h->busy_configuring) {
  534. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  535. return -EBUSY;
  536. }
  537. raid = drv->raid_level;
  538. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  539. if (raid < 0 || raid > RAID_UNKNOWN)
  540. raid = RAID_UNKNOWN;
  541. return snprintf(buf, strlen(raid_label[raid]) + 7, "RAID %s\n",
  542. raid_label[raid]);
  543. }
  544. DEVICE_ATTR(raid_level, S_IRUGO, cciss_show_raid_level, NULL);
  545. static ssize_t cciss_show_usage_count(struct device *dev,
  546. struct device_attribute *attr, char *buf)
  547. {
  548. drive_info_struct *drv = dev_get_drvdata(dev);
  549. struct ctlr_info *h = to_hba(drv->dev->parent);
  550. unsigned long flags;
  551. int count;
  552. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  553. if (h->busy_configuring) {
  554. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  555. return -EBUSY;
  556. }
  557. count = drv->usage_count;
  558. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  559. return snprintf(buf, 20, "%d\n", count);
  560. }
  561. DEVICE_ATTR(usage_count, S_IRUGO, cciss_show_usage_count, NULL);
  562. static struct attribute *cciss_host_attrs[] = {
  563. &dev_attr_rescan.attr,
  564. NULL
  565. };
  566. static struct attribute_group cciss_host_attr_group = {
  567. .attrs = cciss_host_attrs,
  568. };
  569. static struct attribute_group *cciss_host_attr_groups[] = {
  570. &cciss_host_attr_group,
  571. NULL
  572. };
  573. static struct device_type cciss_host_type = {
  574. .name = "cciss_host",
  575. .groups = cciss_host_attr_groups,
  576. .release = cciss_hba_release,
  577. };
  578. static struct attribute *cciss_dev_attrs[] = {
  579. &dev_attr_unique_id.attr,
  580. &dev_attr_model.attr,
  581. &dev_attr_vendor.attr,
  582. &dev_attr_rev.attr,
  583. &dev_attr_lunid.attr,
  584. &dev_attr_raid_level.attr,
  585. &dev_attr_usage_count.attr,
  586. NULL
  587. };
  588. static struct attribute_group cciss_dev_attr_group = {
  589. .attrs = cciss_dev_attrs,
  590. };
  591. static const struct attribute_group *cciss_dev_attr_groups[] = {
  592. &cciss_dev_attr_group,
  593. NULL
  594. };
  595. static struct device_type cciss_dev_type = {
  596. .name = "cciss_device",
  597. .groups = cciss_dev_attr_groups,
  598. .release = cciss_device_release,
  599. };
  600. static struct bus_type cciss_bus_type = {
  601. .name = "cciss",
  602. };
  603. /*
  604. * cciss_hba_release is called when the reference count
  605. * of h->dev goes to zero.
  606. */
  607. static void cciss_hba_release(struct device *dev)
  608. {
  609. /*
  610. * nothing to do, but need this to avoid a warning
  611. * about not having a release handler from lib/kref.c.
  612. */
  613. }
  614. /*
  615. * Initialize sysfs entry for each controller. This sets up and registers
  616. * the 'cciss#' directory for each individual controller under
  617. * /sys/bus/pci/devices/<dev>/.
  618. */
  619. static int cciss_create_hba_sysfs_entry(struct ctlr_info *h)
  620. {
  621. device_initialize(&h->dev);
  622. h->dev.type = &cciss_host_type;
  623. h->dev.bus = &cciss_bus_type;
  624. dev_set_name(&h->dev, "%s", h->devname);
  625. h->dev.parent = &h->pdev->dev;
  626. return device_add(&h->dev);
  627. }
  628. /*
  629. * Remove sysfs entries for an hba.
  630. */
  631. static void cciss_destroy_hba_sysfs_entry(struct ctlr_info *h)
  632. {
  633. device_del(&h->dev);
  634. put_device(&h->dev); /* final put. */
  635. }
  636. /* cciss_device_release is called when the reference count
  637. * of h->drv[x].dev goes to zero.
  638. */
  639. static void cciss_device_release(struct device *dev)
  640. {
  641. kfree(dev);
  642. }
  643. /*
  644. * Initialize sysfs for each logical drive. This sets up and registers
  645. * the 'c#d#' directory for each individual logical drive under
  646. * /sys/bus/pci/devices/<dev/ccis#/. We also create a link from
  647. * /sys/block/cciss!c#d# to this entry.
  648. */
  649. static long cciss_create_ld_sysfs_entry(struct ctlr_info *h,
  650. int drv_index)
  651. {
  652. struct device *dev;
  653. /* Special case for c*d0, we only create it once. */
  654. if (drv_index == 0 && h->drv[drv_index].dev != NULL)
  655. return 0;
  656. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  657. if (!dev)
  658. return -ENOMEM;
  659. device_initialize(dev);
  660. dev->type = &cciss_dev_type;
  661. dev->bus = &cciss_bus_type;
  662. dev_set_name(dev, "c%dd%d", h->ctlr, drv_index);
  663. dev->parent = &h->dev;
  664. h->drv[drv_index].dev = dev;
  665. dev_set_drvdata(dev, &h->drv[drv_index]);
  666. return device_add(dev);
  667. }
  668. /*
  669. * Remove sysfs entries for a logical drive.
  670. */
  671. static void cciss_destroy_ld_sysfs_entry(struct ctlr_info *h, int drv_index,
  672. int ctlr_exiting)
  673. {
  674. struct device *dev = h->drv[drv_index].dev;
  675. /* special case for c*d0, we only destroy it on controller exit */
  676. if (drv_index == 0 && !ctlr_exiting)
  677. return;
  678. device_del(dev);
  679. put_device(dev); /* the "final" put. */
  680. h->drv[drv_index].dev = NULL;
  681. }
  682. /*
  683. * For operations that cannot sleep, a command block is allocated at init,
  684. * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
  685. * which ones are free or in use. For operations that can wait for kmalloc
  686. * to possible sleep, this routine can be called with get_from_pool set to 0.
  687. * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
  688. */
  689. static CommandList_struct *cmd_alloc(ctlr_info_t *h, int get_from_pool)
  690. {
  691. CommandList_struct *c;
  692. int i;
  693. u64bit temp64;
  694. dma_addr_t cmd_dma_handle, err_dma_handle;
  695. if (!get_from_pool) {
  696. c = (CommandList_struct *) pci_alloc_consistent(h->pdev,
  697. sizeof(CommandList_struct), &cmd_dma_handle);
  698. if (c == NULL)
  699. return NULL;
  700. memset(c, 0, sizeof(CommandList_struct));
  701. c->cmdindex = -1;
  702. c->err_info = (ErrorInfo_struct *)
  703. pci_alloc_consistent(h->pdev, sizeof(ErrorInfo_struct),
  704. &err_dma_handle);
  705. if (c->err_info == NULL) {
  706. pci_free_consistent(h->pdev,
  707. sizeof(CommandList_struct), c, cmd_dma_handle);
  708. return NULL;
  709. }
  710. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  711. } else { /* get it out of the controllers pool */
  712. do {
  713. i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
  714. if (i == h->nr_cmds)
  715. return NULL;
  716. } while (test_and_set_bit
  717. (i & (BITS_PER_LONG - 1),
  718. h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
  719. #ifdef CCISS_DEBUG
  720. printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
  721. #endif
  722. c = h->cmd_pool + i;
  723. memset(c, 0, sizeof(CommandList_struct));
  724. cmd_dma_handle = h->cmd_pool_dhandle
  725. + i * sizeof(CommandList_struct);
  726. c->err_info = h->errinfo_pool + i;
  727. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  728. err_dma_handle = h->errinfo_pool_dhandle
  729. + i * sizeof(ErrorInfo_struct);
  730. h->nr_allocs++;
  731. c->cmdindex = i;
  732. }
  733. INIT_HLIST_NODE(&c->list);
  734. c->busaddr = (__u32) cmd_dma_handle;
  735. temp64.val = (__u64) err_dma_handle;
  736. c->ErrDesc.Addr.lower = temp64.val32.lower;
  737. c->ErrDesc.Addr.upper = temp64.val32.upper;
  738. c->ErrDesc.Len = sizeof(ErrorInfo_struct);
  739. c->ctlr = h->ctlr;
  740. return c;
  741. }
  742. /*
  743. * Frees a command block that was previously allocated with cmd_alloc().
  744. */
  745. static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
  746. {
  747. int i;
  748. u64bit temp64;
  749. if (!got_from_pool) {
  750. temp64.val32.lower = c->ErrDesc.Addr.lower;
  751. temp64.val32.upper = c->ErrDesc.Addr.upper;
  752. pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
  753. c->err_info, (dma_addr_t) temp64.val);
  754. pci_free_consistent(h->pdev, sizeof(CommandList_struct),
  755. c, (dma_addr_t) c->busaddr);
  756. } else {
  757. i = c - h->cmd_pool;
  758. clear_bit(i & (BITS_PER_LONG - 1),
  759. h->cmd_pool_bits + (i / BITS_PER_LONG));
  760. h->nr_frees++;
  761. }
  762. }
  763. static inline ctlr_info_t *get_host(struct gendisk *disk)
  764. {
  765. return disk->queue->queuedata;
  766. }
  767. static inline drive_info_struct *get_drv(struct gendisk *disk)
  768. {
  769. return disk->private_data;
  770. }
  771. /*
  772. * Open. Make sure the device is really there.
  773. */
  774. static int cciss_open(struct block_device *bdev, fmode_t mode)
  775. {
  776. ctlr_info_t *host = get_host(bdev->bd_disk);
  777. drive_info_struct *drv = get_drv(bdev->bd_disk);
  778. #ifdef CCISS_DEBUG
  779. printk(KERN_DEBUG "cciss_open %s\n", bdev->bd_disk->disk_name);
  780. #endif /* CCISS_DEBUG */
  781. if (drv->busy_configuring)
  782. return -EBUSY;
  783. /*
  784. * Root is allowed to open raw volume zero even if it's not configured
  785. * so array config can still work. Root is also allowed to open any
  786. * volume that has a LUN ID, so it can issue IOCTL to reread the
  787. * disk information. I don't think I really like this
  788. * but I'm already using way to many device nodes to claim another one
  789. * for "raw controller".
  790. */
  791. if (drv->heads == 0) {
  792. if (MINOR(bdev->bd_dev) != 0) { /* not node 0? */
  793. /* if not node 0 make sure it is a partition = 0 */
  794. if (MINOR(bdev->bd_dev) & 0x0f) {
  795. return -ENXIO;
  796. /* if it is, make sure we have a LUN ID */
  797. } else if (memcmp(drv->LunID, CTLR_LUNID,
  798. sizeof(drv->LunID))) {
  799. return -ENXIO;
  800. }
  801. }
  802. if (!capable(CAP_SYS_ADMIN))
  803. return -EPERM;
  804. }
  805. drv->usage_count++;
  806. host->usage_count++;
  807. return 0;
  808. }
  809. /*
  810. * Close. Sync first.
  811. */
  812. static int cciss_release(struct gendisk *disk, fmode_t mode)
  813. {
  814. ctlr_info_t *host = get_host(disk);
  815. drive_info_struct *drv = get_drv(disk);
  816. #ifdef CCISS_DEBUG
  817. printk(KERN_DEBUG "cciss_release %s\n", disk->disk_name);
  818. #endif /* CCISS_DEBUG */
  819. drv->usage_count--;
  820. host->usage_count--;
  821. return 0;
  822. }
  823. #ifdef CONFIG_COMPAT
  824. static int do_ioctl(struct block_device *bdev, fmode_t mode,
  825. unsigned cmd, unsigned long arg)
  826. {
  827. int ret;
  828. lock_kernel();
  829. ret = cciss_ioctl(bdev, mode, cmd, arg);
  830. unlock_kernel();
  831. return ret;
  832. }
  833. static int cciss_ioctl32_passthru(struct block_device *bdev, fmode_t mode,
  834. unsigned cmd, unsigned long arg);
  835. static int cciss_ioctl32_big_passthru(struct block_device *bdev, fmode_t mode,
  836. unsigned cmd, unsigned long arg);
  837. static int cciss_compat_ioctl(struct block_device *bdev, fmode_t mode,
  838. unsigned cmd, unsigned long arg)
  839. {
  840. switch (cmd) {
  841. case CCISS_GETPCIINFO:
  842. case CCISS_GETINTINFO:
  843. case CCISS_SETINTINFO:
  844. case CCISS_GETNODENAME:
  845. case CCISS_SETNODENAME:
  846. case CCISS_GETHEARTBEAT:
  847. case CCISS_GETBUSTYPES:
  848. case CCISS_GETFIRMVER:
  849. case CCISS_GETDRIVVER:
  850. case CCISS_REVALIDVOLS:
  851. case CCISS_DEREGDISK:
  852. case CCISS_REGNEWDISK:
  853. case CCISS_REGNEWD:
  854. case CCISS_RESCANDISK:
  855. case CCISS_GETLUNINFO:
  856. return do_ioctl(bdev, mode, cmd, arg);
  857. case CCISS_PASSTHRU32:
  858. return cciss_ioctl32_passthru(bdev, mode, cmd, arg);
  859. case CCISS_BIG_PASSTHRU32:
  860. return cciss_ioctl32_big_passthru(bdev, mode, cmd, arg);
  861. default:
  862. return -ENOIOCTLCMD;
  863. }
  864. }
  865. static int cciss_ioctl32_passthru(struct block_device *bdev, fmode_t mode,
  866. unsigned cmd, unsigned long arg)
  867. {
  868. IOCTL32_Command_struct __user *arg32 =
  869. (IOCTL32_Command_struct __user *) arg;
  870. IOCTL_Command_struct arg64;
  871. IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
  872. int err;
  873. u32 cp;
  874. err = 0;
  875. err |=
  876. copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  877. sizeof(arg64.LUN_info));
  878. err |=
  879. copy_from_user(&arg64.Request, &arg32->Request,
  880. sizeof(arg64.Request));
  881. err |=
  882. copy_from_user(&arg64.error_info, &arg32->error_info,
  883. sizeof(arg64.error_info));
  884. err |= get_user(arg64.buf_size, &arg32->buf_size);
  885. err |= get_user(cp, &arg32->buf);
  886. arg64.buf = compat_ptr(cp);
  887. err |= copy_to_user(p, &arg64, sizeof(arg64));
  888. if (err)
  889. return -EFAULT;
  890. err = do_ioctl(bdev, mode, CCISS_PASSTHRU, (unsigned long)p);
  891. if (err)
  892. return err;
  893. err |=
  894. copy_in_user(&arg32->error_info, &p->error_info,
  895. sizeof(arg32->error_info));
  896. if (err)
  897. return -EFAULT;
  898. return err;
  899. }
  900. static int cciss_ioctl32_big_passthru(struct block_device *bdev, fmode_t mode,
  901. unsigned cmd, unsigned long arg)
  902. {
  903. BIG_IOCTL32_Command_struct __user *arg32 =
  904. (BIG_IOCTL32_Command_struct __user *) arg;
  905. BIG_IOCTL_Command_struct arg64;
  906. BIG_IOCTL_Command_struct __user *p =
  907. compat_alloc_user_space(sizeof(arg64));
  908. int err;
  909. u32 cp;
  910. err = 0;
  911. err |=
  912. copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  913. sizeof(arg64.LUN_info));
  914. err |=
  915. copy_from_user(&arg64.Request, &arg32->Request,
  916. sizeof(arg64.Request));
  917. err |=
  918. copy_from_user(&arg64.error_info, &arg32->error_info,
  919. sizeof(arg64.error_info));
  920. err |= get_user(arg64.buf_size, &arg32->buf_size);
  921. err |= get_user(arg64.malloc_size, &arg32->malloc_size);
  922. err |= get_user(cp, &arg32->buf);
  923. arg64.buf = compat_ptr(cp);
  924. err |= copy_to_user(p, &arg64, sizeof(arg64));
  925. if (err)
  926. return -EFAULT;
  927. err = do_ioctl(bdev, mode, CCISS_BIG_PASSTHRU, (unsigned long)p);
  928. if (err)
  929. return err;
  930. err |=
  931. copy_in_user(&arg32->error_info, &p->error_info,
  932. sizeof(arg32->error_info));
  933. if (err)
  934. return -EFAULT;
  935. return err;
  936. }
  937. #endif
  938. static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  939. {
  940. drive_info_struct *drv = get_drv(bdev->bd_disk);
  941. if (!drv->cylinders)
  942. return -ENXIO;
  943. geo->heads = drv->heads;
  944. geo->sectors = drv->sectors;
  945. geo->cylinders = drv->cylinders;
  946. return 0;
  947. }
  948. static void check_ioctl_unit_attention(ctlr_info_t *host, CommandList_struct *c)
  949. {
  950. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  951. c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
  952. (void)check_for_unit_attention(host, c);
  953. }
  954. /*
  955. * ioctl
  956. */
  957. static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
  958. unsigned int cmd, unsigned long arg)
  959. {
  960. struct gendisk *disk = bdev->bd_disk;
  961. ctlr_info_t *host = get_host(disk);
  962. drive_info_struct *drv = get_drv(disk);
  963. int ctlr = host->ctlr;
  964. void __user *argp = (void __user *)arg;
  965. #ifdef CCISS_DEBUG
  966. printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
  967. #endif /* CCISS_DEBUG */
  968. switch (cmd) {
  969. case CCISS_GETPCIINFO:
  970. {
  971. cciss_pci_info_struct pciinfo;
  972. if (!arg)
  973. return -EINVAL;
  974. pciinfo.domain = pci_domain_nr(host->pdev->bus);
  975. pciinfo.bus = host->pdev->bus->number;
  976. pciinfo.dev_fn = host->pdev->devfn;
  977. pciinfo.board_id = host->board_id;
  978. if (copy_to_user
  979. (argp, &pciinfo, sizeof(cciss_pci_info_struct)))
  980. return -EFAULT;
  981. return 0;
  982. }
  983. case CCISS_GETINTINFO:
  984. {
  985. cciss_coalint_struct intinfo;
  986. if (!arg)
  987. return -EINVAL;
  988. intinfo.delay =
  989. readl(&host->cfgtable->HostWrite.CoalIntDelay);
  990. intinfo.count =
  991. readl(&host->cfgtable->HostWrite.CoalIntCount);
  992. if (copy_to_user
  993. (argp, &intinfo, sizeof(cciss_coalint_struct)))
  994. return -EFAULT;
  995. return 0;
  996. }
  997. case CCISS_SETINTINFO:
  998. {
  999. cciss_coalint_struct intinfo;
  1000. unsigned long flags;
  1001. int i;
  1002. if (!arg)
  1003. return -EINVAL;
  1004. if (!capable(CAP_SYS_ADMIN))
  1005. return -EPERM;
  1006. if (copy_from_user
  1007. (&intinfo, argp, sizeof(cciss_coalint_struct)))
  1008. return -EFAULT;
  1009. if ((intinfo.delay == 0) && (intinfo.count == 0))
  1010. {
  1011. // printk("cciss_ioctl: delay and count cannot be 0\n");
  1012. return -EINVAL;
  1013. }
  1014. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  1015. /* Update the field, and then ring the doorbell */
  1016. writel(intinfo.delay,
  1017. &(host->cfgtable->HostWrite.CoalIntDelay));
  1018. writel(intinfo.count,
  1019. &(host->cfgtable->HostWrite.CoalIntCount));
  1020. writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
  1021. for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
  1022. if (!(readl(host->vaddr + SA5_DOORBELL)
  1023. & CFGTBL_ChangeReq))
  1024. break;
  1025. /* delay and try again */
  1026. udelay(1000);
  1027. }
  1028. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  1029. if (i >= MAX_IOCTL_CONFIG_WAIT)
  1030. return -EAGAIN;
  1031. return 0;
  1032. }
  1033. case CCISS_GETNODENAME:
  1034. {
  1035. NodeName_type NodeName;
  1036. int i;
  1037. if (!arg)
  1038. return -EINVAL;
  1039. for (i = 0; i < 16; i++)
  1040. NodeName[i] =
  1041. readb(&host->cfgtable->ServerName[i]);
  1042. if (copy_to_user(argp, NodeName, sizeof(NodeName_type)))
  1043. return -EFAULT;
  1044. return 0;
  1045. }
  1046. case CCISS_SETNODENAME:
  1047. {
  1048. NodeName_type NodeName;
  1049. unsigned long flags;
  1050. int i;
  1051. if (!arg)
  1052. return -EINVAL;
  1053. if (!capable(CAP_SYS_ADMIN))
  1054. return -EPERM;
  1055. if (copy_from_user
  1056. (NodeName, argp, sizeof(NodeName_type)))
  1057. return -EFAULT;
  1058. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  1059. /* Update the field, and then ring the doorbell */
  1060. for (i = 0; i < 16; i++)
  1061. writeb(NodeName[i],
  1062. &host->cfgtable->ServerName[i]);
  1063. writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
  1064. for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
  1065. if (!(readl(host->vaddr + SA5_DOORBELL)
  1066. & CFGTBL_ChangeReq))
  1067. break;
  1068. /* delay and try again */
  1069. udelay(1000);
  1070. }
  1071. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  1072. if (i >= MAX_IOCTL_CONFIG_WAIT)
  1073. return -EAGAIN;
  1074. return 0;
  1075. }
  1076. case CCISS_GETHEARTBEAT:
  1077. {
  1078. Heartbeat_type heartbeat;
  1079. if (!arg)
  1080. return -EINVAL;
  1081. heartbeat = readl(&host->cfgtable->HeartBeat);
  1082. if (copy_to_user
  1083. (argp, &heartbeat, sizeof(Heartbeat_type)))
  1084. return -EFAULT;
  1085. return 0;
  1086. }
  1087. case CCISS_GETBUSTYPES:
  1088. {
  1089. BusTypes_type BusTypes;
  1090. if (!arg)
  1091. return -EINVAL;
  1092. BusTypes = readl(&host->cfgtable->BusTypes);
  1093. if (copy_to_user
  1094. (argp, &BusTypes, sizeof(BusTypes_type)))
  1095. return -EFAULT;
  1096. return 0;
  1097. }
  1098. case CCISS_GETFIRMVER:
  1099. {
  1100. FirmwareVer_type firmware;
  1101. if (!arg)
  1102. return -EINVAL;
  1103. memcpy(firmware, host->firm_ver, 4);
  1104. if (copy_to_user
  1105. (argp, firmware, sizeof(FirmwareVer_type)))
  1106. return -EFAULT;
  1107. return 0;
  1108. }
  1109. case CCISS_GETDRIVVER:
  1110. {
  1111. DriverVer_type DriverVer = DRIVER_VERSION;
  1112. if (!arg)
  1113. return -EINVAL;
  1114. if (copy_to_user
  1115. (argp, &DriverVer, sizeof(DriverVer_type)))
  1116. return -EFAULT;
  1117. return 0;
  1118. }
  1119. case CCISS_DEREGDISK:
  1120. case CCISS_REGNEWD:
  1121. case CCISS_REVALIDVOLS:
  1122. return rebuild_lun_table(host, 0, 1);
  1123. case CCISS_GETLUNINFO:{
  1124. LogvolInfo_struct luninfo;
  1125. memcpy(&luninfo.LunID, drv->LunID,
  1126. sizeof(luninfo.LunID));
  1127. luninfo.num_opens = drv->usage_count;
  1128. luninfo.num_parts = 0;
  1129. if (copy_to_user(argp, &luninfo,
  1130. sizeof(LogvolInfo_struct)))
  1131. return -EFAULT;
  1132. return 0;
  1133. }
  1134. case CCISS_PASSTHRU:
  1135. {
  1136. IOCTL_Command_struct iocommand;
  1137. CommandList_struct *c;
  1138. char *buff = NULL;
  1139. u64bit temp64;
  1140. unsigned long flags;
  1141. DECLARE_COMPLETION_ONSTACK(wait);
  1142. if (!arg)
  1143. return -EINVAL;
  1144. if (!capable(CAP_SYS_RAWIO))
  1145. return -EPERM;
  1146. if (copy_from_user
  1147. (&iocommand, argp, sizeof(IOCTL_Command_struct)))
  1148. return -EFAULT;
  1149. if ((iocommand.buf_size < 1) &&
  1150. (iocommand.Request.Type.Direction != XFER_NONE)) {
  1151. return -EINVAL;
  1152. }
  1153. #if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
  1154. /* Check kmalloc limits */
  1155. if (iocommand.buf_size > 128000)
  1156. return -EINVAL;
  1157. #endif
  1158. if (iocommand.buf_size > 0) {
  1159. buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
  1160. if (buff == NULL)
  1161. return -EFAULT;
  1162. }
  1163. if (iocommand.Request.Type.Direction == XFER_WRITE) {
  1164. /* Copy the data into the buffer we created */
  1165. if (copy_from_user
  1166. (buff, iocommand.buf, iocommand.buf_size)) {
  1167. kfree(buff);
  1168. return -EFAULT;
  1169. }
  1170. } else {
  1171. memset(buff, 0, iocommand.buf_size);
  1172. }
  1173. if ((c = cmd_alloc(host, 0)) == NULL) {
  1174. kfree(buff);
  1175. return -ENOMEM;
  1176. }
  1177. // Fill in the command type
  1178. c->cmd_type = CMD_IOCTL_PEND;
  1179. // Fill in Command Header
  1180. c->Header.ReplyQueue = 0; // unused in simple mode
  1181. if (iocommand.buf_size > 0) // buffer to fill
  1182. {
  1183. c->Header.SGList = 1;
  1184. c->Header.SGTotal = 1;
  1185. } else // no buffers to fill
  1186. {
  1187. c->Header.SGList = 0;
  1188. c->Header.SGTotal = 0;
  1189. }
  1190. c->Header.LUN = iocommand.LUN_info;
  1191. c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
  1192. // Fill in Request block
  1193. c->Request = iocommand.Request;
  1194. // Fill in the scatter gather information
  1195. if (iocommand.buf_size > 0) {
  1196. temp64.val = pci_map_single(host->pdev, buff,
  1197. iocommand.buf_size,
  1198. PCI_DMA_BIDIRECTIONAL);
  1199. c->SG[0].Addr.lower = temp64.val32.lower;
  1200. c->SG[0].Addr.upper = temp64.val32.upper;
  1201. c->SG[0].Len = iocommand.buf_size;
  1202. c->SG[0].Ext = 0; // we are not chaining
  1203. }
  1204. c->waiting = &wait;
  1205. /* Put the request on the tail of the request queue */
  1206. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  1207. addQ(&host->reqQ, c);
  1208. host->Qdepth++;
  1209. start_io(host);
  1210. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  1211. wait_for_completion(&wait);
  1212. /* unlock the buffers from DMA */
  1213. temp64.val32.lower = c->SG[0].Addr.lower;
  1214. temp64.val32.upper = c->SG[0].Addr.upper;
  1215. pci_unmap_single(host->pdev, (dma_addr_t) temp64.val,
  1216. iocommand.buf_size,
  1217. PCI_DMA_BIDIRECTIONAL);
  1218. check_ioctl_unit_attention(host, c);
  1219. /* Copy the error information out */
  1220. iocommand.error_info = *(c->err_info);
  1221. if (copy_to_user
  1222. (argp, &iocommand, sizeof(IOCTL_Command_struct))) {
  1223. kfree(buff);
  1224. cmd_free(host, c, 0);
  1225. return -EFAULT;
  1226. }
  1227. if (iocommand.Request.Type.Direction == XFER_READ) {
  1228. /* Copy the data out of the buffer we created */
  1229. if (copy_to_user
  1230. (iocommand.buf, buff, iocommand.buf_size)) {
  1231. kfree(buff);
  1232. cmd_free(host, c, 0);
  1233. return -EFAULT;
  1234. }
  1235. }
  1236. kfree(buff);
  1237. cmd_free(host, c, 0);
  1238. return 0;
  1239. }
  1240. case CCISS_BIG_PASSTHRU:{
  1241. BIG_IOCTL_Command_struct *ioc;
  1242. CommandList_struct *c;
  1243. unsigned char **buff = NULL;
  1244. int *buff_size = NULL;
  1245. u64bit temp64;
  1246. unsigned long flags;
  1247. BYTE sg_used = 0;
  1248. int status = 0;
  1249. int i;
  1250. DECLARE_COMPLETION_ONSTACK(wait);
  1251. __u32 left;
  1252. __u32 sz;
  1253. BYTE __user *data_ptr;
  1254. if (!arg)
  1255. return -EINVAL;
  1256. if (!capable(CAP_SYS_RAWIO))
  1257. return -EPERM;
  1258. ioc = (BIG_IOCTL_Command_struct *)
  1259. kmalloc(sizeof(*ioc), GFP_KERNEL);
  1260. if (!ioc) {
  1261. status = -ENOMEM;
  1262. goto cleanup1;
  1263. }
  1264. if (copy_from_user(ioc, argp, sizeof(*ioc))) {
  1265. status = -EFAULT;
  1266. goto cleanup1;
  1267. }
  1268. if ((ioc->buf_size < 1) &&
  1269. (ioc->Request.Type.Direction != XFER_NONE)) {
  1270. status = -EINVAL;
  1271. goto cleanup1;
  1272. }
  1273. /* Check kmalloc limits using all SGs */
  1274. if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
  1275. status = -EINVAL;
  1276. goto cleanup1;
  1277. }
  1278. if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
  1279. status = -EINVAL;
  1280. goto cleanup1;
  1281. }
  1282. buff =
  1283. kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
  1284. if (!buff) {
  1285. status = -ENOMEM;
  1286. goto cleanup1;
  1287. }
  1288. buff_size = kmalloc(MAXSGENTRIES * sizeof(int),
  1289. GFP_KERNEL);
  1290. if (!buff_size) {
  1291. status = -ENOMEM;
  1292. goto cleanup1;
  1293. }
  1294. left = ioc->buf_size;
  1295. data_ptr = ioc->buf;
  1296. while (left) {
  1297. sz = (left >
  1298. ioc->malloc_size) ? ioc->
  1299. malloc_size : left;
  1300. buff_size[sg_used] = sz;
  1301. buff[sg_used] = kmalloc(sz, GFP_KERNEL);
  1302. if (buff[sg_used] == NULL) {
  1303. status = -ENOMEM;
  1304. goto cleanup1;
  1305. }
  1306. if (ioc->Request.Type.Direction == XFER_WRITE) {
  1307. if (copy_from_user
  1308. (buff[sg_used], data_ptr, sz)) {
  1309. status = -EFAULT;
  1310. goto cleanup1;
  1311. }
  1312. } else {
  1313. memset(buff[sg_used], 0, sz);
  1314. }
  1315. left -= sz;
  1316. data_ptr += sz;
  1317. sg_used++;
  1318. }
  1319. if ((c = cmd_alloc(host, 0)) == NULL) {
  1320. status = -ENOMEM;
  1321. goto cleanup1;
  1322. }
  1323. c->cmd_type = CMD_IOCTL_PEND;
  1324. c->Header.ReplyQueue = 0;
  1325. if (ioc->buf_size > 0) {
  1326. c->Header.SGList = sg_used;
  1327. c->Header.SGTotal = sg_used;
  1328. } else {
  1329. c->Header.SGList = 0;
  1330. c->Header.SGTotal = 0;
  1331. }
  1332. c->Header.LUN = ioc->LUN_info;
  1333. c->Header.Tag.lower = c->busaddr;
  1334. c->Request = ioc->Request;
  1335. if (ioc->buf_size > 0) {
  1336. int i;
  1337. for (i = 0; i < sg_used; i++) {
  1338. temp64.val =
  1339. pci_map_single(host->pdev, buff[i],
  1340. buff_size[i],
  1341. PCI_DMA_BIDIRECTIONAL);
  1342. c->SG[i].Addr.lower =
  1343. temp64.val32.lower;
  1344. c->SG[i].Addr.upper =
  1345. temp64.val32.upper;
  1346. c->SG[i].Len = buff_size[i];
  1347. c->SG[i].Ext = 0; /* we are not chaining */
  1348. }
  1349. }
  1350. c->waiting = &wait;
  1351. /* Put the request on the tail of the request queue */
  1352. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  1353. addQ(&host->reqQ, c);
  1354. host->Qdepth++;
  1355. start_io(host);
  1356. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  1357. wait_for_completion(&wait);
  1358. /* unlock the buffers from DMA */
  1359. for (i = 0; i < sg_used; i++) {
  1360. temp64.val32.lower = c->SG[i].Addr.lower;
  1361. temp64.val32.upper = c->SG[i].Addr.upper;
  1362. pci_unmap_single(host->pdev,
  1363. (dma_addr_t) temp64.val, buff_size[i],
  1364. PCI_DMA_BIDIRECTIONAL);
  1365. }
  1366. check_ioctl_unit_attention(host, c);
  1367. /* Copy the error information out */
  1368. ioc->error_info = *(c->err_info);
  1369. if (copy_to_user(argp, ioc, sizeof(*ioc))) {
  1370. cmd_free(host, c, 0);
  1371. status = -EFAULT;
  1372. goto cleanup1;
  1373. }
  1374. if (ioc->Request.Type.Direction == XFER_READ) {
  1375. /* Copy the data out of the buffer we created */
  1376. BYTE __user *ptr = ioc->buf;
  1377. for (i = 0; i < sg_used; i++) {
  1378. if (copy_to_user
  1379. (ptr, buff[i], buff_size[i])) {
  1380. cmd_free(host, c, 0);
  1381. status = -EFAULT;
  1382. goto cleanup1;
  1383. }
  1384. ptr += buff_size[i];
  1385. }
  1386. }
  1387. cmd_free(host, c, 0);
  1388. status = 0;
  1389. cleanup1:
  1390. if (buff) {
  1391. for (i = 0; i < sg_used; i++)
  1392. kfree(buff[i]);
  1393. kfree(buff);
  1394. }
  1395. kfree(buff_size);
  1396. kfree(ioc);
  1397. return status;
  1398. }
  1399. /* scsi_cmd_ioctl handles these, below, though some are not */
  1400. /* very meaningful for cciss. SG_IO is the main one people want. */
  1401. case SG_GET_VERSION_NUM:
  1402. case SG_SET_TIMEOUT:
  1403. case SG_GET_TIMEOUT:
  1404. case SG_GET_RESERVED_SIZE:
  1405. case SG_SET_RESERVED_SIZE:
  1406. case SG_EMULATED_HOST:
  1407. case SG_IO:
  1408. case SCSI_IOCTL_SEND_COMMAND:
  1409. return scsi_cmd_ioctl(disk->queue, disk, mode, cmd, argp);
  1410. /* scsi_cmd_ioctl would normally handle these, below, but */
  1411. /* they aren't a good fit for cciss, as CD-ROMs are */
  1412. /* not supported, and we don't have any bus/target/lun */
  1413. /* which we present to the kernel. */
  1414. case CDROM_SEND_PACKET:
  1415. case CDROMCLOSETRAY:
  1416. case CDROMEJECT:
  1417. case SCSI_IOCTL_GET_IDLUN:
  1418. case SCSI_IOCTL_GET_BUS_NUMBER:
  1419. default:
  1420. return -ENOTTY;
  1421. }
  1422. }
  1423. static void cciss_check_queues(ctlr_info_t *h)
  1424. {
  1425. int start_queue = h->next_to_run;
  1426. int i;
  1427. /* check to see if we have maxed out the number of commands that can
  1428. * be placed on the queue. If so then exit. We do this check here
  1429. * in case the interrupt we serviced was from an ioctl and did not
  1430. * free any new commands.
  1431. */
  1432. if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds)
  1433. return;
  1434. /* We have room on the queue for more commands. Now we need to queue
  1435. * them up. We will also keep track of the next queue to run so
  1436. * that every queue gets a chance to be started first.
  1437. */
  1438. for (i = 0; i < h->highest_lun + 1; i++) {
  1439. int curr_queue = (start_queue + i) % (h->highest_lun + 1);
  1440. /* make sure the disk has been added and the drive is real
  1441. * because this can be called from the middle of init_one.
  1442. */
  1443. if (!(h->drv[curr_queue].queue) || !(h->drv[curr_queue].heads))
  1444. continue;
  1445. blk_start_queue(h->gendisk[curr_queue]->queue);
  1446. /* check to see if we have maxed out the number of commands
  1447. * that can be placed on the queue.
  1448. */
  1449. if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds) {
  1450. if (curr_queue == start_queue) {
  1451. h->next_to_run =
  1452. (start_queue + 1) % (h->highest_lun + 1);
  1453. break;
  1454. } else {
  1455. h->next_to_run = curr_queue;
  1456. break;
  1457. }
  1458. }
  1459. }
  1460. }
  1461. static void cciss_softirq_done(struct request *rq)
  1462. {
  1463. CommandList_struct *cmd = rq->completion_data;
  1464. ctlr_info_t *h = hba[cmd->ctlr];
  1465. unsigned long flags;
  1466. u64bit temp64;
  1467. int i, ddir;
  1468. if (cmd->Request.Type.Direction == XFER_READ)
  1469. ddir = PCI_DMA_FROMDEVICE;
  1470. else
  1471. ddir = PCI_DMA_TODEVICE;
  1472. /* command did not need to be retried */
  1473. /* unmap the DMA mapping for all the scatter gather elements */
  1474. for (i = 0; i < cmd->Header.SGList; i++) {
  1475. temp64.val32.lower = cmd->SG[i].Addr.lower;
  1476. temp64.val32.upper = cmd->SG[i].Addr.upper;
  1477. pci_unmap_page(h->pdev, temp64.val, cmd->SG[i].Len, ddir);
  1478. }
  1479. #ifdef CCISS_DEBUG
  1480. printk("Done with %p\n", rq);
  1481. #endif /* CCISS_DEBUG */
  1482. /* set the residual count for pc requests */
  1483. if (blk_pc_request(rq))
  1484. rq->resid_len = cmd->err_info->ResidualCnt;
  1485. blk_end_request_all(rq, (rq->errors == 0) ? 0 : -EIO);
  1486. spin_lock_irqsave(&h->lock, flags);
  1487. cmd_free(h, cmd, 1);
  1488. cciss_check_queues(h);
  1489. spin_unlock_irqrestore(&h->lock, flags);
  1490. }
  1491. static inline void log_unit_to_scsi3addr(ctlr_info_t *h,
  1492. unsigned char scsi3addr[], uint32_t log_unit)
  1493. {
  1494. memcpy(scsi3addr, h->drv[log_unit].LunID,
  1495. sizeof(h->drv[log_unit].LunID));
  1496. }
  1497. /* This function gets the SCSI vendor, model, and revision of a logical drive
  1498. * via the inquiry page 0. Model, vendor, and rev are set to empty strings if
  1499. * they cannot be read.
  1500. */
  1501. static void cciss_get_device_descr(int ctlr, int logvol, int withirq,
  1502. char *vendor, char *model, char *rev)
  1503. {
  1504. int rc;
  1505. InquiryData_struct *inq_buf;
  1506. unsigned char scsi3addr[8];
  1507. *vendor = '\0';
  1508. *model = '\0';
  1509. *rev = '\0';
  1510. inq_buf = kzalloc(sizeof(InquiryData_struct), GFP_KERNEL);
  1511. if (!inq_buf)
  1512. return;
  1513. log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol);
  1514. if (withirq)
  1515. rc = sendcmd_withirq(CISS_INQUIRY, ctlr, inq_buf,
  1516. sizeof(InquiryData_struct), 0,
  1517. scsi3addr, TYPE_CMD);
  1518. else
  1519. rc = sendcmd(CISS_INQUIRY, ctlr, inq_buf,
  1520. sizeof(InquiryData_struct), 0,
  1521. scsi3addr, TYPE_CMD);
  1522. if (rc == IO_OK) {
  1523. memcpy(vendor, &inq_buf->data_byte[8], VENDOR_LEN);
  1524. vendor[VENDOR_LEN] = '\0';
  1525. memcpy(model, &inq_buf->data_byte[16], MODEL_LEN);
  1526. model[MODEL_LEN] = '\0';
  1527. memcpy(rev, &inq_buf->data_byte[32], REV_LEN);
  1528. rev[REV_LEN] = '\0';
  1529. }
  1530. kfree(inq_buf);
  1531. return;
  1532. }
  1533. /* This function gets the serial number of a logical drive via
  1534. * inquiry page 0x83. Serial no. is 16 bytes. If the serial
  1535. * number cannot be had, for whatever reason, 16 bytes of 0xff
  1536. * are returned instead.
  1537. */
  1538. static void cciss_get_serial_no(int ctlr, int logvol, int withirq,
  1539. unsigned char *serial_no, int buflen)
  1540. {
  1541. #define PAGE_83_INQ_BYTES 64
  1542. int rc;
  1543. unsigned char *buf;
  1544. unsigned char scsi3addr[8];
  1545. if (buflen > 16)
  1546. buflen = 16;
  1547. memset(serial_no, 0xff, buflen);
  1548. buf = kzalloc(PAGE_83_INQ_BYTES, GFP_KERNEL);
  1549. if (!buf)
  1550. return;
  1551. memset(serial_no, 0, buflen);
  1552. log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol);
  1553. if (withirq)
  1554. rc = sendcmd_withirq(CISS_INQUIRY, ctlr, buf,
  1555. PAGE_83_INQ_BYTES, 0x83, scsi3addr, TYPE_CMD);
  1556. else
  1557. rc = sendcmd(CISS_INQUIRY, ctlr, buf,
  1558. PAGE_83_INQ_BYTES, 0x83, scsi3addr, TYPE_CMD);
  1559. if (rc == IO_OK)
  1560. memcpy(serial_no, &buf[8], buflen);
  1561. kfree(buf);
  1562. return;
  1563. }
  1564. /*
  1565. * cciss_add_disk sets up the block device queue for a logical drive
  1566. */
  1567. static int cciss_add_disk(ctlr_info_t *h, struct gendisk *disk,
  1568. int drv_index)
  1569. {
  1570. disk->queue = blk_init_queue(do_cciss_request, &h->lock);
  1571. if (!disk->queue)
  1572. goto init_queue_failure;
  1573. sprintf(disk->disk_name, "cciss/c%dd%d", h->ctlr, drv_index);
  1574. disk->major = h->major;
  1575. disk->first_minor = drv_index << NWD_SHIFT;
  1576. disk->fops = &cciss_fops;
  1577. if (h->drv[drv_index].dev == NULL) {
  1578. if (cciss_create_ld_sysfs_entry(h, drv_index))
  1579. goto cleanup_queue;
  1580. }
  1581. disk->private_data = &h->drv[drv_index];
  1582. disk->driverfs_dev = h->drv[drv_index].dev;
  1583. /* Set up queue information */
  1584. blk_queue_bounce_limit(disk->queue, h->pdev->dma_mask);
  1585. /* This is a hardware imposed limit. */
  1586. blk_queue_max_hw_segments(disk->queue, MAXSGENTRIES);
  1587. /* This is a limit in the driver and could be eliminated. */
  1588. blk_queue_max_phys_segments(disk->queue, MAXSGENTRIES);
  1589. blk_queue_max_sectors(disk->queue, h->cciss_max_sectors);
  1590. blk_queue_softirq_done(disk->queue, cciss_softirq_done);
  1591. disk->queue->queuedata = h;
  1592. blk_queue_logical_block_size(disk->queue,
  1593. h->drv[drv_index].block_size);
  1594. /* Make sure all queue data is written out before */
  1595. /* setting h->drv[drv_index].queue, as setting this */
  1596. /* allows the interrupt handler to start the queue */
  1597. wmb();
  1598. h->drv[drv_index].queue = disk->queue;
  1599. add_disk(disk);
  1600. return 0;
  1601. cleanup_queue:
  1602. blk_cleanup_queue(disk->queue);
  1603. disk->queue = NULL;
  1604. init_queue_failure:
  1605. return -1;
  1606. }
  1607. /* This function will check the usage_count of the drive to be updated/added.
  1608. * If the usage_count is zero and it is a heretofore unknown drive, or,
  1609. * the drive's capacity, geometry, or serial number has changed,
  1610. * then the drive information will be updated and the disk will be
  1611. * re-registered with the kernel. If these conditions don't hold,
  1612. * then it will be left alone for the next reboot. The exception to this
  1613. * is disk 0 which will always be left registered with the kernel since it
  1614. * is also the controller node. Any changes to disk 0 will show up on
  1615. * the next reboot.
  1616. */
  1617. static void cciss_update_drive_info(int ctlr, int drv_index, int first_time,
  1618. int via_ioctl)
  1619. {
  1620. ctlr_info_t *h = hba[ctlr];
  1621. struct gendisk *disk;
  1622. InquiryData_struct *inq_buff = NULL;
  1623. unsigned int block_size;
  1624. sector_t total_size;
  1625. unsigned long flags = 0;
  1626. int ret = 0;
  1627. drive_info_struct *drvinfo;
  1628. /* Get information about the disk and modify the driver structure */
  1629. inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
  1630. drvinfo = kmalloc(sizeof(*drvinfo), GFP_KERNEL);
  1631. if (inq_buff == NULL || drvinfo == NULL)
  1632. goto mem_msg;
  1633. /* testing to see if 16-byte CDBs are already being used */
  1634. if (h->cciss_read == CCISS_READ_16) {
  1635. cciss_read_capacity_16(h->ctlr, drv_index, 1,
  1636. &total_size, &block_size);
  1637. } else {
  1638. cciss_read_capacity(ctlr, drv_index, 1,
  1639. &total_size, &block_size);
  1640. /* if read_capacity returns all F's this volume is >2TB */
  1641. /* in size so we switch to 16-byte CDB's for all */
  1642. /* read/write ops */
  1643. if (total_size == 0xFFFFFFFFULL) {
  1644. cciss_read_capacity_16(ctlr, drv_index, 1,
  1645. &total_size, &block_size);
  1646. h->cciss_read = CCISS_READ_16;
  1647. h->cciss_write = CCISS_WRITE_16;
  1648. } else {
  1649. h->cciss_read = CCISS_READ_10;
  1650. h->cciss_write = CCISS_WRITE_10;
  1651. }
  1652. }
  1653. cciss_geometry_inquiry(ctlr, drv_index, 1, total_size, block_size,
  1654. inq_buff, drvinfo);
  1655. drvinfo->block_size = block_size;
  1656. drvinfo->nr_blocks = total_size + 1;
  1657. cciss_get_device_descr(ctlr, drv_index, 1, drvinfo->vendor,
  1658. drvinfo->model, drvinfo->rev);
  1659. cciss_get_serial_no(ctlr, drv_index, 1, drvinfo->serial_no,
  1660. sizeof(drvinfo->serial_no));
  1661. /* Is it the same disk we already know, and nothing's changed? */
  1662. if (h->drv[drv_index].raid_level != -1 &&
  1663. ((memcmp(drvinfo->serial_no,
  1664. h->drv[drv_index].serial_no, 16) == 0) &&
  1665. drvinfo->block_size == h->drv[drv_index].block_size &&
  1666. drvinfo->nr_blocks == h->drv[drv_index].nr_blocks &&
  1667. drvinfo->heads == h->drv[drv_index].heads &&
  1668. drvinfo->sectors == h->drv[drv_index].sectors &&
  1669. drvinfo->cylinders == h->drv[drv_index].cylinders))
  1670. /* The disk is unchanged, nothing to update */
  1671. goto freeret;
  1672. /* If we get here it's not the same disk, or something's changed,
  1673. * so we need to * deregister it, and re-register it, if it's not
  1674. * in use.
  1675. * If the disk already exists then deregister it before proceeding
  1676. * (unless it's the first disk (for the controller node).
  1677. */
  1678. if (h->drv[drv_index].raid_level != -1 && drv_index != 0) {
  1679. printk(KERN_WARNING "disk %d has changed.\n", drv_index);
  1680. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  1681. h->drv[drv_index].busy_configuring = 1;
  1682. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1683. /* deregister_disk sets h->drv[drv_index].queue = NULL
  1684. * which keeps the interrupt handler from starting
  1685. * the queue.
  1686. */
  1687. ret = deregister_disk(h, drv_index, 0, via_ioctl);
  1688. h->drv[drv_index].busy_configuring = 0;
  1689. }
  1690. /* If the disk is in use return */
  1691. if (ret)
  1692. goto freeret;
  1693. /* Save the new information from cciss_geometry_inquiry
  1694. * and serial number inquiry.
  1695. */
  1696. h->drv[drv_index].block_size = drvinfo->block_size;
  1697. h->drv[drv_index].nr_blocks = drvinfo->nr_blocks;
  1698. h->drv[drv_index].heads = drvinfo->heads;
  1699. h->drv[drv_index].sectors = drvinfo->sectors;
  1700. h->drv[drv_index].cylinders = drvinfo->cylinders;
  1701. h->drv[drv_index].raid_level = drvinfo->raid_level;
  1702. memcpy(h->drv[drv_index].serial_no, drvinfo->serial_no, 16);
  1703. memcpy(h->drv[drv_index].vendor, drvinfo->vendor, VENDOR_LEN + 1);
  1704. memcpy(h->drv[drv_index].model, drvinfo->model, MODEL_LEN + 1);
  1705. memcpy(h->drv[drv_index].rev, drvinfo->rev, REV_LEN + 1);
  1706. ++h->num_luns;
  1707. disk = h->gendisk[drv_index];
  1708. set_capacity(disk, h->drv[drv_index].nr_blocks);
  1709. /* If it's not disk 0 (drv_index != 0)
  1710. * or if it was disk 0, but there was previously
  1711. * no actual corresponding configured logical drive
  1712. * (raid_leve == -1) then we want to update the
  1713. * logical drive's information.
  1714. */
  1715. if (drv_index || first_time) {
  1716. if (cciss_add_disk(h, disk, drv_index) != 0) {
  1717. cciss_free_gendisk(h, drv_index);
  1718. printk(KERN_WARNING "cciss:%d could not update "
  1719. "disk %d\n", h->ctlr, drv_index);
  1720. --h->num_luns;
  1721. }
  1722. }
  1723. freeret:
  1724. kfree(inq_buff);
  1725. kfree(drvinfo);
  1726. return;
  1727. mem_msg:
  1728. printk(KERN_ERR "cciss: out of memory\n");
  1729. goto freeret;
  1730. }
  1731. /* This function will find the first index of the controllers drive array
  1732. * that has a -1 for the raid_level and will return that index. This is
  1733. * where new drives will be added. If the index to be returned is greater
  1734. * than the highest_lun index for the controller then highest_lun is set
  1735. * to this new index. If there are no available indexes then -1 is returned.
  1736. * "controller_node" is used to know if this is a real logical drive, or just
  1737. * the controller node, which determines if this counts towards highest_lun.
  1738. */
  1739. static int cciss_find_free_drive_index(int ctlr, int controller_node)
  1740. {
  1741. int i;
  1742. for (i = 0; i < CISS_MAX_LUN; i++) {
  1743. if (hba[ctlr]->drv[i].raid_level == -1) {
  1744. if (i > hba[ctlr]->highest_lun)
  1745. if (!controller_node)
  1746. hba[ctlr]->highest_lun = i;
  1747. return i;
  1748. }
  1749. }
  1750. return -1;
  1751. }
  1752. static void cciss_free_gendisk(ctlr_info_t *h, int drv_index)
  1753. {
  1754. put_disk(h->gendisk[drv_index]);
  1755. h->gendisk[drv_index] = NULL;
  1756. }
  1757. /* cciss_add_gendisk finds a free hba[]->drv structure
  1758. * and allocates a gendisk if needed, and sets the lunid
  1759. * in the drvinfo structure. It returns the index into
  1760. * the ->drv[] array, or -1 if none are free.
  1761. * is_controller_node indicates whether highest_lun should
  1762. * count this disk, or if it's only being added to provide
  1763. * a means to talk to the controller in case no logical
  1764. * drives have yet been configured.
  1765. */
  1766. static int cciss_add_gendisk(ctlr_info_t *h, unsigned char lunid[],
  1767. int controller_node)
  1768. {
  1769. int drv_index;
  1770. drv_index = cciss_find_free_drive_index(h->ctlr, controller_node);
  1771. if (drv_index == -1)
  1772. return -1;
  1773. /*Check if the gendisk needs to be allocated */
  1774. if (!h->gendisk[drv_index]) {
  1775. h->gendisk[drv_index] =
  1776. alloc_disk(1 << NWD_SHIFT);
  1777. if (!h->gendisk[drv_index]) {
  1778. printk(KERN_ERR "cciss%d: could not "
  1779. "allocate a new disk %d\n",
  1780. h->ctlr, drv_index);
  1781. return -1;
  1782. }
  1783. }
  1784. memcpy(h->drv[drv_index].LunID, lunid,
  1785. sizeof(h->drv[drv_index].LunID));
  1786. if (h->drv[drv_index].dev == NULL) {
  1787. if (cciss_create_ld_sysfs_entry(h, drv_index))
  1788. goto err_free_disk;
  1789. }
  1790. /* Don't need to mark this busy because nobody */
  1791. /* else knows about this disk yet to contend */
  1792. /* for access to it. */
  1793. h->drv[drv_index].busy_configuring = 0;
  1794. wmb();
  1795. return drv_index;
  1796. err_free_disk:
  1797. cciss_free_gendisk(h, drv_index);
  1798. return -1;
  1799. }
  1800. /* This is for the special case of a controller which
  1801. * has no logical drives. In this case, we still need
  1802. * to register a disk so the controller can be accessed
  1803. * by the Array Config Utility.
  1804. */
  1805. static void cciss_add_controller_node(ctlr_info_t *h)
  1806. {
  1807. struct gendisk *disk;
  1808. int drv_index;
  1809. if (h->gendisk[0] != NULL) /* already did this? Then bail. */
  1810. return;
  1811. drv_index = cciss_add_gendisk(h, CTLR_LUNID, 1);
  1812. if (drv_index == -1)
  1813. goto error;
  1814. h->drv[drv_index].block_size = 512;
  1815. h->drv[drv_index].nr_blocks = 0;
  1816. h->drv[drv_index].heads = 0;
  1817. h->drv[drv_index].sectors = 0;
  1818. h->drv[drv_index].cylinders = 0;
  1819. h->drv[drv_index].raid_level = -1;
  1820. memset(h->drv[drv_index].serial_no, 0, 16);
  1821. disk = h->gendisk[drv_index];
  1822. if (cciss_add_disk(h, disk, drv_index) == 0)
  1823. return;
  1824. cciss_free_gendisk(h, drv_index);
  1825. error:
  1826. printk(KERN_WARNING "cciss%d: could not "
  1827. "add disk 0.\n", h->ctlr);
  1828. return;
  1829. }
  1830. /* This function will add and remove logical drives from the Logical
  1831. * drive array of the controller and maintain persistency of ordering
  1832. * so that mount points are preserved until the next reboot. This allows
  1833. * for the removal of logical drives in the middle of the drive array
  1834. * without a re-ordering of those drives.
  1835. * INPUT
  1836. * h = The controller to perform the operations on
  1837. */
  1838. static int rebuild_lun_table(ctlr_info_t *h, int first_time,
  1839. int via_ioctl)
  1840. {
  1841. int ctlr = h->ctlr;
  1842. int num_luns;
  1843. ReportLunData_struct *ld_buff = NULL;
  1844. int return_code;
  1845. int listlength = 0;
  1846. int i;
  1847. int drv_found;
  1848. int drv_index = 0;
  1849. unsigned char lunid[8] = CTLR_LUNID;
  1850. unsigned long flags;
  1851. if (!capable(CAP_SYS_RAWIO))
  1852. return -EPERM;
  1853. /* Set busy_configuring flag for this operation */
  1854. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  1855. if (h->busy_configuring) {
  1856. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1857. return -EBUSY;
  1858. }
  1859. h->busy_configuring = 1;
  1860. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1861. ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
  1862. if (ld_buff == NULL)
  1863. goto mem_msg;
  1864. return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
  1865. sizeof(ReportLunData_struct),
  1866. 0, CTLR_LUNID, TYPE_CMD);
  1867. if (return_code == IO_OK)
  1868. listlength = be32_to_cpu(*(__be32 *) ld_buff->LUNListLength);
  1869. else { /* reading number of logical volumes failed */
  1870. printk(KERN_WARNING "cciss: report logical volume"
  1871. " command failed\n");
  1872. listlength = 0;
  1873. goto freeret;
  1874. }
  1875. num_luns = listlength / 8; /* 8 bytes per entry */
  1876. if (num_luns > CISS_MAX_LUN) {
  1877. num_luns = CISS_MAX_LUN;
  1878. printk(KERN_WARNING "cciss: more luns configured"
  1879. " on controller than can be handled by"
  1880. " this driver.\n");
  1881. }
  1882. if (num_luns == 0)
  1883. cciss_add_controller_node(h);
  1884. /* Compare controller drive array to driver's drive array
  1885. * to see if any drives are missing on the controller due
  1886. * to action of Array Config Utility (user deletes drive)
  1887. * and deregister logical drives which have disappeared.
  1888. */
  1889. for (i = 0; i <= h->highest_lun; i++) {
  1890. int j;
  1891. drv_found = 0;
  1892. /* skip holes in the array from already deleted drives */
  1893. if (h->drv[i].raid_level == -1)
  1894. continue;
  1895. for (j = 0; j < num_luns; j++) {
  1896. memcpy(lunid, &ld_buff->LUN[j][0], sizeof(lunid));
  1897. if (memcmp(h->drv[i].LunID, lunid,
  1898. sizeof(lunid)) == 0) {
  1899. drv_found = 1;
  1900. break;
  1901. }
  1902. }
  1903. if (!drv_found) {
  1904. /* Deregister it from the OS, it's gone. */
  1905. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  1906. h->drv[i].busy_configuring = 1;
  1907. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1908. return_code = deregister_disk(h, i, 1, via_ioctl);
  1909. h->drv[i].busy_configuring = 0;
  1910. }
  1911. }
  1912. /* Compare controller drive array to driver's drive array.
  1913. * Check for updates in the drive information and any new drives
  1914. * on the controller due to ACU adding logical drives, or changing
  1915. * a logical drive's size, etc. Reregister any new/changed drives
  1916. */
  1917. for (i = 0; i < num_luns; i++) {
  1918. int j;
  1919. drv_found = 0;
  1920. memcpy(lunid, &ld_buff->LUN[i][0], sizeof(lunid));
  1921. /* Find if the LUN is already in the drive array
  1922. * of the driver. If so then update its info
  1923. * if not in use. If it does not exist then find
  1924. * the first free index and add it.
  1925. */
  1926. for (j = 0; j <= h->highest_lun; j++) {
  1927. if (h->drv[j].raid_level != -1 &&
  1928. memcmp(h->drv[j].LunID, lunid,
  1929. sizeof(h->drv[j].LunID)) == 0) {
  1930. drv_index = j;
  1931. drv_found = 1;
  1932. break;
  1933. }
  1934. }
  1935. /* check if the drive was found already in the array */
  1936. if (!drv_found) {
  1937. drv_index = cciss_add_gendisk(h, lunid, 0);
  1938. if (drv_index == -1)
  1939. goto freeret;
  1940. }
  1941. cciss_update_drive_info(ctlr, drv_index, first_time,
  1942. via_ioctl);
  1943. } /* end for */
  1944. freeret:
  1945. kfree(ld_buff);
  1946. h->busy_configuring = 0;
  1947. /* We return -1 here to tell the ACU that we have registered/updated
  1948. * all of the drives that we can and to keep it from calling us
  1949. * additional times.
  1950. */
  1951. return -1;
  1952. mem_msg:
  1953. printk(KERN_ERR "cciss: out of memory\n");
  1954. h->busy_configuring = 0;
  1955. goto freeret;
  1956. }
  1957. static void cciss_clear_drive_info(drive_info_struct *drive_info)
  1958. {
  1959. /* zero out the disk size info */
  1960. drive_info->nr_blocks = 0;
  1961. drive_info->block_size = 0;
  1962. drive_info->heads = 0;
  1963. drive_info->sectors = 0;
  1964. drive_info->cylinders = 0;
  1965. drive_info->raid_level = -1;
  1966. memset(drive_info->serial_no, 0, sizeof(drive_info->serial_no));
  1967. memset(drive_info->model, 0, sizeof(drive_info->model));
  1968. memset(drive_info->rev, 0, sizeof(drive_info->rev));
  1969. memset(drive_info->vendor, 0, sizeof(drive_info->vendor));
  1970. /*
  1971. * don't clear the LUNID though, we need to remember which
  1972. * one this one is.
  1973. */
  1974. }
  1975. /* This function will deregister the disk and it's queue from the
  1976. * kernel. It must be called with the controller lock held and the
  1977. * drv structures busy_configuring flag set. It's parameters are:
  1978. *
  1979. * disk = This is the disk to be deregistered
  1980. * drv = This is the drive_info_struct associated with the disk to be
  1981. * deregistered. It contains information about the disk used
  1982. * by the driver.
  1983. * clear_all = This flag determines whether or not the disk information
  1984. * is going to be completely cleared out and the highest_lun
  1985. * reset. Sometimes we want to clear out information about
  1986. * the disk in preparation for re-adding it. In this case
  1987. * the highest_lun should be left unchanged and the LunID
  1988. * should not be cleared.
  1989. * via_ioctl
  1990. * This indicates whether we've reached this path via ioctl.
  1991. * This affects the maximum usage count allowed for c0d0 to be messed with.
  1992. * If this path is reached via ioctl(), then the max_usage_count will
  1993. * be 1, as the process calling ioctl() has got to have the device open.
  1994. * If we get here via sysfs, then the max usage count will be zero.
  1995. */
  1996. static int deregister_disk(ctlr_info_t *h, int drv_index,
  1997. int clear_all, int via_ioctl)
  1998. {
  1999. int i;
  2000. struct gendisk *disk;
  2001. drive_info_struct *drv;
  2002. if (!capable(CAP_SYS_RAWIO))
  2003. return -EPERM;
  2004. drv = &h->drv[drv_index];
  2005. disk = h->gendisk[drv_index];
  2006. /* make sure logical volume is NOT is use */
  2007. if (clear_all || (h->gendisk[0] == disk)) {
  2008. if (drv->usage_count > via_ioctl)
  2009. return -EBUSY;
  2010. } else if (drv->usage_count > 0)
  2011. return -EBUSY;
  2012. /* invalidate the devices and deregister the disk. If it is disk
  2013. * zero do not deregister it but just zero out it's values. This
  2014. * allows us to delete disk zero but keep the controller registered.
  2015. */
  2016. if (h->gendisk[0] != disk) {
  2017. struct request_queue *q = disk->queue;
  2018. if (disk->flags & GENHD_FL_UP) {
  2019. cciss_destroy_ld_sysfs_entry(h, drv_index, 0);
  2020. del_gendisk(disk);
  2021. }
  2022. if (q) {
  2023. blk_cleanup_queue(q);
  2024. /* Set drv->queue to NULL so that we do not try
  2025. * to call blk_start_queue on this queue in the
  2026. * interrupt handler
  2027. */
  2028. drv->queue = NULL;
  2029. }
  2030. /* If clear_all is set then we are deleting the logical
  2031. * drive, not just refreshing its info. For drives
  2032. * other than disk 0 we will call put_disk. We do not
  2033. * do this for disk 0 as we need it to be able to
  2034. * configure the controller.
  2035. */
  2036. if (clear_all){
  2037. /* This isn't pretty, but we need to find the
  2038. * disk in our array and NULL our the pointer.
  2039. * This is so that we will call alloc_disk if
  2040. * this index is used again later.
  2041. */
  2042. for (i=0; i < CISS_MAX_LUN; i++){
  2043. if (h->gendisk[i] == disk) {
  2044. h->gendisk[i] = NULL;
  2045. break;
  2046. }
  2047. }
  2048. put_disk(disk);
  2049. }
  2050. } else {
  2051. set_capacity(disk, 0);
  2052. }
  2053. --h->num_luns;
  2054. cciss_clear_drive_info(drv);
  2055. if (clear_all) {
  2056. /* check to see if it was the last disk */
  2057. if (drv == h->drv + h->highest_lun) {
  2058. /* if so, find the new hightest lun */
  2059. int i, newhighest = -1;
  2060. for (i = 0; i <= h->highest_lun; i++) {
  2061. /* if the disk has size > 0, it is available */
  2062. if (h->drv[i].heads)
  2063. newhighest = i;
  2064. }
  2065. h->highest_lun = newhighest;
  2066. }
  2067. memset(drv->LunID, 0, sizeof(drv->LunID));
  2068. }
  2069. return 0;
  2070. }
  2071. static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
  2072. size_t size, __u8 page_code, unsigned char *scsi3addr,
  2073. int cmd_type)
  2074. {
  2075. ctlr_info_t *h = hba[ctlr];
  2076. u64bit buff_dma_handle;
  2077. int status = IO_OK;
  2078. c->cmd_type = CMD_IOCTL_PEND;
  2079. c->Header.ReplyQueue = 0;
  2080. if (buff != NULL) {
  2081. c->Header.SGList = 1;
  2082. c->Header.SGTotal = 1;
  2083. } else {
  2084. c->Header.SGList = 0;
  2085. c->Header.SGTotal = 0;
  2086. }
  2087. c->Header.Tag.lower = c->busaddr;
  2088. memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
  2089. c->Request.Type.Type = cmd_type;
  2090. if (cmd_type == TYPE_CMD) {
  2091. switch (cmd) {
  2092. case CISS_INQUIRY:
  2093. /* are we trying to read a vital product page */
  2094. if (page_code != 0) {
  2095. c->Request.CDB[1] = 0x01;
  2096. c->Request.CDB[2] = page_code;
  2097. }
  2098. c->Request.CDBLen = 6;
  2099. c->Request.Type.Attribute = ATTR_SIMPLE;
  2100. c->Request.Type.Direction = XFER_READ;
  2101. c->Request.Timeout = 0;
  2102. c->Request.CDB[0] = CISS_INQUIRY;
  2103. c->Request.CDB[4] = size & 0xFF;
  2104. break;
  2105. case CISS_REPORT_LOG:
  2106. case CISS_REPORT_PHYS:
  2107. /* Talking to controller so It's a physical command
  2108. mode = 00 target = 0. Nothing to write.
  2109. */
  2110. c->Request.CDBLen = 12;
  2111. c->Request.Type.Attribute = ATTR_SIMPLE;
  2112. c->Request.Type.Direction = XFER_READ;
  2113. c->Request.Timeout = 0;
  2114. c->Request.CDB[0] = cmd;
  2115. c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
  2116. c->Request.CDB[7] = (size >> 16) & 0xFF;
  2117. c->Request.CDB[8] = (size >> 8) & 0xFF;
  2118. c->Request.CDB[9] = size & 0xFF;
  2119. break;
  2120. case CCISS_READ_CAPACITY:
  2121. c->Request.CDBLen = 10;
  2122. c->Request.Type.Attribute = ATTR_SIMPLE;
  2123. c->Request.Type.Direction = XFER_READ;
  2124. c->Request.Timeout = 0;
  2125. c->Request.CDB[0] = cmd;
  2126. break;
  2127. case CCISS_READ_CAPACITY_16:
  2128. c->Request.CDBLen = 16;
  2129. c->Request.Type.Attribute = ATTR_SIMPLE;
  2130. c->Request.Type.Direction = XFER_READ;
  2131. c->Request.Timeout = 0;
  2132. c->Request.CDB[0] = cmd;
  2133. c->Request.CDB[1] = 0x10;
  2134. c->Request.CDB[10] = (size >> 24) & 0xFF;
  2135. c->Request.CDB[11] = (size >> 16) & 0xFF;
  2136. c->Request.CDB[12] = (size >> 8) & 0xFF;
  2137. c->Request.CDB[13] = size & 0xFF;
  2138. c->Request.Timeout = 0;
  2139. c->Request.CDB[0] = cmd;
  2140. break;
  2141. case CCISS_CACHE_FLUSH:
  2142. c->Request.CDBLen = 12;
  2143. c->Request.Type.Attribute = ATTR_SIMPLE;
  2144. c->Request.Type.Direction = XFER_WRITE;
  2145. c->Request.Timeout = 0;
  2146. c->Request.CDB[0] = BMIC_WRITE;
  2147. c->Request.CDB[6] = BMIC_CACHE_FLUSH;
  2148. break;
  2149. case TEST_UNIT_READY:
  2150. c->Request.CDBLen = 6;
  2151. c->Request.Type.Attribute = ATTR_SIMPLE;
  2152. c->Request.Type.Direction = XFER_NONE;
  2153. c->Request.Timeout = 0;
  2154. break;
  2155. default:
  2156. printk(KERN_WARNING
  2157. "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
  2158. return IO_ERROR;
  2159. }
  2160. } else if (cmd_type == TYPE_MSG) {
  2161. switch (cmd) {
  2162. case 0: /* ABORT message */
  2163. c->Request.CDBLen = 12;
  2164. c->Request.Type.Attribute = ATTR_SIMPLE;
  2165. c->Request.Type.Direction = XFER_WRITE;
  2166. c->Request.Timeout = 0;
  2167. c->Request.CDB[0] = cmd; /* abort */
  2168. c->Request.CDB[1] = 0; /* abort a command */
  2169. /* buff contains the tag of the command to abort */
  2170. memcpy(&c->Request.CDB[4], buff, 8);
  2171. break;
  2172. case 1: /* RESET message */
  2173. c->Request.CDBLen = 16;
  2174. c->Request.Type.Attribute = ATTR_SIMPLE;
  2175. c->Request.Type.Direction = XFER_NONE;
  2176. c->Request.Timeout = 0;
  2177. memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
  2178. c->Request.CDB[0] = cmd; /* reset */
  2179. c->Request.CDB[1] = 0x03; /* reset a target */
  2180. break;
  2181. case 3: /* No-Op message */
  2182. c->Request.CDBLen = 1;
  2183. c->Request.Type.Attribute = ATTR_SIMPLE;
  2184. c->Request.Type.Direction = XFER_WRITE;
  2185. c->Request.Timeout = 0;
  2186. c->Request.CDB[0] = cmd;
  2187. break;
  2188. default:
  2189. printk(KERN_WARNING
  2190. "cciss%d: unknown message type %d\n", ctlr, cmd);
  2191. return IO_ERROR;
  2192. }
  2193. } else {
  2194. printk(KERN_WARNING
  2195. "cciss%d: unknown command type %d\n", ctlr, cmd_type);
  2196. return IO_ERROR;
  2197. }
  2198. /* Fill in the scatter gather information */
  2199. if (size > 0) {
  2200. buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
  2201. buff, size,
  2202. PCI_DMA_BIDIRECTIONAL);
  2203. c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
  2204. c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
  2205. c->SG[0].Len = size;
  2206. c->SG[0].Ext = 0; /* we are not chaining */
  2207. }
  2208. return status;
  2209. }
  2210. static int check_target_status(ctlr_info_t *h, CommandList_struct *c)
  2211. {
  2212. switch (c->err_info->ScsiStatus) {
  2213. case SAM_STAT_GOOD:
  2214. return IO_OK;
  2215. case SAM_STAT_CHECK_CONDITION:
  2216. switch (0xf & c->err_info->SenseInfo[2]) {
  2217. case 0: return IO_OK; /* no sense */
  2218. case 1: return IO_OK; /* recovered error */
  2219. default:
  2220. printk(KERN_WARNING "cciss%d: cmd 0x%02x "
  2221. "check condition, sense key = 0x%02x\n",
  2222. h->ctlr, c->Request.CDB[0],
  2223. c->err_info->SenseInfo[2]);
  2224. }
  2225. break;
  2226. default:
  2227. printk(KERN_WARNING "cciss%d: cmd 0x%02x"
  2228. "scsi status = 0x%02x\n", h->ctlr,
  2229. c->Request.CDB[0], c->err_info->ScsiStatus);
  2230. break;
  2231. }
  2232. return IO_ERROR;
  2233. }
  2234. static int process_sendcmd_error(ctlr_info_t *h, CommandList_struct *c)
  2235. {
  2236. int return_status = IO_OK;
  2237. if (c->err_info->CommandStatus == CMD_SUCCESS)
  2238. return IO_OK;
  2239. switch (c->err_info->CommandStatus) {
  2240. case CMD_TARGET_STATUS:
  2241. return_status = check_target_status(h, c);
  2242. break;
  2243. case CMD_DATA_UNDERRUN:
  2244. case CMD_DATA_OVERRUN:
  2245. /* expected for inquiry and report lun commands */
  2246. break;
  2247. case CMD_INVALID:
  2248. printk(KERN_WARNING "cciss: cmd 0x%02x is "
  2249. "reported invalid\n", c->Request.CDB[0]);
  2250. return_status = IO_ERROR;
  2251. break;
  2252. case CMD_PROTOCOL_ERR:
  2253. printk(KERN_WARNING "cciss: cmd 0x%02x has "
  2254. "protocol error \n", c->Request.CDB[0]);
  2255. return_status = IO_ERROR;
  2256. break;
  2257. case CMD_HARDWARE_ERR:
  2258. printk(KERN_WARNING "cciss: cmd 0x%02x had "
  2259. " hardware error\n", c->Request.CDB[0]);
  2260. return_status = IO_ERROR;
  2261. break;
  2262. case CMD_CONNECTION_LOST:
  2263. printk(KERN_WARNING "cciss: cmd 0x%02x had "
  2264. "connection lost\n", c->Request.CDB[0]);
  2265. return_status = IO_ERROR;
  2266. break;
  2267. case CMD_ABORTED:
  2268. printk(KERN_WARNING "cciss: cmd 0x%02x was "
  2269. "aborted\n", c->Request.CDB[0]);
  2270. return_status = IO_ERROR;
  2271. break;
  2272. case CMD_ABORT_FAILED:
  2273. printk(KERN_WARNING "cciss: cmd 0x%02x reports "
  2274. "abort failed\n", c->Request.CDB[0]);
  2275. return_status = IO_ERROR;
  2276. break;
  2277. case CMD_UNSOLICITED_ABORT:
  2278. printk(KERN_WARNING
  2279. "cciss%d: unsolicited abort 0x%02x\n", h->ctlr,
  2280. c->Request.CDB[0]);
  2281. return_status = IO_NEEDS_RETRY;
  2282. break;
  2283. default:
  2284. printk(KERN_WARNING "cciss: cmd 0x%02x returned "
  2285. "unknown status %x\n", c->Request.CDB[0],
  2286. c->err_info->CommandStatus);
  2287. return_status = IO_ERROR;
  2288. }
  2289. return return_status;
  2290. }
  2291. static int sendcmd_withirq_core(ctlr_info_t *h, CommandList_struct *c,
  2292. int attempt_retry)
  2293. {
  2294. DECLARE_COMPLETION_ONSTACK(wait);
  2295. u64bit buff_dma_handle;
  2296. unsigned long flags;
  2297. int return_status = IO_OK;
  2298. resend_cmd2:
  2299. c->waiting = &wait;
  2300. /* Put the request on the tail of the queue and send it */
  2301. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  2302. addQ(&h->reqQ, c);
  2303. h->Qdepth++;
  2304. start_io(h);
  2305. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  2306. wait_for_completion(&wait);
  2307. if (c->err_info->CommandStatus == 0 || !attempt_retry)
  2308. goto command_done;
  2309. return_status = process_sendcmd_error(h, c);
  2310. if (return_status == IO_NEEDS_RETRY &&
  2311. c->retry_count < MAX_CMD_RETRIES) {
  2312. printk(KERN_WARNING "cciss%d: retrying 0x%02x\n", h->ctlr,
  2313. c->Request.CDB[0]);
  2314. c->retry_count++;
  2315. /* erase the old error information */
  2316. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  2317. return_status = IO_OK;
  2318. INIT_COMPLETION(wait);
  2319. goto resend_cmd2;
  2320. }
  2321. command_done:
  2322. /* unlock the buffers from DMA */
  2323. buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
  2324. buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
  2325. pci_unmap_single(h->pdev, (dma_addr_t) buff_dma_handle.val,
  2326. c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
  2327. return return_status;
  2328. }
  2329. static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size,
  2330. __u8 page_code, unsigned char scsi3addr[],
  2331. int cmd_type)
  2332. {
  2333. ctlr_info_t *h = hba[ctlr];
  2334. CommandList_struct *c;
  2335. int return_status;
  2336. c = cmd_alloc(h, 0);
  2337. if (!c)
  2338. return -ENOMEM;
  2339. return_status = fill_cmd(c, cmd, ctlr, buff, size, page_code,
  2340. scsi3addr, cmd_type);
  2341. if (return_status == IO_OK)
  2342. return_status = sendcmd_withirq_core(h, c, 1);
  2343. cmd_free(h, c, 0);
  2344. return return_status;
  2345. }
  2346. static void cciss_geometry_inquiry(int ctlr, int logvol,
  2347. int withirq, sector_t total_size,
  2348. unsigned int block_size,
  2349. InquiryData_struct *inq_buff,
  2350. drive_info_struct *drv)
  2351. {
  2352. int return_code;
  2353. unsigned long t;
  2354. unsigned char scsi3addr[8];
  2355. memset(inq_buff, 0, sizeof(InquiryData_struct));
  2356. log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol);
  2357. if (withirq)
  2358. return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
  2359. inq_buff, sizeof(*inq_buff),
  2360. 0xC1, scsi3addr, TYPE_CMD);
  2361. else
  2362. return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
  2363. sizeof(*inq_buff), 0xC1, scsi3addr,
  2364. TYPE_CMD);
  2365. if (return_code == IO_OK) {
  2366. if (inq_buff->data_byte[8] == 0xFF) {
  2367. printk(KERN_WARNING
  2368. "cciss: reading geometry failed, volume "
  2369. "does not support reading geometry\n");
  2370. drv->heads = 255;
  2371. drv->sectors = 32; // Sectors per track
  2372. drv->cylinders = total_size + 1;
  2373. drv->raid_level = RAID_UNKNOWN;
  2374. } else {
  2375. drv->heads = inq_buff->data_byte[6];
  2376. drv->sectors = inq_buff->data_byte[7];
  2377. drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
  2378. drv->cylinders += inq_buff->data_byte[5];
  2379. drv->raid_level = inq_buff->data_byte[8];
  2380. }
  2381. drv->block_size = block_size;
  2382. drv->nr_blocks = total_size + 1;
  2383. t = drv->heads * drv->sectors;
  2384. if (t > 1) {
  2385. sector_t real_size = total_size + 1;
  2386. unsigned long rem = sector_div(real_size, t);
  2387. if (rem)
  2388. real_size++;
  2389. drv->cylinders = real_size;
  2390. }
  2391. } else { /* Get geometry failed */
  2392. printk(KERN_WARNING "cciss: reading geometry failed\n");
  2393. }
  2394. }
  2395. static void
  2396. cciss_read_capacity(int ctlr, int logvol, int withirq, sector_t *total_size,
  2397. unsigned int *block_size)
  2398. {
  2399. ReadCapdata_struct *buf;
  2400. int return_code;
  2401. unsigned char scsi3addr[8];
  2402. buf = kzalloc(sizeof(ReadCapdata_struct), GFP_KERNEL);
  2403. if (!buf) {
  2404. printk(KERN_WARNING "cciss: out of memory\n");
  2405. return;
  2406. }
  2407. log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol);
  2408. if (withirq)
  2409. return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
  2410. ctlr, buf, sizeof(ReadCapdata_struct),
  2411. 0, scsi3addr, TYPE_CMD);
  2412. else
  2413. return_code = sendcmd(CCISS_READ_CAPACITY,
  2414. ctlr, buf, sizeof(ReadCapdata_struct),
  2415. 0, scsi3addr, TYPE_CMD);
  2416. if (return_code == IO_OK) {
  2417. *total_size = be32_to_cpu(*(__be32 *) buf->total_size);
  2418. *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
  2419. } else { /* read capacity command failed */
  2420. printk(KERN_WARNING "cciss: read capacity failed\n");
  2421. *total_size = 0;
  2422. *block_size = BLOCK_SIZE;
  2423. }
  2424. kfree(buf);
  2425. }
  2426. static void
  2427. cciss_read_capacity_16(int ctlr, int logvol, int withirq, sector_t *total_size, unsigned int *block_size)
  2428. {
  2429. ReadCapdata_struct_16 *buf;
  2430. int return_code;
  2431. unsigned char scsi3addr[8];
  2432. buf = kzalloc(sizeof(ReadCapdata_struct_16), GFP_KERNEL);
  2433. if (!buf) {
  2434. printk(KERN_WARNING "cciss: out of memory\n");
  2435. return;
  2436. }
  2437. log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol);
  2438. if (withirq) {
  2439. return_code = sendcmd_withirq(CCISS_READ_CAPACITY_16,
  2440. ctlr, buf, sizeof(ReadCapdata_struct_16),
  2441. 0, scsi3addr, TYPE_CMD);
  2442. }
  2443. else {
  2444. return_code = sendcmd(CCISS_READ_CAPACITY_16,
  2445. ctlr, buf, sizeof(ReadCapdata_struct_16),
  2446. 0, scsi3addr, TYPE_CMD);
  2447. }
  2448. if (return_code == IO_OK) {
  2449. *total_size = be64_to_cpu(*(__be64 *) buf->total_size);
  2450. *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
  2451. } else { /* read capacity command failed */
  2452. printk(KERN_WARNING "cciss: read capacity failed\n");
  2453. *total_size = 0;
  2454. *block_size = BLOCK_SIZE;
  2455. }
  2456. printk(KERN_INFO " blocks= %llu block_size= %d\n",
  2457. (unsigned long long)*total_size+1, *block_size);
  2458. kfree(buf);
  2459. }
  2460. static int cciss_revalidate(struct gendisk *disk)
  2461. {
  2462. ctlr_info_t *h = get_host(disk);
  2463. drive_info_struct *drv = get_drv(disk);
  2464. int logvol;
  2465. int FOUND = 0;
  2466. unsigned int block_size;
  2467. sector_t total_size;
  2468. InquiryData_struct *inq_buff = NULL;
  2469. for (logvol = 0; logvol < CISS_MAX_LUN; logvol++) {
  2470. if (memcmp(h->drv[logvol].LunID, drv->LunID,
  2471. sizeof(drv->LunID)) == 0) {
  2472. FOUND = 1;
  2473. break;
  2474. }
  2475. }
  2476. if (!FOUND)
  2477. return 1;
  2478. inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
  2479. if (inq_buff == NULL) {
  2480. printk(KERN_WARNING "cciss: out of memory\n");
  2481. return 1;
  2482. }
  2483. if (h->cciss_read == CCISS_READ_10) {
  2484. cciss_read_capacity(h->ctlr, logvol, 1,
  2485. &total_size, &block_size);
  2486. } else {
  2487. cciss_read_capacity_16(h->ctlr, logvol, 1,
  2488. &total_size, &block_size);
  2489. }
  2490. cciss_geometry_inquiry(h->ctlr, logvol, 1, total_size, block_size,
  2491. inq_buff, drv);
  2492. blk_queue_logical_block_size(drv->queue, drv->block_size);
  2493. set_capacity(disk, drv->nr_blocks);
  2494. kfree(inq_buff);
  2495. return 0;
  2496. }
  2497. /*
  2498. * Wait polling for a command to complete.
  2499. * The memory mapped FIFO is polled for the completion.
  2500. * Used only at init time, interrupts from the HBA are disabled.
  2501. */
  2502. static unsigned long pollcomplete(int ctlr)
  2503. {
  2504. unsigned long done;
  2505. int i;
  2506. /* Wait (up to 20 seconds) for a command to complete */
  2507. for (i = 20 * HZ; i > 0; i--) {
  2508. done = hba[ctlr]->access.command_completed(hba[ctlr]);
  2509. if (done == FIFO_EMPTY)
  2510. schedule_timeout_uninterruptible(1);
  2511. else
  2512. return done;
  2513. }
  2514. /* Invalid address to tell caller we ran out of time */
  2515. return 1;
  2516. }
  2517. /* Send command c to controller h and poll for it to complete.
  2518. * Turns interrupts off on the board. Used at driver init time
  2519. * and during SCSI error recovery.
  2520. */
  2521. static int sendcmd_core(ctlr_info_t *h, CommandList_struct *c)
  2522. {
  2523. int i;
  2524. unsigned long complete;
  2525. int status = IO_ERROR;
  2526. u64bit buff_dma_handle;
  2527. resend_cmd1:
  2528. /* Disable interrupt on the board. */
  2529. h->access.set_intr_mask(h, CCISS_INTR_OFF);
  2530. /* Make sure there is room in the command FIFO */
  2531. /* Actually it should be completely empty at this time */
  2532. /* unless we are in here doing error handling for the scsi */
  2533. /* tape side of the driver. */
  2534. for (i = 200000; i > 0; i--) {
  2535. /* if fifo isn't full go */
  2536. if (!(h->access.fifo_full(h)))
  2537. break;
  2538. udelay(10);
  2539. printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
  2540. " waiting!\n", h->ctlr);
  2541. }
  2542. h->access.submit_command(h, c); /* Send the cmd */
  2543. do {
  2544. complete = pollcomplete(h->ctlr);
  2545. #ifdef CCISS_DEBUG
  2546. printk(KERN_DEBUG "cciss: command completed\n");
  2547. #endif /* CCISS_DEBUG */
  2548. if (complete == 1) {
  2549. printk(KERN_WARNING
  2550. "cciss cciss%d: SendCmd Timeout out, "
  2551. "No command list address returned!\n", h->ctlr);
  2552. status = IO_ERROR;
  2553. break;
  2554. }
  2555. /* Make sure it's the command we're expecting. */
  2556. if ((complete & ~CISS_ERROR_BIT) != c->busaddr) {
  2557. printk(KERN_WARNING "cciss%d: Unexpected command "
  2558. "completion.\n", h->ctlr);
  2559. continue;
  2560. }
  2561. /* It is our command. If no error, we're done. */
  2562. if (!(complete & CISS_ERROR_BIT)) {
  2563. status = IO_OK;
  2564. break;
  2565. }
  2566. /* There is an error... */
  2567. /* if data overrun or underun on Report command ignore it */
  2568. if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
  2569. (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
  2570. (c->Request.CDB[0] == CISS_INQUIRY)) &&
  2571. ((c->err_info->CommandStatus == CMD_DATA_OVERRUN) ||
  2572. (c->err_info->CommandStatus == CMD_DATA_UNDERRUN))) {
  2573. complete = c->busaddr;
  2574. status = IO_OK;
  2575. break;
  2576. }
  2577. if (c->err_info->CommandStatus == CMD_UNSOLICITED_ABORT) {
  2578. printk(KERN_WARNING "cciss%d: unsolicited abort %p\n",
  2579. h->ctlr, c);
  2580. if (c->retry_count < MAX_CMD_RETRIES) {
  2581. printk(KERN_WARNING "cciss%d: retrying %p\n",
  2582. h->ctlr, c);
  2583. c->retry_count++;
  2584. /* erase the old error information */
  2585. memset(c->err_info, 0, sizeof(c->err_info));
  2586. goto resend_cmd1;
  2587. }
  2588. printk(KERN_WARNING "cciss%d: retried %p too many "
  2589. "times\n", h->ctlr, c);
  2590. status = IO_ERROR;
  2591. break;
  2592. }
  2593. if (c->err_info->CommandStatus == CMD_UNABORTABLE) {
  2594. printk(KERN_WARNING "cciss%d: command could not be "
  2595. "aborted.\n", h->ctlr);
  2596. status = IO_ERROR;
  2597. break;
  2598. }
  2599. if (c->err_info->CommandStatus == CMD_TARGET_STATUS) {
  2600. status = check_target_status(h, c);
  2601. break;
  2602. }
  2603. printk(KERN_WARNING "cciss%d: sendcmd error\n", h->ctlr);
  2604. printk(KERN_WARNING "cmd = 0x%02x, CommandStatus = 0x%02x\n",
  2605. c->Request.CDB[0], c->err_info->CommandStatus);
  2606. status = IO_ERROR;
  2607. break;
  2608. } while (1);
  2609. /* unlock the data buffer from DMA */
  2610. buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
  2611. buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
  2612. pci_unmap_single(h->pdev, (dma_addr_t) buff_dma_handle.val,
  2613. c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
  2614. return status;
  2615. }
  2616. /*
  2617. * Send a command to the controller, and wait for it to complete.
  2618. * Used at init time, and during SCSI error recovery.
  2619. */
  2620. static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size,
  2621. __u8 page_code, unsigned char *scsi3addr, int cmd_type)
  2622. {
  2623. CommandList_struct *c;
  2624. int status;
  2625. c = cmd_alloc(hba[ctlr], 1);
  2626. if (!c) {
  2627. printk(KERN_WARNING "cciss: unable to get memory");
  2628. return IO_ERROR;
  2629. }
  2630. status = fill_cmd(c, cmd, ctlr, buff, size, page_code,
  2631. scsi3addr, cmd_type);
  2632. if (status == IO_OK)
  2633. status = sendcmd_core(hba[ctlr], c);
  2634. cmd_free(hba[ctlr], c, 1);
  2635. return status;
  2636. }
  2637. /*
  2638. * Map (physical) PCI mem into (virtual) kernel space
  2639. */
  2640. static void __iomem *remap_pci_mem(ulong base, ulong size)
  2641. {
  2642. ulong page_base = ((ulong) base) & PAGE_MASK;
  2643. ulong page_offs = ((ulong) base) - page_base;
  2644. void __iomem *page_remapped = ioremap(page_base, page_offs + size);
  2645. return page_remapped ? (page_remapped + page_offs) : NULL;
  2646. }
  2647. /*
  2648. * Takes jobs of the Q and sends them to the hardware, then puts it on
  2649. * the Q to wait for completion.
  2650. */
  2651. static void start_io(ctlr_info_t *h)
  2652. {
  2653. CommandList_struct *c;
  2654. while (!hlist_empty(&h->reqQ)) {
  2655. c = hlist_entry(h->reqQ.first, CommandList_struct, list);
  2656. /* can't do anything if fifo is full */
  2657. if ((h->access.fifo_full(h))) {
  2658. printk(KERN_WARNING "cciss: fifo full\n");
  2659. break;
  2660. }
  2661. /* Get the first entry from the Request Q */
  2662. removeQ(c);
  2663. h->Qdepth--;
  2664. /* Tell the controller execute command */
  2665. h->access.submit_command(h, c);
  2666. /* Put job onto the completed Q */
  2667. addQ(&h->cmpQ, c);
  2668. }
  2669. }
  2670. /* Assumes that CCISS_LOCK(h->ctlr) is held. */
  2671. /* Zeros out the error record and then resends the command back */
  2672. /* to the controller */
  2673. static inline void resend_cciss_cmd(ctlr_info_t *h, CommandList_struct *c)
  2674. {
  2675. /* erase the old error information */
  2676. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  2677. /* add it to software queue and then send it to the controller */
  2678. addQ(&h->reqQ, c);
  2679. h->Qdepth++;
  2680. if (h->Qdepth > h->maxQsinceinit)
  2681. h->maxQsinceinit = h->Qdepth;
  2682. start_io(h);
  2683. }
  2684. static inline unsigned int make_status_bytes(unsigned int scsi_status_byte,
  2685. unsigned int msg_byte, unsigned int host_byte,
  2686. unsigned int driver_byte)
  2687. {
  2688. /* inverse of macros in scsi.h */
  2689. return (scsi_status_byte & 0xff) |
  2690. ((msg_byte & 0xff) << 8) |
  2691. ((host_byte & 0xff) << 16) |
  2692. ((driver_byte & 0xff) << 24);
  2693. }
  2694. static inline int evaluate_target_status(ctlr_info_t *h,
  2695. CommandList_struct *cmd, int *retry_cmd)
  2696. {
  2697. unsigned char sense_key;
  2698. unsigned char status_byte, msg_byte, host_byte, driver_byte;
  2699. int error_value;
  2700. *retry_cmd = 0;
  2701. /* If we get in here, it means we got "target status", that is, scsi status */
  2702. status_byte = cmd->err_info->ScsiStatus;
  2703. driver_byte = DRIVER_OK;
  2704. msg_byte = cmd->err_info->CommandStatus; /* correct? seems too device specific */
  2705. if (blk_pc_request(cmd->rq))
  2706. host_byte = DID_PASSTHROUGH;
  2707. else
  2708. host_byte = DID_OK;
  2709. error_value = make_status_bytes(status_byte, msg_byte,
  2710. host_byte, driver_byte);
  2711. if (cmd->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION) {
  2712. if (!blk_pc_request(cmd->rq))
  2713. printk(KERN_WARNING "cciss: cmd %p "
  2714. "has SCSI Status 0x%x\n",
  2715. cmd, cmd->err_info->ScsiStatus);
  2716. return error_value;
  2717. }
  2718. /* check the sense key */
  2719. sense_key = 0xf & cmd->err_info->SenseInfo[2];
  2720. /* no status or recovered error */
  2721. if (((sense_key == 0x0) || (sense_key == 0x1)) && !blk_pc_request(cmd->rq))
  2722. error_value = 0;
  2723. if (check_for_unit_attention(h, cmd)) {
  2724. *retry_cmd = !blk_pc_request(cmd->rq);
  2725. return 0;
  2726. }
  2727. if (!blk_pc_request(cmd->rq)) { /* Not SG_IO or similar? */
  2728. if (error_value != 0)
  2729. printk(KERN_WARNING "cciss: cmd %p has CHECK CONDITION"
  2730. " sense key = 0x%x\n", cmd, sense_key);
  2731. return error_value;
  2732. }
  2733. /* SG_IO or similar, copy sense data back */
  2734. if (cmd->rq->sense) {
  2735. if (cmd->rq->sense_len > cmd->err_info->SenseLen)
  2736. cmd->rq->sense_len = cmd->err_info->SenseLen;
  2737. memcpy(cmd->rq->sense, cmd->err_info->SenseInfo,
  2738. cmd->rq->sense_len);
  2739. } else
  2740. cmd->rq->sense_len = 0;
  2741. return error_value;
  2742. }
  2743. /* checks the status of the job and calls complete buffers to mark all
  2744. * buffers for the completed job. Note that this function does not need
  2745. * to hold the hba/queue lock.
  2746. */
  2747. static inline void complete_command(ctlr_info_t *h, CommandList_struct *cmd,
  2748. int timeout)
  2749. {
  2750. int retry_cmd = 0;
  2751. struct request *rq = cmd->rq;
  2752. rq->errors = 0;
  2753. if (timeout)
  2754. rq->errors = make_status_bytes(0, 0, 0, DRIVER_TIMEOUT);
  2755. if (cmd->err_info->CommandStatus == 0) /* no error has occurred */
  2756. goto after_error_processing;
  2757. switch (cmd->err_info->CommandStatus) {
  2758. case CMD_TARGET_STATUS:
  2759. rq->errors = evaluate_target_status(h, cmd, &retry_cmd);
  2760. break;
  2761. case CMD_DATA_UNDERRUN:
  2762. if (blk_fs_request(cmd->rq)) {
  2763. printk(KERN_WARNING "cciss: cmd %p has"
  2764. " completed with data underrun "
  2765. "reported\n", cmd);
  2766. cmd->rq->resid_len = cmd->err_info->ResidualCnt;
  2767. }
  2768. break;
  2769. case CMD_DATA_OVERRUN:
  2770. if (blk_fs_request(cmd->rq))
  2771. printk(KERN_WARNING "cciss: cmd %p has"
  2772. " completed with data overrun "
  2773. "reported\n", cmd);
  2774. break;
  2775. case CMD_INVALID:
  2776. printk(KERN_WARNING "cciss: cmd %p is "
  2777. "reported invalid\n", cmd);
  2778. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2779. cmd->err_info->CommandStatus, DRIVER_OK,
  2780. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
  2781. break;
  2782. case CMD_PROTOCOL_ERR:
  2783. printk(KERN_WARNING "cciss: cmd %p has "
  2784. "protocol error \n", cmd);
  2785. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2786. cmd->err_info->CommandStatus, DRIVER_OK,
  2787. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
  2788. break;
  2789. case CMD_HARDWARE_ERR:
  2790. printk(KERN_WARNING "cciss: cmd %p had "
  2791. " hardware error\n", cmd);
  2792. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2793. cmd->err_info->CommandStatus, DRIVER_OK,
  2794. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
  2795. break;
  2796. case CMD_CONNECTION_LOST:
  2797. printk(KERN_WARNING "cciss: cmd %p had "
  2798. "connection lost\n", cmd);
  2799. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2800. cmd->err_info->CommandStatus, DRIVER_OK,
  2801. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
  2802. break;
  2803. case CMD_ABORTED:
  2804. printk(KERN_WARNING "cciss: cmd %p was "
  2805. "aborted\n", cmd);
  2806. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2807. cmd->err_info->CommandStatus, DRIVER_OK,
  2808. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ABORT);
  2809. break;
  2810. case CMD_ABORT_FAILED:
  2811. printk(KERN_WARNING "cciss: cmd %p reports "
  2812. "abort failed\n", cmd);
  2813. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2814. cmd->err_info->CommandStatus, DRIVER_OK,
  2815. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
  2816. break;
  2817. case CMD_UNSOLICITED_ABORT:
  2818. printk(KERN_WARNING "cciss%d: unsolicited "
  2819. "abort %p\n", h->ctlr, cmd);
  2820. if (cmd->retry_count < MAX_CMD_RETRIES) {
  2821. retry_cmd = 1;
  2822. printk(KERN_WARNING
  2823. "cciss%d: retrying %p\n", h->ctlr, cmd);
  2824. cmd->retry_count++;
  2825. } else
  2826. printk(KERN_WARNING
  2827. "cciss%d: %p retried too "
  2828. "many times\n", h->ctlr, cmd);
  2829. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2830. cmd->err_info->CommandStatus, DRIVER_OK,
  2831. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ABORT);
  2832. break;
  2833. case CMD_TIMEOUT:
  2834. printk(KERN_WARNING "cciss: cmd %p timedout\n", cmd);
  2835. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2836. cmd->err_info->CommandStatus, DRIVER_OK,
  2837. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
  2838. break;
  2839. default:
  2840. printk(KERN_WARNING "cciss: cmd %p returned "
  2841. "unknown status %x\n", cmd,
  2842. cmd->err_info->CommandStatus);
  2843. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2844. cmd->err_info->CommandStatus, DRIVER_OK,
  2845. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
  2846. }
  2847. after_error_processing:
  2848. /* We need to return this command */
  2849. if (retry_cmd) {
  2850. resend_cciss_cmd(h, cmd);
  2851. return;
  2852. }
  2853. cmd->rq->completion_data = cmd;
  2854. blk_complete_request(cmd->rq);
  2855. }
  2856. /*
  2857. * Get a request and submit it to the controller.
  2858. */
  2859. static void do_cciss_request(struct request_queue *q)
  2860. {
  2861. ctlr_info_t *h = q->queuedata;
  2862. CommandList_struct *c;
  2863. sector_t start_blk;
  2864. int seg;
  2865. struct request *creq;
  2866. u64bit temp64;
  2867. struct scatterlist tmp_sg[MAXSGENTRIES];
  2868. drive_info_struct *drv;
  2869. int i, dir;
  2870. /* We call start_io here in case there is a command waiting on the
  2871. * queue that has not been sent.
  2872. */
  2873. if (blk_queue_plugged(q))
  2874. goto startio;
  2875. queue:
  2876. creq = blk_peek_request(q);
  2877. if (!creq)
  2878. goto startio;
  2879. BUG_ON(creq->nr_phys_segments > MAXSGENTRIES);
  2880. if ((c = cmd_alloc(h, 1)) == NULL)
  2881. goto full;
  2882. blk_start_request(creq);
  2883. spin_unlock_irq(q->queue_lock);
  2884. c->cmd_type = CMD_RWREQ;
  2885. c->rq = creq;
  2886. /* fill in the request */
  2887. drv = creq->rq_disk->private_data;
  2888. c->Header.ReplyQueue = 0; // unused in simple mode
  2889. /* got command from pool, so use the command block index instead */
  2890. /* for direct lookups. */
  2891. /* The first 2 bits are reserved for controller error reporting. */
  2892. c->Header.Tag.lower = (c->cmdindex << 3);
  2893. c->Header.Tag.lower |= 0x04; /* flag for direct lookup. */
  2894. memcpy(&c->Header.LUN, drv->LunID, sizeof(drv->LunID));
  2895. c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
  2896. c->Request.Type.Type = TYPE_CMD; // It is a command.
  2897. c->Request.Type.Attribute = ATTR_SIMPLE;
  2898. c->Request.Type.Direction =
  2899. (rq_data_dir(creq) == READ) ? XFER_READ : XFER_WRITE;
  2900. c->Request.Timeout = 0; // Don't time out
  2901. c->Request.CDB[0] =
  2902. (rq_data_dir(creq) == READ) ? h->cciss_read : h->cciss_write;
  2903. start_blk = blk_rq_pos(creq);
  2904. #ifdef CCISS_DEBUG
  2905. printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n",
  2906. (int)blk_rq_pos(creq), (int)blk_rq_sectors(creq));
  2907. #endif /* CCISS_DEBUG */
  2908. sg_init_table(tmp_sg, MAXSGENTRIES);
  2909. seg = blk_rq_map_sg(q, creq, tmp_sg);
  2910. /* get the DMA records for the setup */
  2911. if (c->Request.Type.Direction == XFER_READ)
  2912. dir = PCI_DMA_FROMDEVICE;
  2913. else
  2914. dir = PCI_DMA_TODEVICE;
  2915. for (i = 0; i < seg; i++) {
  2916. c->SG[i].Len = tmp_sg[i].length;
  2917. temp64.val = (__u64) pci_map_page(h->pdev, sg_page(&tmp_sg[i]),
  2918. tmp_sg[i].offset,
  2919. tmp_sg[i].length, dir);
  2920. c->SG[i].Addr.lower = temp64.val32.lower;
  2921. c->SG[i].Addr.upper = temp64.val32.upper;
  2922. c->SG[i].Ext = 0; // we are not chaining
  2923. }
  2924. /* track how many SG entries we are using */
  2925. if (seg > h->maxSG)
  2926. h->maxSG = seg;
  2927. #ifdef CCISS_DEBUG
  2928. printk(KERN_DEBUG "cciss: Submitting %u sectors in %d segments\n",
  2929. blk_rq_sectors(creq), seg);
  2930. #endif /* CCISS_DEBUG */
  2931. c->Header.SGList = c->Header.SGTotal = seg;
  2932. if (likely(blk_fs_request(creq))) {
  2933. if(h->cciss_read == CCISS_READ_10) {
  2934. c->Request.CDB[1] = 0;
  2935. c->Request.CDB[2] = (start_blk >> 24) & 0xff; //MSB
  2936. c->Request.CDB[3] = (start_blk >> 16) & 0xff;
  2937. c->Request.CDB[4] = (start_blk >> 8) & 0xff;
  2938. c->Request.CDB[5] = start_blk & 0xff;
  2939. c->Request.CDB[6] = 0; // (sect >> 24) & 0xff; MSB
  2940. c->Request.CDB[7] = (blk_rq_sectors(creq) >> 8) & 0xff;
  2941. c->Request.CDB[8] = blk_rq_sectors(creq) & 0xff;
  2942. c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
  2943. } else {
  2944. u32 upper32 = upper_32_bits(start_blk);
  2945. c->Request.CDBLen = 16;
  2946. c->Request.CDB[1]= 0;
  2947. c->Request.CDB[2]= (upper32 >> 24) & 0xff; //MSB
  2948. c->Request.CDB[3]= (upper32 >> 16) & 0xff;
  2949. c->Request.CDB[4]= (upper32 >> 8) & 0xff;
  2950. c->Request.CDB[5]= upper32 & 0xff;
  2951. c->Request.CDB[6]= (start_blk >> 24) & 0xff;
  2952. c->Request.CDB[7]= (start_blk >> 16) & 0xff;
  2953. c->Request.CDB[8]= (start_blk >> 8) & 0xff;
  2954. c->Request.CDB[9]= start_blk & 0xff;
  2955. c->Request.CDB[10]= (blk_rq_sectors(creq) >> 24) & 0xff;
  2956. c->Request.CDB[11]= (blk_rq_sectors(creq) >> 16) & 0xff;
  2957. c->Request.CDB[12]= (blk_rq_sectors(creq) >> 8) & 0xff;
  2958. c->Request.CDB[13]= blk_rq_sectors(creq) & 0xff;
  2959. c->Request.CDB[14] = c->Request.CDB[15] = 0;
  2960. }
  2961. } else if (blk_pc_request(creq)) {
  2962. c->Request.CDBLen = creq->cmd_len;
  2963. memcpy(c->Request.CDB, creq->cmd, BLK_MAX_CDB);
  2964. } else {
  2965. printk(KERN_WARNING "cciss%d: bad request type %d\n", h->ctlr, creq->cmd_type);
  2966. BUG();
  2967. }
  2968. spin_lock_irq(q->queue_lock);
  2969. addQ(&h->reqQ, c);
  2970. h->Qdepth++;
  2971. if (h->Qdepth > h->maxQsinceinit)
  2972. h->maxQsinceinit = h->Qdepth;
  2973. goto queue;
  2974. full:
  2975. blk_stop_queue(q);
  2976. startio:
  2977. /* We will already have the driver lock here so not need
  2978. * to lock it.
  2979. */
  2980. start_io(h);
  2981. }
  2982. static inline unsigned long get_next_completion(ctlr_info_t *h)
  2983. {
  2984. return h->access.command_completed(h);
  2985. }
  2986. static inline int interrupt_pending(ctlr_info_t *h)
  2987. {
  2988. return h->access.intr_pending(h);
  2989. }
  2990. static inline long interrupt_not_for_us(ctlr_info_t *h)
  2991. {
  2992. return (((h->access.intr_pending(h) == 0) ||
  2993. (h->interrupts_enabled == 0)));
  2994. }
  2995. static irqreturn_t do_cciss_intr(int irq, void *dev_id)
  2996. {
  2997. ctlr_info_t *h = dev_id;
  2998. CommandList_struct *c;
  2999. unsigned long flags;
  3000. __u32 a, a1, a2;
  3001. if (interrupt_not_for_us(h))
  3002. return IRQ_NONE;
  3003. /*
  3004. * If there are completed commands in the completion queue,
  3005. * we had better do something about it.
  3006. */
  3007. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  3008. while (interrupt_pending(h)) {
  3009. while ((a = get_next_completion(h)) != FIFO_EMPTY) {
  3010. a1 = a;
  3011. if ((a & 0x04)) {
  3012. a2 = (a >> 3);
  3013. if (a2 >= h->nr_cmds) {
  3014. printk(KERN_WARNING
  3015. "cciss: controller cciss%d failed, stopping.\n",
  3016. h->ctlr);
  3017. fail_all_cmds(h->ctlr);
  3018. return IRQ_HANDLED;
  3019. }
  3020. c = h->cmd_pool + a2;
  3021. a = c->busaddr;
  3022. } else {
  3023. struct hlist_node *tmp;
  3024. a &= ~3;
  3025. c = NULL;
  3026. hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
  3027. if (c->busaddr == a)
  3028. break;
  3029. }
  3030. }
  3031. /*
  3032. * If we've found the command, take it off the
  3033. * completion Q and free it
  3034. */
  3035. if (c && c->busaddr == a) {
  3036. removeQ(c);
  3037. if (c->cmd_type == CMD_RWREQ) {
  3038. complete_command(h, c, 0);
  3039. } else if (c->cmd_type == CMD_IOCTL_PEND) {
  3040. complete(c->waiting);
  3041. }
  3042. # ifdef CONFIG_CISS_SCSI_TAPE
  3043. else if (c->cmd_type == CMD_SCSI)
  3044. complete_scsi_command(c, 0, a1);
  3045. # endif
  3046. continue;
  3047. }
  3048. }
  3049. }
  3050. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  3051. return IRQ_HANDLED;
  3052. }
  3053. /**
  3054. * add_to_scan_list() - add controller to rescan queue
  3055. * @h: Pointer to the controller.
  3056. *
  3057. * Adds the controller to the rescan queue if not already on the queue.
  3058. *
  3059. * returns 1 if added to the queue, 0 if skipped (could be on the
  3060. * queue already, or the controller could be initializing or shutting
  3061. * down).
  3062. **/
  3063. static int add_to_scan_list(struct ctlr_info *h)
  3064. {
  3065. struct ctlr_info *test_h;
  3066. int found = 0;
  3067. int ret = 0;
  3068. if (h->busy_initializing)
  3069. return 0;
  3070. if (!mutex_trylock(&h->busy_shutting_down))
  3071. return 0;
  3072. mutex_lock(&scan_mutex);
  3073. list_for_each_entry(test_h, &scan_q, scan_list) {
  3074. if (test_h == h) {
  3075. found = 1;
  3076. break;
  3077. }
  3078. }
  3079. if (!found && !h->busy_scanning) {
  3080. INIT_COMPLETION(h->scan_wait);
  3081. list_add_tail(&h->scan_list, &scan_q);
  3082. ret = 1;
  3083. }
  3084. mutex_unlock(&scan_mutex);
  3085. mutex_unlock(&h->busy_shutting_down);
  3086. return ret;
  3087. }
  3088. /**
  3089. * remove_from_scan_list() - remove controller from rescan queue
  3090. * @h: Pointer to the controller.
  3091. *
  3092. * Removes the controller from the rescan queue if present. Blocks if
  3093. * the controller is currently conducting a rescan.
  3094. **/
  3095. static void remove_from_scan_list(struct ctlr_info *h)
  3096. {
  3097. struct ctlr_info *test_h, *tmp_h;
  3098. int scanning = 0;
  3099. mutex_lock(&scan_mutex);
  3100. list_for_each_entry_safe(test_h, tmp_h, &scan_q, scan_list) {
  3101. if (test_h == h) {
  3102. list_del(&h->scan_list);
  3103. complete_all(&h->scan_wait);
  3104. mutex_unlock(&scan_mutex);
  3105. return;
  3106. }
  3107. }
  3108. if (&h->busy_scanning)
  3109. scanning = 0;
  3110. mutex_unlock(&scan_mutex);
  3111. if (scanning)
  3112. wait_for_completion(&h->scan_wait);
  3113. }
  3114. /**
  3115. * scan_thread() - kernel thread used to rescan controllers
  3116. * @data: Ignored.
  3117. *
  3118. * A kernel thread used scan for drive topology changes on
  3119. * controllers. The thread processes only one controller at a time
  3120. * using a queue. Controllers are added to the queue using
  3121. * add_to_scan_list() and removed from the queue either after done
  3122. * processing or using remove_from_scan_list().
  3123. *
  3124. * returns 0.
  3125. **/
  3126. static int scan_thread(void *data)
  3127. {
  3128. struct ctlr_info *h;
  3129. while (1) {
  3130. set_current_state(TASK_INTERRUPTIBLE);
  3131. schedule();
  3132. if (kthread_should_stop())
  3133. break;
  3134. while (1) {
  3135. mutex_lock(&scan_mutex);
  3136. if (list_empty(&scan_q)) {
  3137. mutex_unlock(&scan_mutex);
  3138. break;
  3139. }
  3140. h = list_entry(scan_q.next,
  3141. struct ctlr_info,
  3142. scan_list);
  3143. list_del(&h->scan_list);
  3144. h->busy_scanning = 1;
  3145. mutex_unlock(&scan_mutex);
  3146. if (h) {
  3147. rebuild_lun_table(h, 0, 0);
  3148. complete_all(&h->scan_wait);
  3149. mutex_lock(&scan_mutex);
  3150. h->busy_scanning = 0;
  3151. mutex_unlock(&scan_mutex);
  3152. }
  3153. }
  3154. }
  3155. return 0;
  3156. }
  3157. static int check_for_unit_attention(ctlr_info_t *h, CommandList_struct *c)
  3158. {
  3159. if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
  3160. return 0;
  3161. switch (c->err_info->SenseInfo[12]) {
  3162. case STATE_CHANGED:
  3163. printk(KERN_WARNING "cciss%d: a state change "
  3164. "detected, command retried\n", h->ctlr);
  3165. return 1;
  3166. break;
  3167. case LUN_FAILED:
  3168. printk(KERN_WARNING "cciss%d: LUN failure "
  3169. "detected, action required\n", h->ctlr);
  3170. return 1;
  3171. break;
  3172. case REPORT_LUNS_CHANGED:
  3173. printk(KERN_WARNING "cciss%d: report LUN data "
  3174. "changed\n", h->ctlr);
  3175. add_to_scan_list(h);
  3176. wake_up_process(cciss_scan_thread);
  3177. return 1;
  3178. break;
  3179. case POWER_OR_RESET:
  3180. printk(KERN_WARNING "cciss%d: a power on "
  3181. "or device reset detected\n", h->ctlr);
  3182. return 1;
  3183. break;
  3184. case UNIT_ATTENTION_CLEARED:
  3185. printk(KERN_WARNING "cciss%d: unit attention "
  3186. "cleared by another initiator\n", h->ctlr);
  3187. return 1;
  3188. break;
  3189. default:
  3190. printk(KERN_WARNING "cciss%d: unknown "
  3191. "unit attention detected\n", h->ctlr);
  3192. return 1;
  3193. }
  3194. }
  3195. /*
  3196. * We cannot read the structure directly, for portability we must use
  3197. * the io functions.
  3198. * This is for debug only.
  3199. */
  3200. #ifdef CCISS_DEBUG
  3201. static void print_cfg_table(CfgTable_struct *tb)
  3202. {
  3203. int i;
  3204. char temp_name[17];
  3205. printk("Controller Configuration information\n");
  3206. printk("------------------------------------\n");
  3207. for (i = 0; i < 4; i++)
  3208. temp_name[i] = readb(&(tb->Signature[i]));
  3209. temp_name[4] = '\0';
  3210. printk(" Signature = %s\n", temp_name);
  3211. printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
  3212. printk(" Transport methods supported = 0x%x\n",
  3213. readl(&(tb->TransportSupport)));
  3214. printk(" Transport methods active = 0x%x\n",
  3215. readl(&(tb->TransportActive)));
  3216. printk(" Requested transport Method = 0x%x\n",
  3217. readl(&(tb->HostWrite.TransportRequest)));
  3218. printk(" Coalesce Interrupt Delay = 0x%x\n",
  3219. readl(&(tb->HostWrite.CoalIntDelay)));
  3220. printk(" Coalesce Interrupt Count = 0x%x\n",
  3221. readl(&(tb->HostWrite.CoalIntCount)));
  3222. printk(" Max outstanding commands = 0x%d\n",
  3223. readl(&(tb->CmdsOutMax)));
  3224. printk(" Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
  3225. for (i = 0; i < 16; i++)
  3226. temp_name[i] = readb(&(tb->ServerName[i]));
  3227. temp_name[16] = '\0';
  3228. printk(" Server Name = %s\n", temp_name);
  3229. printk(" Heartbeat Counter = 0x%x\n\n\n", readl(&(tb->HeartBeat)));
  3230. }
  3231. #endif /* CCISS_DEBUG */
  3232. static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
  3233. {
  3234. int i, offset, mem_type, bar_type;
  3235. if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
  3236. return 0;
  3237. offset = 0;
  3238. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  3239. bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
  3240. if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
  3241. offset += 4;
  3242. else {
  3243. mem_type = pci_resource_flags(pdev, i) &
  3244. PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  3245. switch (mem_type) {
  3246. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  3247. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  3248. offset += 4; /* 32 bit */
  3249. break;
  3250. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  3251. offset += 8;
  3252. break;
  3253. default: /* reserved in PCI 2.2 */
  3254. printk(KERN_WARNING
  3255. "Base address is invalid\n");
  3256. return -1;
  3257. break;
  3258. }
  3259. }
  3260. if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
  3261. return i + 1;
  3262. }
  3263. return -1;
  3264. }
  3265. /* If MSI/MSI-X is supported by the kernel we will try to enable it on
  3266. * controllers that are capable. If not, we use IO-APIC mode.
  3267. */
  3268. static void __devinit cciss_interrupt_mode(ctlr_info_t *c,
  3269. struct pci_dev *pdev, __u32 board_id)
  3270. {
  3271. #ifdef CONFIG_PCI_MSI
  3272. int err;
  3273. struct msix_entry cciss_msix_entries[4] = { {0, 0}, {0, 1},
  3274. {0, 2}, {0, 3}
  3275. };
  3276. /* Some boards advertise MSI but don't really support it */
  3277. if ((board_id == 0x40700E11) ||
  3278. (board_id == 0x40800E11) ||
  3279. (board_id == 0x40820E11) || (board_id == 0x40830E11))
  3280. goto default_int_mode;
  3281. if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
  3282. err = pci_enable_msix(pdev, cciss_msix_entries, 4);
  3283. if (!err) {
  3284. c->intr[0] = cciss_msix_entries[0].vector;
  3285. c->intr[1] = cciss_msix_entries[1].vector;
  3286. c->intr[2] = cciss_msix_entries[2].vector;
  3287. c->intr[3] = cciss_msix_entries[3].vector;
  3288. c->msix_vector = 1;
  3289. return;
  3290. }
  3291. if (err > 0) {
  3292. printk(KERN_WARNING "cciss: only %d MSI-X vectors "
  3293. "available\n", err);
  3294. goto default_int_mode;
  3295. } else {
  3296. printk(KERN_WARNING "cciss: MSI-X init failed %d\n",
  3297. err);
  3298. goto default_int_mode;
  3299. }
  3300. }
  3301. if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
  3302. if (!pci_enable_msi(pdev)) {
  3303. c->msi_vector = 1;
  3304. } else {
  3305. printk(KERN_WARNING "cciss: MSI init failed\n");
  3306. }
  3307. }
  3308. default_int_mode:
  3309. #endif /* CONFIG_PCI_MSI */
  3310. /* if we get here we're going to use the default interrupt mode */
  3311. c->intr[SIMPLE_MODE_INT] = pdev->irq;
  3312. return;
  3313. }
  3314. static int __devinit cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
  3315. {
  3316. ushort subsystem_vendor_id, subsystem_device_id, command;
  3317. __u32 board_id, scratchpad = 0;
  3318. __u64 cfg_offset;
  3319. __u32 cfg_base_addr;
  3320. __u64 cfg_base_addr_index;
  3321. int i, err;
  3322. /* check to see if controller has been disabled */
  3323. /* BEFORE trying to enable it */
  3324. (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
  3325. if (!(command & 0x02)) {
  3326. printk(KERN_WARNING
  3327. "cciss: controller appears to be disabled\n");
  3328. return -ENODEV;
  3329. }
  3330. err = pci_enable_device(pdev);
  3331. if (err) {
  3332. printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
  3333. return err;
  3334. }
  3335. err = pci_request_regions(pdev, "cciss");
  3336. if (err) {
  3337. printk(KERN_ERR "cciss: Cannot obtain PCI resources, "
  3338. "aborting\n");
  3339. return err;
  3340. }
  3341. subsystem_vendor_id = pdev->subsystem_vendor;
  3342. subsystem_device_id = pdev->subsystem_device;
  3343. board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
  3344. subsystem_vendor_id);
  3345. #ifdef CCISS_DEBUG
  3346. printk("command = %x\n", command);
  3347. printk("irq = %x\n", pdev->irq);
  3348. printk("board_id = %x\n", board_id);
  3349. #endif /* CCISS_DEBUG */
  3350. /* If the kernel supports MSI/MSI-X we will try to enable that functionality,
  3351. * else we use the IO-APIC interrupt assigned to us by system ROM.
  3352. */
  3353. cciss_interrupt_mode(c, pdev, board_id);
  3354. /* find the memory BAR */
  3355. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  3356. if (pci_resource_flags(pdev, i) & IORESOURCE_MEM)
  3357. break;
  3358. }
  3359. if (i == DEVICE_COUNT_RESOURCE) {
  3360. printk(KERN_WARNING "cciss: No memory BAR found\n");
  3361. err = -ENODEV;
  3362. goto err_out_free_res;
  3363. }
  3364. c->paddr = pci_resource_start(pdev, i); /* addressing mode bits
  3365. * already removed
  3366. */
  3367. #ifdef CCISS_DEBUG
  3368. printk("address 0 = %lx\n", c->paddr);
  3369. #endif /* CCISS_DEBUG */
  3370. c->vaddr = remap_pci_mem(c->paddr, 0x250);
  3371. /* Wait for the board to become ready. (PCI hotplug needs this.)
  3372. * We poll for up to 120 secs, once per 100ms. */
  3373. for (i = 0; i < 1200; i++) {
  3374. scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
  3375. if (scratchpad == CCISS_FIRMWARE_READY)
  3376. break;
  3377. set_current_state(TASK_INTERRUPTIBLE);
  3378. schedule_timeout(msecs_to_jiffies(100)); /* wait 100ms */
  3379. }
  3380. if (scratchpad != CCISS_FIRMWARE_READY) {
  3381. printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
  3382. err = -ENODEV;
  3383. goto err_out_free_res;
  3384. }
  3385. /* get the address index number */
  3386. cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
  3387. cfg_base_addr &= (__u32) 0x0000ffff;
  3388. #ifdef CCISS_DEBUG
  3389. printk("cfg base address = %x\n", cfg_base_addr);
  3390. #endif /* CCISS_DEBUG */
  3391. cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
  3392. #ifdef CCISS_DEBUG
  3393. printk("cfg base address index = %llx\n",
  3394. (unsigned long long)cfg_base_addr_index);
  3395. #endif /* CCISS_DEBUG */
  3396. if (cfg_base_addr_index == -1) {
  3397. printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
  3398. err = -ENODEV;
  3399. goto err_out_free_res;
  3400. }
  3401. cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
  3402. #ifdef CCISS_DEBUG
  3403. printk("cfg offset = %llx\n", (unsigned long long)cfg_offset);
  3404. #endif /* CCISS_DEBUG */
  3405. c->cfgtable = remap_pci_mem(pci_resource_start(pdev,
  3406. cfg_base_addr_index) +
  3407. cfg_offset, sizeof(CfgTable_struct));
  3408. c->board_id = board_id;
  3409. #ifdef CCISS_DEBUG
  3410. print_cfg_table(c->cfgtable);
  3411. #endif /* CCISS_DEBUG */
  3412. /* Some controllers support Zero Memory Raid (ZMR).
  3413. * When configured in ZMR mode the number of supported
  3414. * commands drops to 64. So instead of just setting an
  3415. * arbitrary value we make the driver a little smarter.
  3416. * We read the config table to tell us how many commands
  3417. * are supported on the controller then subtract 4 to
  3418. * leave a little room for ioctl calls.
  3419. */
  3420. c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
  3421. for (i = 0; i < ARRAY_SIZE(products); i++) {
  3422. if (board_id == products[i].board_id) {
  3423. c->product_name = products[i].product_name;
  3424. c->access = *(products[i].access);
  3425. c->nr_cmds = c->max_commands - 4;
  3426. break;
  3427. }
  3428. }
  3429. if ((readb(&c->cfgtable->Signature[0]) != 'C') ||
  3430. (readb(&c->cfgtable->Signature[1]) != 'I') ||
  3431. (readb(&c->cfgtable->Signature[2]) != 'S') ||
  3432. (readb(&c->cfgtable->Signature[3]) != 'S')) {
  3433. printk("Does not appear to be a valid CISS config table\n");
  3434. err = -ENODEV;
  3435. goto err_out_free_res;
  3436. }
  3437. /* We didn't find the controller in our list. We know the
  3438. * signature is valid. If it's an HP device let's try to
  3439. * bind to the device and fire it up. Otherwise we bail.
  3440. */
  3441. if (i == ARRAY_SIZE(products)) {
  3442. if (subsystem_vendor_id == PCI_VENDOR_ID_HP) {
  3443. c->product_name = products[i-1].product_name;
  3444. c->access = *(products[i-1].access);
  3445. c->nr_cmds = c->max_commands - 4;
  3446. printk(KERN_WARNING "cciss: This is an unknown "
  3447. "Smart Array controller.\n"
  3448. "cciss: Please update to the latest driver "
  3449. "available from www.hp.com.\n");
  3450. } else {
  3451. printk(KERN_WARNING "cciss: Sorry, I don't know how"
  3452. " to access the Smart Array controller %08lx\n"
  3453. , (unsigned long)board_id);
  3454. err = -ENODEV;
  3455. goto err_out_free_res;
  3456. }
  3457. }
  3458. #ifdef CONFIG_X86
  3459. {
  3460. /* Need to enable prefetch in the SCSI core for 6400 in x86 */
  3461. __u32 prefetch;
  3462. prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
  3463. prefetch |= 0x100;
  3464. writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
  3465. }
  3466. #endif
  3467. /* Disabling DMA prefetch and refetch for the P600.
  3468. * An ASIC bug may result in accesses to invalid memory addresses.
  3469. * We've disabled prefetch for some time now. Testing with XEN
  3470. * kernels revealed a bug in the refetch if dom0 resides on a P600.
  3471. */
  3472. if(board_id == 0x3225103C) {
  3473. __u32 dma_prefetch;
  3474. __u32 dma_refetch;
  3475. dma_prefetch = readl(c->vaddr + I2O_DMA1_CFG);
  3476. dma_prefetch |= 0x8000;
  3477. writel(dma_prefetch, c->vaddr + I2O_DMA1_CFG);
  3478. pci_read_config_dword(pdev, PCI_COMMAND_PARITY, &dma_refetch);
  3479. dma_refetch |= 0x1;
  3480. pci_write_config_dword(pdev, PCI_COMMAND_PARITY, dma_refetch);
  3481. }
  3482. #ifdef CCISS_DEBUG
  3483. printk("Trying to put board into Simple mode\n");
  3484. #endif /* CCISS_DEBUG */
  3485. c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
  3486. /* Update the field, and then ring the doorbell */
  3487. writel(CFGTBL_Trans_Simple, &(c->cfgtable->HostWrite.TransportRequest));
  3488. writel(CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
  3489. /* under certain very rare conditions, this can take awhile.
  3490. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  3491. * as we enter this code.) */
  3492. for (i = 0; i < MAX_CONFIG_WAIT; i++) {
  3493. if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
  3494. break;
  3495. /* delay and try again */
  3496. set_current_state(TASK_INTERRUPTIBLE);
  3497. schedule_timeout(msecs_to_jiffies(1));
  3498. }
  3499. #ifdef CCISS_DEBUG
  3500. printk(KERN_DEBUG "I counter got to %d %x\n", i,
  3501. readl(c->vaddr + SA5_DOORBELL));
  3502. #endif /* CCISS_DEBUG */
  3503. #ifdef CCISS_DEBUG
  3504. print_cfg_table(c->cfgtable);
  3505. #endif /* CCISS_DEBUG */
  3506. if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
  3507. printk(KERN_WARNING "cciss: unable to get board into"
  3508. " simple mode\n");
  3509. err = -ENODEV;
  3510. goto err_out_free_res;
  3511. }
  3512. return 0;
  3513. err_out_free_res:
  3514. /*
  3515. * Deliberately omit pci_disable_device(): it does something nasty to
  3516. * Smart Array controllers that pci_enable_device does not undo
  3517. */
  3518. pci_release_regions(pdev);
  3519. return err;
  3520. }
  3521. /* Function to find the first free pointer into our hba[] array
  3522. * Returns -1 if no free entries are left.
  3523. */
  3524. static int alloc_cciss_hba(void)
  3525. {
  3526. int i;
  3527. for (i = 0; i < MAX_CTLR; i++) {
  3528. if (!hba[i]) {
  3529. ctlr_info_t *p;
  3530. p = kzalloc(sizeof(ctlr_info_t), GFP_KERNEL);
  3531. if (!p)
  3532. goto Enomem;
  3533. hba[i] = p;
  3534. return i;
  3535. }
  3536. }
  3537. printk(KERN_WARNING "cciss: This driver supports a maximum"
  3538. " of %d controllers.\n", MAX_CTLR);
  3539. return -1;
  3540. Enomem:
  3541. printk(KERN_ERR "cciss: out of memory.\n");
  3542. return -1;
  3543. }
  3544. static void free_hba(int n)
  3545. {
  3546. ctlr_info_t *h = hba[n];
  3547. int i;
  3548. hba[n] = NULL;
  3549. for (i = 0; i < h->highest_lun + 1; i++)
  3550. if (h->gendisk[i] != NULL)
  3551. put_disk(h->gendisk[i]);
  3552. kfree(h);
  3553. }
  3554. /* Send a message CDB to the firmware. */
  3555. static __devinit int cciss_message(struct pci_dev *pdev, unsigned char opcode, unsigned char type)
  3556. {
  3557. typedef struct {
  3558. CommandListHeader_struct CommandHeader;
  3559. RequestBlock_struct Request;
  3560. ErrDescriptor_struct ErrorDescriptor;
  3561. } Command;
  3562. static const size_t cmd_sz = sizeof(Command) + sizeof(ErrorInfo_struct);
  3563. Command *cmd;
  3564. dma_addr_t paddr64;
  3565. uint32_t paddr32, tag;
  3566. void __iomem *vaddr;
  3567. int i, err;
  3568. vaddr = ioremap_nocache(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  3569. if (vaddr == NULL)
  3570. return -ENOMEM;
  3571. /* The Inbound Post Queue only accepts 32-bit physical addresses for the
  3572. CCISS commands, so they must be allocated from the lower 4GiB of
  3573. memory. */
  3574. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  3575. if (err) {
  3576. iounmap(vaddr);
  3577. return -ENOMEM;
  3578. }
  3579. cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
  3580. if (cmd == NULL) {
  3581. iounmap(vaddr);
  3582. return -ENOMEM;
  3583. }
  3584. /* This must fit, because of the 32-bit consistent DMA mask. Also,
  3585. although there's no guarantee, we assume that the address is at
  3586. least 4-byte aligned (most likely, it's page-aligned). */
  3587. paddr32 = paddr64;
  3588. cmd->CommandHeader.ReplyQueue = 0;
  3589. cmd->CommandHeader.SGList = 0;
  3590. cmd->CommandHeader.SGTotal = 0;
  3591. cmd->CommandHeader.Tag.lower = paddr32;
  3592. cmd->CommandHeader.Tag.upper = 0;
  3593. memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
  3594. cmd->Request.CDBLen = 16;
  3595. cmd->Request.Type.Type = TYPE_MSG;
  3596. cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
  3597. cmd->Request.Type.Direction = XFER_NONE;
  3598. cmd->Request.Timeout = 0; /* Don't time out */
  3599. cmd->Request.CDB[0] = opcode;
  3600. cmd->Request.CDB[1] = type;
  3601. memset(&cmd->Request.CDB[2], 0, 14); /* the rest of the CDB is reserved */
  3602. cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(Command);
  3603. cmd->ErrorDescriptor.Addr.upper = 0;
  3604. cmd->ErrorDescriptor.Len = sizeof(ErrorInfo_struct);
  3605. writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
  3606. for (i = 0; i < 10; i++) {
  3607. tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
  3608. if ((tag & ~3) == paddr32)
  3609. break;
  3610. schedule_timeout_uninterruptible(HZ);
  3611. }
  3612. iounmap(vaddr);
  3613. /* we leak the DMA buffer here ... no choice since the controller could
  3614. still complete the command. */
  3615. if (i == 10) {
  3616. printk(KERN_ERR "cciss: controller message %02x:%02x timed out\n",
  3617. opcode, type);
  3618. return -ETIMEDOUT;
  3619. }
  3620. pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
  3621. if (tag & 2) {
  3622. printk(KERN_ERR "cciss: controller message %02x:%02x failed\n",
  3623. opcode, type);
  3624. return -EIO;
  3625. }
  3626. printk(KERN_INFO "cciss: controller message %02x:%02x succeeded\n",
  3627. opcode, type);
  3628. return 0;
  3629. }
  3630. #define cciss_soft_reset_controller(p) cciss_message(p, 1, 0)
  3631. #define cciss_noop(p) cciss_message(p, 3, 0)
  3632. static __devinit int cciss_reset_msi(struct pci_dev *pdev)
  3633. {
  3634. /* the #defines are stolen from drivers/pci/msi.h. */
  3635. #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
  3636. #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
  3637. int pos;
  3638. u16 control = 0;
  3639. pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
  3640. if (pos) {
  3641. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  3642. if (control & PCI_MSI_FLAGS_ENABLE) {
  3643. printk(KERN_INFO "cciss: resetting MSI\n");
  3644. pci_write_config_word(pdev, msi_control_reg(pos), control & ~PCI_MSI_FLAGS_ENABLE);
  3645. }
  3646. }
  3647. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  3648. if (pos) {
  3649. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  3650. if (control & PCI_MSIX_FLAGS_ENABLE) {
  3651. printk(KERN_INFO "cciss: resetting MSI-X\n");
  3652. pci_write_config_word(pdev, msi_control_reg(pos), control & ~PCI_MSIX_FLAGS_ENABLE);
  3653. }
  3654. }
  3655. return 0;
  3656. }
  3657. /* This does a hard reset of the controller using PCI power management
  3658. * states. */
  3659. static __devinit int cciss_hard_reset_controller(struct pci_dev *pdev)
  3660. {
  3661. u16 pmcsr, saved_config_space[32];
  3662. int i, pos;
  3663. printk(KERN_INFO "cciss: using PCI PM to reset controller\n");
  3664. /* This is very nearly the same thing as
  3665. pci_save_state(pci_dev);
  3666. pci_set_power_state(pci_dev, PCI_D3hot);
  3667. pci_set_power_state(pci_dev, PCI_D0);
  3668. pci_restore_state(pci_dev);
  3669. but we can't use these nice canned kernel routines on
  3670. kexec, because they also check the MSI/MSI-X state in PCI
  3671. configuration space and do the wrong thing when it is
  3672. set/cleared. Also, the pci_save/restore_state functions
  3673. violate the ordering requirements for restoring the
  3674. configuration space from the CCISS document (see the
  3675. comment below). So we roll our own .... */
  3676. for (i = 0; i < 32; i++)
  3677. pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
  3678. pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
  3679. if (pos == 0) {
  3680. printk(KERN_ERR "cciss_reset_controller: PCI PM not supported\n");
  3681. return -ENODEV;
  3682. }
  3683. /* Quoting from the Open CISS Specification: "The Power
  3684. * Management Control/Status Register (CSR) controls the power
  3685. * state of the device. The normal operating state is D0,
  3686. * CSR=00h. The software off state is D3, CSR=03h. To reset
  3687. * the controller, place the interface device in D3 then to
  3688. * D0, this causes a secondary PCI reset which will reset the
  3689. * controller." */
  3690. /* enter the D3hot power management state */
  3691. pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
  3692. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  3693. pmcsr |= PCI_D3hot;
  3694. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  3695. schedule_timeout_uninterruptible(HZ >> 1);
  3696. /* enter the D0 power management state */
  3697. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  3698. pmcsr |= PCI_D0;
  3699. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  3700. schedule_timeout_uninterruptible(HZ >> 1);
  3701. /* Restore the PCI configuration space. The Open CISS
  3702. * Specification says, "Restore the PCI Configuration
  3703. * Registers, offsets 00h through 60h. It is important to
  3704. * restore the command register, 16-bits at offset 04h,
  3705. * last. Do not restore the configuration status register,
  3706. * 16-bits at offset 06h." Note that the offset is 2*i. */
  3707. for (i = 0; i < 32; i++) {
  3708. if (i == 2 || i == 3)
  3709. continue;
  3710. pci_write_config_word(pdev, 2*i, saved_config_space[i]);
  3711. }
  3712. wmb();
  3713. pci_write_config_word(pdev, 4, saved_config_space[2]);
  3714. return 0;
  3715. }
  3716. /*
  3717. * This is it. Find all the controllers and register them. I really hate
  3718. * stealing all these major device numbers.
  3719. * returns the number of block devices registered.
  3720. */
  3721. static int __devinit cciss_init_one(struct pci_dev *pdev,
  3722. const struct pci_device_id *ent)
  3723. {
  3724. int i;
  3725. int j = 0;
  3726. int rc;
  3727. int dac, return_code;
  3728. InquiryData_struct *inq_buff;
  3729. if (reset_devices) {
  3730. /* Reset the controller with a PCI power-cycle */
  3731. if (cciss_hard_reset_controller(pdev) || cciss_reset_msi(pdev))
  3732. return -ENODEV;
  3733. /* Now try to get the controller to respond to a no-op. Some
  3734. devices (notably the HP Smart Array 5i Controller) need
  3735. up to 30 seconds to respond. */
  3736. for (i=0; i<30; i++) {
  3737. if (cciss_noop(pdev) == 0)
  3738. break;
  3739. schedule_timeout_uninterruptible(HZ);
  3740. }
  3741. if (i == 30) {
  3742. printk(KERN_ERR "cciss: controller seems dead\n");
  3743. return -EBUSY;
  3744. }
  3745. }
  3746. i = alloc_cciss_hba();
  3747. if (i < 0)
  3748. return -1;
  3749. hba[i]->busy_initializing = 1;
  3750. INIT_HLIST_HEAD(&hba[i]->cmpQ);
  3751. INIT_HLIST_HEAD(&hba[i]->reqQ);
  3752. mutex_init(&hba[i]->busy_shutting_down);
  3753. if (cciss_pci_init(hba[i], pdev) != 0)
  3754. goto clean0;
  3755. sprintf(hba[i]->devname, "cciss%d", i);
  3756. hba[i]->ctlr = i;
  3757. hba[i]->pdev = pdev;
  3758. init_completion(&hba[i]->scan_wait);
  3759. if (cciss_create_hba_sysfs_entry(hba[i]))
  3760. goto clean0;
  3761. /* configure PCI DMA stuff */
  3762. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  3763. dac = 1;
  3764. else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))
  3765. dac = 0;
  3766. else {
  3767. printk(KERN_ERR "cciss: no suitable DMA available\n");
  3768. goto clean1;
  3769. }
  3770. /*
  3771. * register with the major number, or get a dynamic major number
  3772. * by passing 0 as argument. This is done for greater than
  3773. * 8 controller support.
  3774. */
  3775. if (i < MAX_CTLR_ORIG)
  3776. hba[i]->major = COMPAQ_CISS_MAJOR + i;
  3777. rc = register_blkdev(hba[i]->major, hba[i]->devname);
  3778. if (rc == -EBUSY || rc == -EINVAL) {
  3779. printk(KERN_ERR
  3780. "cciss: Unable to get major number %d for %s "
  3781. "on hba %d\n", hba[i]->major, hba[i]->devname, i);
  3782. goto clean1;
  3783. } else {
  3784. if (i >= MAX_CTLR_ORIG)
  3785. hba[i]->major = rc;
  3786. }
  3787. /* make sure the board interrupts are off */
  3788. hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
  3789. if (request_irq(hba[i]->intr[SIMPLE_MODE_INT], do_cciss_intr,
  3790. IRQF_DISABLED | IRQF_SHARED, hba[i]->devname, hba[i])) {
  3791. printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
  3792. hba[i]->intr[SIMPLE_MODE_INT], hba[i]->devname);
  3793. goto clean2;
  3794. }
  3795. printk(KERN_INFO "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n",
  3796. hba[i]->devname, pdev->device, pci_name(pdev),
  3797. hba[i]->intr[SIMPLE_MODE_INT], dac ? "" : " not");
  3798. hba[i]->cmd_pool_bits =
  3799. kmalloc(DIV_ROUND_UP(hba[i]->nr_cmds, BITS_PER_LONG)
  3800. * sizeof(unsigned long), GFP_KERNEL);
  3801. hba[i]->cmd_pool = (CommandList_struct *)
  3802. pci_alloc_consistent(hba[i]->pdev,
  3803. hba[i]->nr_cmds * sizeof(CommandList_struct),
  3804. &(hba[i]->cmd_pool_dhandle));
  3805. hba[i]->errinfo_pool = (ErrorInfo_struct *)
  3806. pci_alloc_consistent(hba[i]->pdev,
  3807. hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
  3808. &(hba[i]->errinfo_pool_dhandle));
  3809. if ((hba[i]->cmd_pool_bits == NULL)
  3810. || (hba[i]->cmd_pool == NULL)
  3811. || (hba[i]->errinfo_pool == NULL)) {
  3812. printk(KERN_ERR "cciss: out of memory");
  3813. goto clean4;
  3814. }
  3815. spin_lock_init(&hba[i]->lock);
  3816. /* Initialize the pdev driver private data.
  3817. have it point to hba[i]. */
  3818. pci_set_drvdata(pdev, hba[i]);
  3819. /* command and error info recs zeroed out before
  3820. they are used */
  3821. memset(hba[i]->cmd_pool_bits, 0,
  3822. DIV_ROUND_UP(hba[i]->nr_cmds, BITS_PER_LONG)
  3823. * sizeof(unsigned long));
  3824. hba[i]->num_luns = 0;
  3825. hba[i]->highest_lun = -1;
  3826. for (j = 0; j < CISS_MAX_LUN; j++) {
  3827. hba[i]->drv[j].raid_level = -1;
  3828. hba[i]->drv[j].queue = NULL;
  3829. hba[i]->gendisk[j] = NULL;
  3830. }
  3831. cciss_scsi_setup(i);
  3832. /* Turn the interrupts on so we can service requests */
  3833. hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
  3834. /* Get the firmware version */
  3835. inq_buff = kzalloc(sizeof(InquiryData_struct), GFP_KERNEL);
  3836. if (inq_buff == NULL) {
  3837. printk(KERN_ERR "cciss: out of memory\n");
  3838. goto clean4;
  3839. }
  3840. return_code = sendcmd_withirq(CISS_INQUIRY, i, inq_buff,
  3841. sizeof(InquiryData_struct), 0, CTLR_LUNID, TYPE_CMD);
  3842. if (return_code == IO_OK) {
  3843. hba[i]->firm_ver[0] = inq_buff->data_byte[32];
  3844. hba[i]->firm_ver[1] = inq_buff->data_byte[33];
  3845. hba[i]->firm_ver[2] = inq_buff->data_byte[34];
  3846. hba[i]->firm_ver[3] = inq_buff->data_byte[35];
  3847. } else { /* send command failed */
  3848. printk(KERN_WARNING "cciss: unable to determine firmware"
  3849. " version of controller\n");
  3850. }
  3851. kfree(inq_buff);
  3852. cciss_procinit(i);
  3853. hba[i]->cciss_max_sectors = 2048;
  3854. rebuild_lun_table(hba[i], 1, 0);
  3855. hba[i]->busy_initializing = 0;
  3856. return 1;
  3857. clean4:
  3858. kfree(hba[i]->cmd_pool_bits);
  3859. if (hba[i]->cmd_pool)
  3860. pci_free_consistent(hba[i]->pdev,
  3861. hba[i]->nr_cmds * sizeof(CommandList_struct),
  3862. hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
  3863. if (hba[i]->errinfo_pool)
  3864. pci_free_consistent(hba[i]->pdev,
  3865. hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
  3866. hba[i]->errinfo_pool,
  3867. hba[i]->errinfo_pool_dhandle);
  3868. free_irq(hba[i]->intr[SIMPLE_MODE_INT], hba[i]);
  3869. clean2:
  3870. unregister_blkdev(hba[i]->major, hba[i]->devname);
  3871. clean1:
  3872. cciss_destroy_hba_sysfs_entry(hba[i]);
  3873. clean0:
  3874. hba[i]->busy_initializing = 0;
  3875. /* cleanup any queues that may have been initialized */
  3876. for (j=0; j <= hba[i]->highest_lun; j++){
  3877. drive_info_struct *drv = &(hba[i]->drv[j]);
  3878. if (drv->queue)
  3879. blk_cleanup_queue(drv->queue);
  3880. }
  3881. /*
  3882. * Deliberately omit pci_disable_device(): it does something nasty to
  3883. * Smart Array controllers that pci_enable_device does not undo
  3884. */
  3885. pci_release_regions(pdev);
  3886. pci_set_drvdata(pdev, NULL);
  3887. free_hba(i);
  3888. return -1;
  3889. }
  3890. static void cciss_shutdown(struct pci_dev *pdev)
  3891. {
  3892. ctlr_info_t *tmp_ptr;
  3893. int i;
  3894. char flush_buf[4];
  3895. int return_code;
  3896. tmp_ptr = pci_get_drvdata(pdev);
  3897. if (tmp_ptr == NULL)
  3898. return;
  3899. i = tmp_ptr->ctlr;
  3900. if (hba[i] == NULL)
  3901. return;
  3902. /* Turn board interrupts off and send the flush cache command */
  3903. /* sendcmd will turn off interrupt, and send the flush...
  3904. * To write all data in the battery backed cache to disks */
  3905. memset(flush_buf, 0, 4);
  3906. return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0,
  3907. CTLR_LUNID, TYPE_CMD);
  3908. if (return_code == IO_OK) {
  3909. printk(KERN_INFO "Completed flushing cache on controller %d\n", i);
  3910. } else {
  3911. printk(KERN_WARNING "Error flushing cache on controller %d\n", i);
  3912. }
  3913. free_irq(hba[i]->intr[2], hba[i]);
  3914. }
  3915. static void __devexit cciss_remove_one(struct pci_dev *pdev)
  3916. {
  3917. ctlr_info_t *tmp_ptr;
  3918. int i, j;
  3919. if (pci_get_drvdata(pdev) == NULL) {
  3920. printk(KERN_ERR "cciss: Unable to remove device \n");
  3921. return;
  3922. }
  3923. tmp_ptr = pci_get_drvdata(pdev);
  3924. i = tmp_ptr->ctlr;
  3925. if (hba[i] == NULL) {
  3926. printk(KERN_ERR "cciss: device appears to "
  3927. "already be removed \n");
  3928. return;
  3929. }
  3930. mutex_lock(&hba[i]->busy_shutting_down);
  3931. remove_from_scan_list(hba[i]);
  3932. remove_proc_entry(hba[i]->devname, proc_cciss);
  3933. unregister_blkdev(hba[i]->major, hba[i]->devname);
  3934. /* remove it from the disk list */
  3935. for (j = 0; j < CISS_MAX_LUN; j++) {
  3936. struct gendisk *disk = hba[i]->gendisk[j];
  3937. if (disk) {
  3938. struct request_queue *q = disk->queue;
  3939. if (disk->flags & GENHD_FL_UP) {
  3940. cciss_destroy_ld_sysfs_entry(hba[i], j, 1);
  3941. del_gendisk(disk);
  3942. }
  3943. if (q)
  3944. blk_cleanup_queue(q);
  3945. }
  3946. }
  3947. #ifdef CONFIG_CISS_SCSI_TAPE
  3948. cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
  3949. #endif
  3950. cciss_shutdown(pdev);
  3951. #ifdef CONFIG_PCI_MSI
  3952. if (hba[i]->msix_vector)
  3953. pci_disable_msix(hba[i]->pdev);
  3954. else if (hba[i]->msi_vector)
  3955. pci_disable_msi(hba[i]->pdev);
  3956. #endif /* CONFIG_PCI_MSI */
  3957. iounmap(hba[i]->vaddr);
  3958. pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(CommandList_struct),
  3959. hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
  3960. pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
  3961. hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
  3962. kfree(hba[i]->cmd_pool_bits);
  3963. /*
  3964. * Deliberately omit pci_disable_device(): it does something nasty to
  3965. * Smart Array controllers that pci_enable_device does not undo
  3966. */
  3967. pci_release_regions(pdev);
  3968. pci_set_drvdata(pdev, NULL);
  3969. cciss_destroy_hba_sysfs_entry(hba[i]);
  3970. mutex_unlock(&hba[i]->busy_shutting_down);
  3971. free_hba(i);
  3972. }
  3973. static struct pci_driver cciss_pci_driver = {
  3974. .name = "cciss",
  3975. .probe = cciss_init_one,
  3976. .remove = __devexit_p(cciss_remove_one),
  3977. .id_table = cciss_pci_device_id, /* id_table */
  3978. .shutdown = cciss_shutdown,
  3979. };
  3980. /*
  3981. * This is it. Register the PCI driver information for the cards we control
  3982. * the OS will call our registered routines when it finds one of our cards.
  3983. */
  3984. static int __init cciss_init(void)
  3985. {
  3986. int err;
  3987. /*
  3988. * The hardware requires that commands are aligned on a 64-bit
  3989. * boundary. Given that we use pci_alloc_consistent() to allocate an
  3990. * array of them, the size must be a multiple of 8 bytes.
  3991. */
  3992. BUILD_BUG_ON(sizeof(CommandList_struct) % 8);
  3993. printk(KERN_INFO DRIVER_NAME "\n");
  3994. err = bus_register(&cciss_bus_type);
  3995. if (err)
  3996. return err;
  3997. /* Start the scan thread */
  3998. cciss_scan_thread = kthread_run(scan_thread, NULL, "cciss_scan");
  3999. if (IS_ERR(cciss_scan_thread)) {
  4000. err = PTR_ERR(cciss_scan_thread);
  4001. goto err_bus_unregister;
  4002. }
  4003. /* Register for our PCI devices */
  4004. err = pci_register_driver(&cciss_pci_driver);
  4005. if (err)
  4006. goto err_thread_stop;
  4007. return err;
  4008. err_thread_stop:
  4009. kthread_stop(cciss_scan_thread);
  4010. err_bus_unregister:
  4011. bus_unregister(&cciss_bus_type);
  4012. return err;
  4013. }
  4014. static void __exit cciss_cleanup(void)
  4015. {
  4016. int i;
  4017. pci_unregister_driver(&cciss_pci_driver);
  4018. /* double check that all controller entrys have been removed */
  4019. for (i = 0; i < MAX_CTLR; i++) {
  4020. if (hba[i] != NULL) {
  4021. printk(KERN_WARNING "cciss: had to remove"
  4022. " controller %d\n", i);
  4023. cciss_remove_one(hba[i]->pdev);
  4024. }
  4025. }
  4026. kthread_stop(cciss_scan_thread);
  4027. remove_proc_entry("driver/cciss", NULL);
  4028. bus_unregister(&cciss_bus_type);
  4029. }
  4030. static void fail_all_cmds(unsigned long ctlr)
  4031. {
  4032. /* If we get here, the board is apparently dead. */
  4033. ctlr_info_t *h = hba[ctlr];
  4034. CommandList_struct *c;
  4035. unsigned long flags;
  4036. printk(KERN_WARNING "cciss%d: controller not responding.\n", h->ctlr);
  4037. h->alive = 0; /* the controller apparently died... */
  4038. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  4039. pci_disable_device(h->pdev); /* Make sure it is really dead. */
  4040. /* move everything off the request queue onto the completed queue */
  4041. while (!hlist_empty(&h->reqQ)) {
  4042. c = hlist_entry(h->reqQ.first, CommandList_struct, list);
  4043. removeQ(c);
  4044. h->Qdepth--;
  4045. addQ(&h->cmpQ, c);
  4046. }
  4047. /* Now, fail everything on the completed queue with a HW error */
  4048. while (!hlist_empty(&h->cmpQ)) {
  4049. c = hlist_entry(h->cmpQ.first, CommandList_struct, list);
  4050. removeQ(c);
  4051. if (c->cmd_type != CMD_MSG_STALE)
  4052. c->err_info->CommandStatus = CMD_HARDWARE_ERR;
  4053. if (c->cmd_type == CMD_RWREQ) {
  4054. complete_command(h, c, 0);
  4055. } else if (c->cmd_type == CMD_IOCTL_PEND)
  4056. complete(c->waiting);
  4057. #ifdef CONFIG_CISS_SCSI_TAPE
  4058. else if (c->cmd_type == CMD_SCSI)
  4059. complete_scsi_command(c, 0, 0);
  4060. #endif
  4061. }
  4062. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  4063. return;
  4064. }
  4065. module_init(cciss_init);
  4066. module_exit(cciss_cleanup);