cciss.c 129 KB

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