cciss.c 149 KB

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