cciss.c 136 KB

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