cciss.c 135 KB

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