mpt2sas_scsih.c 163 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804
  1. /*
  2. * Scsi Host Layer for MPT (Message Passing Technology) based controllers
  3. *
  4. * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
  5. * Copyright (C) 2007-2008 LSI Corporation
  6. * (mailto:DL-MPTFusionLinux@lsi.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * NO WARRANTY
  19. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  20. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  21. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  22. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  23. * solely responsible for determining the appropriateness of using and
  24. * distributing the Program and assumes all risks associated with its
  25. * exercise of rights under this Agreement, including but not limited to
  26. * the risks and costs of program errors, damage to or loss of data,
  27. * programs or equipment, and unavailability or interruption of operations.
  28. * DISCLAIMER OF LIABILITY
  29. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  30. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  32. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  33. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  34. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  35. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program; if not, write to the Free Software
  38. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  39. * USA.
  40. */
  41. #include <linux/version.h>
  42. #include <linux/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/init.h>
  45. #include <linux/errno.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/sched.h>
  48. #include <linux/workqueue.h>
  49. #include <linux/delay.h>
  50. #include <linux/pci.h>
  51. #include <linux/interrupt.h>
  52. #include "mpt2sas_base.h"
  53. MODULE_AUTHOR(MPT2SAS_AUTHOR);
  54. MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
  55. MODULE_LICENSE("GPL");
  56. MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
  57. #define RAID_CHANNEL 1
  58. /* forward proto's */
  59. static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
  60. struct _sas_node *sas_expander);
  61. static void _firmware_event_work(struct work_struct *work);
  62. /* global parameters */
  63. LIST_HEAD(mpt2sas_ioc_list);
  64. /* local parameters */
  65. static u8 scsi_io_cb_idx = -1;
  66. static u8 tm_cb_idx = -1;
  67. static u8 ctl_cb_idx = -1;
  68. static u8 base_cb_idx = -1;
  69. static u8 transport_cb_idx = -1;
  70. static u8 config_cb_idx = -1;
  71. static int mpt_ids;
  72. /* command line options */
  73. static u32 logging_level;
  74. MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
  75. "(default=0)");
  76. /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
  77. #define MPT2SAS_MAX_LUN (16895)
  78. static int max_lun = MPT2SAS_MAX_LUN;
  79. module_param(max_lun, int, 0);
  80. MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
  81. /**
  82. * struct sense_info - common structure for obtaining sense keys
  83. * @skey: sense key
  84. * @asc: additional sense code
  85. * @ascq: additional sense code qualifier
  86. */
  87. struct sense_info {
  88. u8 skey;
  89. u8 asc;
  90. u8 ascq;
  91. };
  92. #define MPT2SAS_RESCAN_AFTER_HOST_RESET (0xFFFF)
  93. /**
  94. * struct fw_event_work - firmware event struct
  95. * @list: link list framework
  96. * @work: work object (ioc->fault_reset_work_q)
  97. * @ioc: per adapter object
  98. * @VF_ID: virtual function id
  99. * @host_reset_handling: handling events during host reset
  100. * @ignore: flag meaning this event has been marked to ignore
  101. * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
  102. * @event_data: reply event data payload follows
  103. *
  104. * This object stored on ioc->fw_event_list.
  105. */
  106. struct fw_event_work {
  107. struct list_head list;
  108. struct work_struct work;
  109. struct MPT2SAS_ADAPTER *ioc;
  110. u8 VF_ID;
  111. u8 host_reset_handling;
  112. u8 ignore;
  113. u16 event;
  114. void *event_data;
  115. };
  116. /**
  117. * struct _scsi_io_transfer - scsi io transfer
  118. * @handle: sas device handle (assigned by firmware)
  119. * @is_raid: flag set for hidden raid components
  120. * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
  121. * @data_length: data transfer length
  122. * @data_dma: dma pointer to data
  123. * @sense: sense data
  124. * @lun: lun number
  125. * @cdb_length: cdb length
  126. * @cdb: cdb contents
  127. * @valid_reply: flag set for reply message
  128. * @timeout: timeout for this command
  129. * @sense_length: sense length
  130. * @ioc_status: ioc status
  131. * @scsi_state: scsi state
  132. * @scsi_status: scsi staus
  133. * @log_info: log information
  134. * @transfer_length: data length transfer when there is a reply message
  135. *
  136. * Used for sending internal scsi commands to devices within this module.
  137. * Refer to _scsi_send_scsi_io().
  138. */
  139. struct _scsi_io_transfer {
  140. u16 handle;
  141. u8 is_raid;
  142. enum dma_data_direction dir;
  143. u32 data_length;
  144. dma_addr_t data_dma;
  145. u8 sense[SCSI_SENSE_BUFFERSIZE];
  146. u32 lun;
  147. u8 cdb_length;
  148. u8 cdb[32];
  149. u8 timeout;
  150. u8 valid_reply;
  151. /* the following bits are only valid when 'valid_reply = 1' */
  152. u32 sense_length;
  153. u16 ioc_status;
  154. u8 scsi_state;
  155. u8 scsi_status;
  156. u32 log_info;
  157. u32 transfer_length;
  158. };
  159. /*
  160. * The pci device ids are defined in mpi/mpi2_cnfg.h.
  161. */
  162. static struct pci_device_id scsih_pci_table[] = {
  163. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
  164. PCI_ANY_ID, PCI_ANY_ID },
  165. /* Falcon ~ 2008*/
  166. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
  167. PCI_ANY_ID, PCI_ANY_ID },
  168. /* Liberator ~ 2108 */
  169. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
  170. PCI_ANY_ID, PCI_ANY_ID },
  171. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
  172. PCI_ANY_ID, PCI_ANY_ID },
  173. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
  174. PCI_ANY_ID, PCI_ANY_ID },
  175. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
  176. PCI_ANY_ID, PCI_ANY_ID },
  177. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
  178. PCI_ANY_ID, PCI_ANY_ID },
  179. {0} /* Terminating entry */
  180. };
  181. MODULE_DEVICE_TABLE(pci, scsih_pci_table);
  182. /**
  183. * scsih_set_debug_level - global setting of ioc->logging_level.
  184. *
  185. * Note: The logging levels are defined in mpt2sas_debug.h.
  186. */
  187. static int
  188. scsih_set_debug_level(const char *val, struct kernel_param *kp)
  189. {
  190. int ret = param_set_int(val, kp);
  191. struct MPT2SAS_ADAPTER *ioc;
  192. if (ret)
  193. return ret;
  194. printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
  195. list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
  196. ioc->logging_level = logging_level;
  197. return 0;
  198. }
  199. module_param_call(logging_level, scsih_set_debug_level, param_get_int,
  200. &logging_level, 0644);
  201. /**
  202. * _scsih_srch_boot_sas_address - search based on sas_address
  203. * @sas_address: sas address
  204. * @boot_device: boot device object from bios page 2
  205. *
  206. * Returns 1 when there's a match, 0 means no match.
  207. */
  208. static inline int
  209. _scsih_srch_boot_sas_address(u64 sas_address,
  210. Mpi2BootDeviceSasWwid_t *boot_device)
  211. {
  212. return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
  213. }
  214. /**
  215. * _scsih_srch_boot_device_name - search based on device name
  216. * @device_name: device name specified in INDENTIFY fram
  217. * @boot_device: boot device object from bios page 2
  218. *
  219. * Returns 1 when there's a match, 0 means no match.
  220. */
  221. static inline int
  222. _scsih_srch_boot_device_name(u64 device_name,
  223. Mpi2BootDeviceDeviceName_t *boot_device)
  224. {
  225. return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
  226. }
  227. /**
  228. * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
  229. * @enclosure_logical_id: enclosure logical id
  230. * @slot_number: slot number
  231. * @boot_device: boot device object from bios page 2
  232. *
  233. * Returns 1 when there's a match, 0 means no match.
  234. */
  235. static inline int
  236. _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
  237. Mpi2BootDeviceEnclosureSlot_t *boot_device)
  238. {
  239. return (enclosure_logical_id == le64_to_cpu(boot_device->
  240. EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
  241. SlotNumber)) ? 1 : 0;
  242. }
  243. /**
  244. * _scsih_is_boot_device - search for matching boot device.
  245. * @sas_address: sas address
  246. * @device_name: device name specified in INDENTIFY fram
  247. * @enclosure_logical_id: enclosure logical id
  248. * @slot_number: slot number
  249. * @form: specifies boot device form
  250. * @boot_device: boot device object from bios page 2
  251. *
  252. * Returns 1 when there's a match, 0 means no match.
  253. */
  254. static int
  255. _scsih_is_boot_device(u64 sas_address, u64 device_name,
  256. u64 enclosure_logical_id, u16 slot, u8 form,
  257. Mpi2BiosPage2BootDevice_t *boot_device)
  258. {
  259. int rc = 0;
  260. switch (form) {
  261. case MPI2_BIOSPAGE2_FORM_SAS_WWID:
  262. if (!sas_address)
  263. break;
  264. rc = _scsih_srch_boot_sas_address(
  265. sas_address, &boot_device->SasWwid);
  266. break;
  267. case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
  268. if (!enclosure_logical_id)
  269. break;
  270. rc = _scsih_srch_boot_encl_slot(
  271. enclosure_logical_id,
  272. slot, &boot_device->EnclosureSlot);
  273. break;
  274. case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
  275. if (!device_name)
  276. break;
  277. rc = _scsih_srch_boot_device_name(
  278. device_name, &boot_device->DeviceName);
  279. break;
  280. case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
  281. break;
  282. }
  283. return rc;
  284. }
  285. /**
  286. * _scsih_determine_boot_device - determine boot device.
  287. * @ioc: per adapter object
  288. * @device: either sas_device or raid_device object
  289. * @is_raid: [flag] 1 = raid object, 0 = sas object
  290. *
  291. * Determines whether this device should be first reported device to
  292. * to scsi-ml or sas transport, this purpose is for persistant boot device.
  293. * There are primary, alternate, and current entries in bios page 2. The order
  294. * priority is primary, alternate, then current. This routine saves
  295. * the corresponding device object and is_raid flag in the ioc object.
  296. * The saved data to be used later in _scsih_probe_boot_devices().
  297. */
  298. static void
  299. _scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
  300. void *device, u8 is_raid)
  301. {
  302. struct _sas_device *sas_device;
  303. struct _raid_device *raid_device;
  304. u64 sas_address;
  305. u64 device_name;
  306. u64 enclosure_logical_id;
  307. u16 slot;
  308. /* only process this function when driver loads */
  309. if (!ioc->wait_for_port_enable_to_complete)
  310. return;
  311. if (!is_raid) {
  312. sas_device = device;
  313. sas_address = sas_device->sas_address;
  314. device_name = sas_device->device_name;
  315. enclosure_logical_id = sas_device->enclosure_logical_id;
  316. slot = sas_device->slot;
  317. } else {
  318. raid_device = device;
  319. sas_address = raid_device->wwid;
  320. device_name = 0;
  321. enclosure_logical_id = 0;
  322. slot = 0;
  323. }
  324. if (!ioc->req_boot_device.device) {
  325. if (_scsih_is_boot_device(sas_address, device_name,
  326. enclosure_logical_id, slot,
  327. (ioc->bios_pg2.ReqBootDeviceForm &
  328. MPI2_BIOSPAGE2_FORM_MASK),
  329. &ioc->bios_pg2.RequestedBootDevice)) {
  330. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  331. "%s: req_boot_device(0x%016llx)\n",
  332. ioc->name, __func__,
  333. (unsigned long long)sas_address));
  334. ioc->req_boot_device.device = device;
  335. ioc->req_boot_device.is_raid = is_raid;
  336. }
  337. }
  338. if (!ioc->req_alt_boot_device.device) {
  339. if (_scsih_is_boot_device(sas_address, device_name,
  340. enclosure_logical_id, slot,
  341. (ioc->bios_pg2.ReqAltBootDeviceForm &
  342. MPI2_BIOSPAGE2_FORM_MASK),
  343. &ioc->bios_pg2.RequestedAltBootDevice)) {
  344. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  345. "%s: req_alt_boot_device(0x%016llx)\n",
  346. ioc->name, __func__,
  347. (unsigned long long)sas_address));
  348. ioc->req_alt_boot_device.device = device;
  349. ioc->req_alt_boot_device.is_raid = is_raid;
  350. }
  351. }
  352. if (!ioc->current_boot_device.device) {
  353. if (_scsih_is_boot_device(sas_address, device_name,
  354. enclosure_logical_id, slot,
  355. (ioc->bios_pg2.CurrentBootDeviceForm &
  356. MPI2_BIOSPAGE2_FORM_MASK),
  357. &ioc->bios_pg2.CurrentBootDevice)) {
  358. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  359. "%s: current_boot_device(0x%016llx)\n",
  360. ioc->name, __func__,
  361. (unsigned long long)sas_address));
  362. ioc->current_boot_device.device = device;
  363. ioc->current_boot_device.is_raid = is_raid;
  364. }
  365. }
  366. }
  367. /**
  368. * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
  369. * @ioc: per adapter object
  370. * @sas_address: sas address
  371. * Context: Calling function should acquire ioc->sas_device_lock
  372. *
  373. * This searches for sas_device based on sas_address, then return sas_device
  374. * object.
  375. */
  376. struct _sas_device *
  377. mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
  378. u64 sas_address)
  379. {
  380. struct _sas_device *sas_device, *r;
  381. r = NULL;
  382. /* check the sas_device_init_list */
  383. list_for_each_entry(sas_device, &ioc->sas_device_init_list,
  384. list) {
  385. if (sas_device->sas_address != sas_address)
  386. continue;
  387. r = sas_device;
  388. goto out;
  389. }
  390. /* then check the sas_device_list */
  391. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  392. if (sas_device->sas_address != sas_address)
  393. continue;
  394. r = sas_device;
  395. goto out;
  396. }
  397. out:
  398. return r;
  399. }
  400. /**
  401. * _scsih_sas_device_find_by_handle - sas device search
  402. * @ioc: per adapter object
  403. * @handle: sas device handle (assigned by firmware)
  404. * Context: Calling function should acquire ioc->sas_device_lock
  405. *
  406. * This searches for sas_device based on sas_address, then return sas_device
  407. * object.
  408. */
  409. static struct _sas_device *
  410. _scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  411. {
  412. struct _sas_device *sas_device, *r;
  413. r = NULL;
  414. if (ioc->wait_for_port_enable_to_complete) {
  415. list_for_each_entry(sas_device, &ioc->sas_device_init_list,
  416. list) {
  417. if (sas_device->handle != handle)
  418. continue;
  419. r = sas_device;
  420. goto out;
  421. }
  422. } else {
  423. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  424. if (sas_device->handle != handle)
  425. continue;
  426. r = sas_device;
  427. goto out;
  428. }
  429. }
  430. out:
  431. return r;
  432. }
  433. /**
  434. * _scsih_sas_device_remove - remove sas_device from list.
  435. * @ioc: per adapter object
  436. * @sas_device: the sas_device object
  437. * Context: This function will acquire ioc->sas_device_lock.
  438. *
  439. * Removing object and freeing associated memory from the ioc->sas_device_list.
  440. */
  441. static void
  442. _scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
  443. struct _sas_device *sas_device)
  444. {
  445. unsigned long flags;
  446. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  447. list_del(&sas_device->list);
  448. memset(sas_device, 0, sizeof(struct _sas_device));
  449. kfree(sas_device);
  450. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  451. }
  452. /**
  453. * _scsih_sas_device_add - insert sas_device to the list.
  454. * @ioc: per adapter object
  455. * @sas_device: the sas_device object
  456. * Context: This function will acquire ioc->sas_device_lock.
  457. *
  458. * Adding new object to the ioc->sas_device_list.
  459. */
  460. static void
  461. _scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
  462. struct _sas_device *sas_device)
  463. {
  464. unsigned long flags;
  465. u16 handle, parent_handle;
  466. u64 sas_address;
  467. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
  468. "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
  469. sas_device->handle, (unsigned long long)sas_device->sas_address));
  470. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  471. list_add_tail(&sas_device->list, &ioc->sas_device_list);
  472. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  473. handle = sas_device->handle;
  474. parent_handle = sas_device->parent_handle;
  475. sas_address = sas_device->sas_address;
  476. if (!mpt2sas_transport_port_add(ioc, handle, parent_handle))
  477. _scsih_sas_device_remove(ioc, sas_device);
  478. }
  479. /**
  480. * _scsih_sas_device_init_add - insert sas_device to the list.
  481. * @ioc: per adapter object
  482. * @sas_device: the sas_device object
  483. * Context: This function will acquire ioc->sas_device_lock.
  484. *
  485. * Adding new object at driver load time to the ioc->sas_device_init_list.
  486. */
  487. static void
  488. _scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
  489. struct _sas_device *sas_device)
  490. {
  491. unsigned long flags;
  492. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
  493. "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
  494. sas_device->handle, (unsigned long long)sas_device->sas_address));
  495. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  496. list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
  497. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  498. _scsih_determine_boot_device(ioc, sas_device, 0);
  499. }
  500. /**
  501. * mpt2sas_scsih_expander_find_by_handle - expander device search
  502. * @ioc: per adapter object
  503. * @handle: expander handle (assigned by firmware)
  504. * Context: Calling function should acquire ioc->sas_device_lock
  505. *
  506. * This searches for expander device based on handle, then returns the
  507. * sas_node object.
  508. */
  509. struct _sas_node *
  510. mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  511. {
  512. struct _sas_node *sas_expander, *r;
  513. r = NULL;
  514. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  515. if (sas_expander->handle != handle)
  516. continue;
  517. r = sas_expander;
  518. goto out;
  519. }
  520. out:
  521. return r;
  522. }
  523. /**
  524. * _scsih_raid_device_find_by_id - raid device search
  525. * @ioc: per adapter object
  526. * @id: sas device target id
  527. * @channel: sas device channel
  528. * Context: Calling function should acquire ioc->raid_device_lock
  529. *
  530. * This searches for raid_device based on target id, then return raid_device
  531. * object.
  532. */
  533. static struct _raid_device *
  534. _scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
  535. {
  536. struct _raid_device *raid_device, *r;
  537. r = NULL;
  538. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  539. if (raid_device->id == id && raid_device->channel == channel) {
  540. r = raid_device;
  541. goto out;
  542. }
  543. }
  544. out:
  545. return r;
  546. }
  547. /**
  548. * _scsih_raid_device_find_by_handle - raid device search
  549. * @ioc: per adapter object
  550. * @handle: sas device handle (assigned by firmware)
  551. * Context: Calling function should acquire ioc->raid_device_lock
  552. *
  553. * This searches for raid_device based on handle, then return raid_device
  554. * object.
  555. */
  556. static struct _raid_device *
  557. _scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  558. {
  559. struct _raid_device *raid_device, *r;
  560. r = NULL;
  561. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  562. if (raid_device->handle != handle)
  563. continue;
  564. r = raid_device;
  565. goto out;
  566. }
  567. out:
  568. return r;
  569. }
  570. /**
  571. * _scsih_raid_device_find_by_wwid - raid device search
  572. * @ioc: per adapter object
  573. * @handle: sas device handle (assigned by firmware)
  574. * Context: Calling function should acquire ioc->raid_device_lock
  575. *
  576. * This searches for raid_device based on wwid, then return raid_device
  577. * object.
  578. */
  579. static struct _raid_device *
  580. _scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
  581. {
  582. struct _raid_device *raid_device, *r;
  583. r = NULL;
  584. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  585. if (raid_device->wwid != wwid)
  586. continue;
  587. r = raid_device;
  588. goto out;
  589. }
  590. out:
  591. return r;
  592. }
  593. /**
  594. * _scsih_raid_device_add - add raid_device object
  595. * @ioc: per adapter object
  596. * @raid_device: raid_device object
  597. *
  598. * This is added to the raid_device_list link list.
  599. */
  600. static void
  601. _scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
  602. struct _raid_device *raid_device)
  603. {
  604. unsigned long flags;
  605. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
  606. "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
  607. raid_device->handle, (unsigned long long)raid_device->wwid));
  608. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  609. list_add_tail(&raid_device->list, &ioc->raid_device_list);
  610. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  611. }
  612. /**
  613. * _scsih_raid_device_remove - delete raid_device object
  614. * @ioc: per adapter object
  615. * @raid_device: raid_device object
  616. *
  617. * This is removed from the raid_device_list link list.
  618. */
  619. static void
  620. _scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
  621. struct _raid_device *raid_device)
  622. {
  623. unsigned long flags;
  624. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  625. list_del(&raid_device->list);
  626. memset(raid_device, 0, sizeof(struct _raid_device));
  627. kfree(raid_device);
  628. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  629. }
  630. /**
  631. * mpt2sas_scsih_expander_find_by_sas_address - expander device search
  632. * @ioc: per adapter object
  633. * @sas_address: sas address
  634. * Context: Calling function should acquire ioc->sas_node_lock.
  635. *
  636. * This searches for expander device based on sas_address, then returns the
  637. * sas_node object.
  638. */
  639. struct _sas_node *
  640. mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
  641. u64 sas_address)
  642. {
  643. struct _sas_node *sas_expander, *r;
  644. r = NULL;
  645. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  646. if (sas_expander->sas_address != sas_address)
  647. continue;
  648. r = sas_expander;
  649. goto out;
  650. }
  651. out:
  652. return r;
  653. }
  654. /**
  655. * _scsih_expander_node_add - insert expander device to the list.
  656. * @ioc: per adapter object
  657. * @sas_expander: the sas_device object
  658. * Context: This function will acquire ioc->sas_node_lock.
  659. *
  660. * Adding new object to the ioc->sas_expander_list.
  661. *
  662. * Return nothing.
  663. */
  664. static void
  665. _scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
  666. struct _sas_node *sas_expander)
  667. {
  668. unsigned long flags;
  669. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  670. list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
  671. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  672. }
  673. /**
  674. * _scsih_is_end_device - determines if device is an end device
  675. * @device_info: bitfield providing information about the device.
  676. * Context: none
  677. *
  678. * Returns 1 if end device.
  679. */
  680. static int
  681. _scsih_is_end_device(u32 device_info)
  682. {
  683. if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
  684. ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
  685. (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
  686. (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
  687. return 1;
  688. else
  689. return 0;
  690. }
  691. /**
  692. * _scsih_scsi_lookup_get - returns scmd entry
  693. * @ioc: per adapter object
  694. * @smid: system request message index
  695. * Context: This function will acquire ioc->scsi_lookup_lock.
  696. *
  697. * Returns the smid stored scmd pointer.
  698. */
  699. static struct scsi_cmnd *
  700. _scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  701. {
  702. unsigned long flags;
  703. struct scsi_cmnd *scmd;
  704. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  705. scmd = ioc->scsi_lookup[smid - 1].scmd;
  706. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  707. return scmd;
  708. }
  709. /**
  710. * mptscsih_getclear_scsi_lookup - returns scmd entry
  711. * @ioc: per adapter object
  712. * @smid: system request message index
  713. * Context: This function will acquire ioc->scsi_lookup_lock.
  714. *
  715. * Returns the smid stored scmd pointer, as well as clearing the scmd pointer.
  716. */
  717. static struct scsi_cmnd *
  718. _scsih_scsi_lookup_getclear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  719. {
  720. unsigned long flags;
  721. struct scsi_cmnd *scmd;
  722. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  723. scmd = ioc->scsi_lookup[smid - 1].scmd;
  724. ioc->scsi_lookup[smid - 1].scmd = NULL;
  725. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  726. return scmd;
  727. }
  728. /**
  729. * _scsih_scsi_lookup_set - updates scmd entry in lookup
  730. * @ioc: per adapter object
  731. * @smid: system request message index
  732. * @scmd: pointer to scsi command object
  733. * Context: This function will acquire ioc->scsi_lookup_lock.
  734. *
  735. * This will save scmd pointer in the scsi_lookup array.
  736. *
  737. * Return nothing.
  738. */
  739. static void
  740. _scsih_scsi_lookup_set(struct MPT2SAS_ADAPTER *ioc, u16 smid,
  741. struct scsi_cmnd *scmd)
  742. {
  743. unsigned long flags;
  744. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  745. ioc->scsi_lookup[smid - 1].scmd = scmd;
  746. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  747. }
  748. /**
  749. * _scsih_scsi_lookup_find_by_scmd - scmd lookup
  750. * @ioc: per adapter object
  751. * @smid: system request message index
  752. * @scmd: pointer to scsi command object
  753. * Context: This function will acquire ioc->scsi_lookup_lock.
  754. *
  755. * This will search for a scmd pointer in the scsi_lookup array,
  756. * returning the revelent smid. A returned value of zero means invalid.
  757. */
  758. static u16
  759. _scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
  760. *scmd)
  761. {
  762. u16 smid;
  763. unsigned long flags;
  764. int i;
  765. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  766. smid = 0;
  767. for (i = 0; i < ioc->request_depth; i++) {
  768. if (ioc->scsi_lookup[i].scmd == scmd) {
  769. smid = i + 1;
  770. goto out;
  771. }
  772. }
  773. out:
  774. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  775. return smid;
  776. }
  777. /**
  778. * _scsih_scsi_lookup_find_by_target - search for matching channel:id
  779. * @ioc: per adapter object
  780. * @id: target id
  781. * @channel: channel
  782. * Context: This function will acquire ioc->scsi_lookup_lock.
  783. *
  784. * This will search for a matching channel:id in the scsi_lookup array,
  785. * returning 1 if found.
  786. */
  787. static u8
  788. _scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
  789. int channel)
  790. {
  791. u8 found;
  792. unsigned long flags;
  793. int i;
  794. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  795. found = 0;
  796. for (i = 0 ; i < ioc->request_depth; i++) {
  797. if (ioc->scsi_lookup[i].scmd &&
  798. (ioc->scsi_lookup[i].scmd->device->id == id &&
  799. ioc->scsi_lookup[i].scmd->device->channel == channel)) {
  800. found = 1;
  801. goto out;
  802. }
  803. }
  804. out:
  805. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  806. return found;
  807. }
  808. /**
  809. * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
  810. * @ioc: per adapter object
  811. * @smid: system request message index
  812. *
  813. * Returns phys pointer to chain buffer.
  814. */
  815. static dma_addr_t
  816. _scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  817. {
  818. return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
  819. ioc->chains_needed_per_io));
  820. }
  821. /**
  822. * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
  823. * @ioc: per adapter object
  824. * @smid: system request message index
  825. *
  826. * Returns virt pointer to chain buffer.
  827. */
  828. static void *
  829. _scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  830. {
  831. return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
  832. ioc->chains_needed_per_io)));
  833. }
  834. /**
  835. * _scsih_build_scatter_gather - main sg creation routine
  836. * @ioc: per adapter object
  837. * @scmd: scsi command
  838. * @smid: system request message index
  839. * Context: none.
  840. *
  841. * The main routine that builds scatter gather table from a given
  842. * scsi request sent via the .queuecommand main handler.
  843. *
  844. * Returns 0 success, anything else error
  845. */
  846. static int
  847. _scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
  848. struct scsi_cmnd *scmd, u16 smid)
  849. {
  850. Mpi2SCSIIORequest_t *mpi_request;
  851. dma_addr_t chain_dma;
  852. struct scatterlist *sg_scmd;
  853. void *sg_local, *chain;
  854. u32 chain_offset;
  855. u32 chain_length;
  856. u32 chain_flags;
  857. u32 sges_left;
  858. u32 sges_in_segment;
  859. u32 sgl_flags;
  860. u32 sgl_flags_last_element;
  861. u32 sgl_flags_end_buffer;
  862. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  863. /* init scatter gather flags */
  864. sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
  865. if (scmd->sc_data_direction == DMA_TO_DEVICE)
  866. sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
  867. sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
  868. << MPI2_SGE_FLAGS_SHIFT;
  869. sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
  870. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
  871. << MPI2_SGE_FLAGS_SHIFT;
  872. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  873. sg_scmd = scsi_sglist(scmd);
  874. sges_left = scsi_dma_map(scmd);
  875. if (!sges_left) {
  876. sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
  877. " failed: request for %d bytes!\n", scsi_bufflen(scmd));
  878. return -ENOMEM;
  879. }
  880. sg_local = &mpi_request->SGL;
  881. sges_in_segment = ioc->max_sges_in_main_message;
  882. if (sges_left <= sges_in_segment)
  883. goto fill_in_last_segment;
  884. mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
  885. (sges_in_segment * ioc->sge_size))/4;
  886. /* fill in main message segment when there is a chain following */
  887. while (sges_in_segment) {
  888. if (sges_in_segment == 1)
  889. ioc->base_add_sg_single(sg_local,
  890. sgl_flags_last_element | sg_dma_len(sg_scmd),
  891. sg_dma_address(sg_scmd));
  892. else
  893. ioc->base_add_sg_single(sg_local, sgl_flags |
  894. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  895. sg_scmd = sg_next(sg_scmd);
  896. sg_local += ioc->sge_size;
  897. sges_left--;
  898. sges_in_segment--;
  899. }
  900. /* initializing the chain flags and pointers */
  901. chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
  902. chain = _scsih_get_chain_buffer(ioc, smid);
  903. chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
  904. do {
  905. sges_in_segment = (sges_left <=
  906. ioc->max_sges_in_chain_message) ? sges_left :
  907. ioc->max_sges_in_chain_message;
  908. chain_offset = (sges_left == sges_in_segment) ?
  909. 0 : (sges_in_segment * ioc->sge_size)/4;
  910. chain_length = sges_in_segment * ioc->sge_size;
  911. if (chain_offset) {
  912. chain_offset = chain_offset <<
  913. MPI2_SGE_CHAIN_OFFSET_SHIFT;
  914. chain_length += ioc->sge_size;
  915. }
  916. ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
  917. chain_length, chain_dma);
  918. sg_local = chain;
  919. if (!chain_offset)
  920. goto fill_in_last_segment;
  921. /* fill in chain segments */
  922. while (sges_in_segment) {
  923. if (sges_in_segment == 1)
  924. ioc->base_add_sg_single(sg_local,
  925. sgl_flags_last_element |
  926. sg_dma_len(sg_scmd),
  927. sg_dma_address(sg_scmd));
  928. else
  929. ioc->base_add_sg_single(sg_local, sgl_flags |
  930. sg_dma_len(sg_scmd),
  931. sg_dma_address(sg_scmd));
  932. sg_scmd = sg_next(sg_scmd);
  933. sg_local += ioc->sge_size;
  934. sges_left--;
  935. sges_in_segment--;
  936. }
  937. chain_dma += ioc->request_sz;
  938. chain += ioc->request_sz;
  939. } while (1);
  940. fill_in_last_segment:
  941. /* fill the last segment */
  942. while (sges_left) {
  943. if (sges_left == 1)
  944. ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
  945. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  946. else
  947. ioc->base_add_sg_single(sg_local, sgl_flags |
  948. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  949. sg_scmd = sg_next(sg_scmd);
  950. sg_local += ioc->sge_size;
  951. sges_left--;
  952. }
  953. return 0;
  954. }
  955. /**
  956. * scsih_change_queue_depth - setting device queue depth
  957. * @sdev: scsi device struct
  958. * @qdepth: requested queue depth
  959. *
  960. * Returns queue depth.
  961. */
  962. static int
  963. scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
  964. {
  965. struct Scsi_Host *shost = sdev->host;
  966. int max_depth;
  967. int tag_type;
  968. max_depth = shost->can_queue;
  969. if (!sdev->tagged_supported)
  970. max_depth = 1;
  971. if (qdepth > max_depth)
  972. qdepth = max_depth;
  973. tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
  974. scsi_adjust_queue_depth(sdev, tag_type, qdepth);
  975. if (sdev->inquiry_len > 7)
  976. sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
  977. "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
  978. sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
  979. sdev->ordered_tags, sdev->scsi_level,
  980. (sdev->inquiry[7] & 2) >> 1);
  981. return sdev->queue_depth;
  982. }
  983. /**
  984. * scsih_change_queue_depth - changing device queue tag type
  985. * @sdev: scsi device struct
  986. * @tag_type: requested tag type
  987. *
  988. * Returns queue tag type.
  989. */
  990. static int
  991. scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
  992. {
  993. if (sdev->tagged_supported) {
  994. scsi_set_tag_type(sdev, tag_type);
  995. if (tag_type)
  996. scsi_activate_tcq(sdev, sdev->queue_depth);
  997. else
  998. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  999. } else
  1000. tag_type = 0;
  1001. return tag_type;
  1002. }
  1003. /**
  1004. * scsih_target_alloc - target add routine
  1005. * @starget: scsi target struct
  1006. *
  1007. * Returns 0 if ok. Any other return is assumed to be an error and
  1008. * the device is ignored.
  1009. */
  1010. static int
  1011. scsih_target_alloc(struct scsi_target *starget)
  1012. {
  1013. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  1014. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1015. struct MPT2SAS_TARGET *sas_target_priv_data;
  1016. struct _sas_device *sas_device;
  1017. struct _raid_device *raid_device;
  1018. unsigned long flags;
  1019. struct sas_rphy *rphy;
  1020. sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
  1021. if (!sas_target_priv_data)
  1022. return -ENOMEM;
  1023. starget->hostdata = sas_target_priv_data;
  1024. sas_target_priv_data->starget = starget;
  1025. sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
  1026. /* RAID volumes */
  1027. if (starget->channel == RAID_CHANNEL) {
  1028. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1029. raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
  1030. starget->channel);
  1031. if (raid_device) {
  1032. sas_target_priv_data->handle = raid_device->handle;
  1033. sas_target_priv_data->sas_address = raid_device->wwid;
  1034. sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
  1035. raid_device->starget = starget;
  1036. }
  1037. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1038. return 0;
  1039. }
  1040. /* sas/sata devices */
  1041. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1042. rphy = dev_to_rphy(starget->dev.parent);
  1043. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1044. rphy->identify.sas_address);
  1045. if (sas_device) {
  1046. sas_target_priv_data->handle = sas_device->handle;
  1047. sas_target_priv_data->sas_address = sas_device->sas_address;
  1048. sas_device->starget = starget;
  1049. sas_device->id = starget->id;
  1050. sas_device->channel = starget->channel;
  1051. if (sas_device->hidden_raid_component)
  1052. sas_target_priv_data->flags |=
  1053. MPT_TARGET_FLAGS_RAID_COMPONENT;
  1054. }
  1055. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1056. return 0;
  1057. }
  1058. /**
  1059. * scsih_target_destroy - target destroy routine
  1060. * @starget: scsi target struct
  1061. *
  1062. * Returns nothing.
  1063. */
  1064. static void
  1065. scsih_target_destroy(struct scsi_target *starget)
  1066. {
  1067. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  1068. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1069. struct MPT2SAS_TARGET *sas_target_priv_data;
  1070. struct _sas_device *sas_device;
  1071. struct _raid_device *raid_device;
  1072. unsigned long flags;
  1073. struct sas_rphy *rphy;
  1074. sas_target_priv_data = starget->hostdata;
  1075. if (!sas_target_priv_data)
  1076. return;
  1077. if (starget->channel == RAID_CHANNEL) {
  1078. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1079. raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
  1080. starget->channel);
  1081. if (raid_device) {
  1082. raid_device->starget = NULL;
  1083. raid_device->sdev = NULL;
  1084. }
  1085. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1086. goto out;
  1087. }
  1088. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1089. rphy = dev_to_rphy(starget->dev.parent);
  1090. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1091. rphy->identify.sas_address);
  1092. if (sas_device && (sas_device->starget == starget) &&
  1093. (sas_device->id == starget->id) &&
  1094. (sas_device->channel == starget->channel))
  1095. sas_device->starget = NULL;
  1096. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1097. out:
  1098. kfree(sas_target_priv_data);
  1099. starget->hostdata = NULL;
  1100. }
  1101. /**
  1102. * scsih_slave_alloc - device add routine
  1103. * @sdev: scsi device struct
  1104. *
  1105. * Returns 0 if ok. Any other return is assumed to be an error and
  1106. * the device is ignored.
  1107. */
  1108. static int
  1109. scsih_slave_alloc(struct scsi_device *sdev)
  1110. {
  1111. struct Scsi_Host *shost;
  1112. struct MPT2SAS_ADAPTER *ioc;
  1113. struct MPT2SAS_TARGET *sas_target_priv_data;
  1114. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1115. struct scsi_target *starget;
  1116. struct _raid_device *raid_device;
  1117. struct _sas_device *sas_device;
  1118. unsigned long flags;
  1119. sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
  1120. if (!sas_device_priv_data)
  1121. return -ENOMEM;
  1122. sas_device_priv_data->lun = sdev->lun;
  1123. sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
  1124. starget = scsi_target(sdev);
  1125. sas_target_priv_data = starget->hostdata;
  1126. sas_target_priv_data->num_luns++;
  1127. sas_device_priv_data->sas_target = sas_target_priv_data;
  1128. sdev->hostdata = sas_device_priv_data;
  1129. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
  1130. sdev->no_uld_attach = 1;
  1131. shost = dev_to_shost(&starget->dev);
  1132. ioc = shost_priv(shost);
  1133. if (starget->channel == RAID_CHANNEL) {
  1134. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1135. raid_device = _scsih_raid_device_find_by_id(ioc,
  1136. starget->id, starget->channel);
  1137. if (raid_device)
  1138. raid_device->sdev = sdev; /* raid is single lun */
  1139. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1140. } else {
  1141. /* set TLR bit for SSP devices */
  1142. if (!(ioc->facts.IOCCapabilities &
  1143. MPI2_IOCFACTS_CAPABILITY_TLR))
  1144. goto out;
  1145. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1146. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1147. sas_device_priv_data->sas_target->sas_address);
  1148. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1149. if (sas_device && sas_device->device_info &
  1150. MPI2_SAS_DEVICE_INFO_SSP_TARGET)
  1151. sas_device_priv_data->flags |= MPT_DEVICE_TLR_ON;
  1152. }
  1153. out:
  1154. return 0;
  1155. }
  1156. /**
  1157. * scsih_slave_destroy - device destroy routine
  1158. * @sdev: scsi device struct
  1159. *
  1160. * Returns nothing.
  1161. */
  1162. static void
  1163. scsih_slave_destroy(struct scsi_device *sdev)
  1164. {
  1165. struct MPT2SAS_TARGET *sas_target_priv_data;
  1166. struct scsi_target *starget;
  1167. if (!sdev->hostdata)
  1168. return;
  1169. starget = scsi_target(sdev);
  1170. sas_target_priv_data = starget->hostdata;
  1171. sas_target_priv_data->num_luns--;
  1172. kfree(sdev->hostdata);
  1173. sdev->hostdata = NULL;
  1174. }
  1175. /**
  1176. * scsih_display_sata_capabilities - sata capabilities
  1177. * @ioc: per adapter object
  1178. * @sas_device: the sas_device object
  1179. * @sdev: scsi device struct
  1180. */
  1181. static void
  1182. scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
  1183. struct _sas_device *sas_device, struct scsi_device *sdev)
  1184. {
  1185. Mpi2ConfigReply_t mpi_reply;
  1186. Mpi2SasDevicePage0_t sas_device_pg0;
  1187. u32 ioc_status;
  1188. u16 flags;
  1189. u32 device_info;
  1190. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  1191. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
  1192. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1193. ioc->name, __FILE__, __LINE__, __func__);
  1194. return;
  1195. }
  1196. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1197. MPI2_IOCSTATUS_MASK;
  1198. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1199. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1200. ioc->name, __FILE__, __LINE__, __func__);
  1201. return;
  1202. }
  1203. flags = le16_to_cpu(sas_device_pg0.Flags);
  1204. device_info = le16_to_cpu(sas_device_pg0.DeviceInfo);
  1205. sdev_printk(KERN_INFO, sdev,
  1206. "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
  1207. "sw_preserve(%s)\n",
  1208. (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
  1209. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
  1210. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
  1211. "n",
  1212. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
  1213. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
  1214. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
  1215. }
  1216. /**
  1217. * _scsih_get_volume_capabilities - volume capabilities
  1218. * @ioc: per adapter object
  1219. * @sas_device: the raid_device object
  1220. */
  1221. static void
  1222. _scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
  1223. struct _raid_device *raid_device)
  1224. {
  1225. Mpi2RaidVolPage0_t *vol_pg0;
  1226. Mpi2RaidPhysDiskPage0_t pd_pg0;
  1227. Mpi2SasDevicePage0_t sas_device_pg0;
  1228. Mpi2ConfigReply_t mpi_reply;
  1229. u16 sz;
  1230. u8 num_pds;
  1231. if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
  1232. &num_pds)) || !num_pds) {
  1233. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1234. ioc->name, __FILE__, __LINE__, __func__);
  1235. return;
  1236. }
  1237. raid_device->num_pds = num_pds;
  1238. sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
  1239. sizeof(Mpi2RaidVol0PhysDisk_t));
  1240. vol_pg0 = kzalloc(sz, GFP_KERNEL);
  1241. if (!vol_pg0) {
  1242. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1243. ioc->name, __FILE__, __LINE__, __func__);
  1244. return;
  1245. }
  1246. if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
  1247. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
  1248. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1249. ioc->name, __FILE__, __LINE__, __func__);
  1250. kfree(vol_pg0);
  1251. return;
  1252. }
  1253. raid_device->volume_type = vol_pg0->VolumeType;
  1254. /* figure out what the underlying devices are by
  1255. * obtaining the device_info bits for the 1st device
  1256. */
  1257. if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
  1258. &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
  1259. vol_pg0->PhysDisk[0].PhysDiskNum))) {
  1260. if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  1261. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  1262. le16_to_cpu(pd_pg0.DevHandle)))) {
  1263. raid_device->device_info =
  1264. le32_to_cpu(sas_device_pg0.DeviceInfo);
  1265. }
  1266. }
  1267. kfree(vol_pg0);
  1268. }
  1269. /**
  1270. * scsih_slave_configure - device configure routine.
  1271. * @sdev: scsi device struct
  1272. *
  1273. * Returns 0 if ok. Any other return is assumed to be an error and
  1274. * the device is ignored.
  1275. */
  1276. static int
  1277. scsih_slave_configure(struct scsi_device *sdev)
  1278. {
  1279. struct Scsi_Host *shost = sdev->host;
  1280. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1281. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1282. struct MPT2SAS_TARGET *sas_target_priv_data;
  1283. struct _sas_device *sas_device;
  1284. struct _raid_device *raid_device;
  1285. unsigned long flags;
  1286. int qdepth;
  1287. u8 ssp_target = 0;
  1288. char *ds = "";
  1289. char *r_level = "";
  1290. qdepth = 1;
  1291. sas_device_priv_data = sdev->hostdata;
  1292. sas_device_priv_data->configured_lun = 1;
  1293. sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
  1294. sas_target_priv_data = sas_device_priv_data->sas_target;
  1295. /* raid volume handling */
  1296. if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
  1297. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1298. raid_device = _scsih_raid_device_find_by_handle(ioc,
  1299. sas_target_priv_data->handle);
  1300. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1301. if (!raid_device) {
  1302. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1303. ioc->name, __FILE__, __LINE__, __func__);
  1304. return 0;
  1305. }
  1306. _scsih_get_volume_capabilities(ioc, raid_device);
  1307. /* RAID Queue Depth Support
  1308. * IS volume = underlying qdepth of drive type, either
  1309. * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
  1310. * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
  1311. */
  1312. if (raid_device->device_info &
  1313. MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
  1314. qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
  1315. ds = "SSP";
  1316. } else {
  1317. qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
  1318. if (raid_device->device_info &
  1319. MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  1320. ds = "SATA";
  1321. else
  1322. ds = "STP";
  1323. }
  1324. switch (raid_device->volume_type) {
  1325. case MPI2_RAID_VOL_TYPE_RAID0:
  1326. r_level = "RAID0";
  1327. break;
  1328. case MPI2_RAID_VOL_TYPE_RAID1E:
  1329. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1330. r_level = "RAID1E";
  1331. break;
  1332. case MPI2_RAID_VOL_TYPE_RAID1:
  1333. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1334. r_level = "RAID1";
  1335. break;
  1336. case MPI2_RAID_VOL_TYPE_RAID10:
  1337. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1338. r_level = "RAID10";
  1339. break;
  1340. case MPI2_RAID_VOL_TYPE_UNKNOWN:
  1341. default:
  1342. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1343. r_level = "RAIDX";
  1344. break;
  1345. }
  1346. sdev_printk(KERN_INFO, sdev, "%s: "
  1347. "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
  1348. r_level, raid_device->handle,
  1349. (unsigned long long)raid_device->wwid,
  1350. raid_device->num_pds, ds);
  1351. scsih_change_queue_depth(sdev, qdepth);
  1352. return 0;
  1353. }
  1354. /* non-raid handling */
  1355. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1356. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1357. sas_device_priv_data->sas_target->sas_address);
  1358. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1359. if (sas_device) {
  1360. if (sas_target_priv_data->flags &
  1361. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1362. mpt2sas_config_get_volume_handle(ioc,
  1363. sas_device->handle, &sas_device->volume_handle);
  1364. mpt2sas_config_get_volume_wwid(ioc,
  1365. sas_device->volume_handle,
  1366. &sas_device->volume_wwid);
  1367. }
  1368. if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
  1369. qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
  1370. ssp_target = 1;
  1371. ds = "SSP";
  1372. } else {
  1373. qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
  1374. if (sas_device->device_info &
  1375. MPI2_SAS_DEVICE_INFO_STP_TARGET)
  1376. ds = "STP";
  1377. else if (sas_device->device_info &
  1378. MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  1379. ds = "SATA";
  1380. }
  1381. sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
  1382. "sas_addr(0x%016llx), device_name(0x%016llx)\n",
  1383. ds, sas_device->handle,
  1384. (unsigned long long)sas_device->sas_address,
  1385. (unsigned long long)sas_device->device_name);
  1386. sdev_printk(KERN_INFO, sdev, "%s: "
  1387. "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
  1388. (unsigned long long) sas_device->enclosure_logical_id,
  1389. sas_device->slot);
  1390. if (!ssp_target)
  1391. scsih_display_sata_capabilities(ioc, sas_device, sdev);
  1392. }
  1393. scsih_change_queue_depth(sdev, qdepth);
  1394. if (ssp_target)
  1395. sas_read_port_mode_page(sdev);
  1396. return 0;
  1397. }
  1398. /**
  1399. * scsih_bios_param - fetch head, sector, cylinder info for a disk
  1400. * @sdev: scsi device struct
  1401. * @bdev: pointer to block device context
  1402. * @capacity: device size (in 512 byte sectors)
  1403. * @params: three element array to place output:
  1404. * params[0] number of heads (max 255)
  1405. * params[1] number of sectors (max 63)
  1406. * params[2] number of cylinders
  1407. *
  1408. * Return nothing.
  1409. */
  1410. static int
  1411. scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1412. sector_t capacity, int params[])
  1413. {
  1414. int heads;
  1415. int sectors;
  1416. sector_t cylinders;
  1417. ulong dummy;
  1418. heads = 64;
  1419. sectors = 32;
  1420. dummy = heads * sectors;
  1421. cylinders = capacity;
  1422. sector_div(cylinders, dummy);
  1423. /*
  1424. * Handle extended translation size for logical drives
  1425. * > 1Gb
  1426. */
  1427. if ((ulong)capacity >= 0x200000) {
  1428. heads = 255;
  1429. sectors = 63;
  1430. dummy = heads * sectors;
  1431. cylinders = capacity;
  1432. sector_div(cylinders, dummy);
  1433. }
  1434. /* return result */
  1435. params[0] = heads;
  1436. params[1] = sectors;
  1437. params[2] = cylinders;
  1438. return 0;
  1439. }
  1440. /**
  1441. * _scsih_response_code - translation of device response code
  1442. * @ioc: per adapter object
  1443. * @response_code: response code returned by the device
  1444. *
  1445. * Return nothing.
  1446. */
  1447. static void
  1448. _scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
  1449. {
  1450. char *desc;
  1451. switch (response_code) {
  1452. case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
  1453. desc = "task management request completed";
  1454. break;
  1455. case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
  1456. desc = "invalid frame";
  1457. break;
  1458. case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
  1459. desc = "task management request not supported";
  1460. break;
  1461. case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
  1462. desc = "task management request failed";
  1463. break;
  1464. case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
  1465. desc = "task management request succeeded";
  1466. break;
  1467. case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
  1468. desc = "invalid lun";
  1469. break;
  1470. case 0xA:
  1471. desc = "overlapped tag attempted";
  1472. break;
  1473. case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
  1474. desc = "task queued, however not sent to target";
  1475. break;
  1476. default:
  1477. desc = "unknown";
  1478. break;
  1479. }
  1480. printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
  1481. ioc->name, response_code, desc);
  1482. }
  1483. /**
  1484. * scsih_tm_done - tm completion routine
  1485. * @ioc: per adapter object
  1486. * @smid: system request message index
  1487. * @VF_ID: virtual function id
  1488. * @reply: reply message frame(lower 32bit addr)
  1489. * Context: none.
  1490. *
  1491. * The callback handler when using scsih_issue_tm.
  1492. *
  1493. * Return nothing.
  1494. */
  1495. static void
  1496. scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
  1497. {
  1498. MPI2DefaultReply_t *mpi_reply;
  1499. if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
  1500. return;
  1501. if (ioc->tm_cmds.smid != smid)
  1502. return;
  1503. ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
  1504. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  1505. if (mpi_reply) {
  1506. memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  1507. ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
  1508. }
  1509. ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
  1510. complete(&ioc->tm_cmds.done);
  1511. }
  1512. /**
  1513. * mpt2sas_scsih_set_tm_flag - set per target tm_busy
  1514. * @ioc: per adapter object
  1515. * @handle: device handle
  1516. *
  1517. * During taskmangement request, we need to freeze the device queue.
  1518. */
  1519. void
  1520. mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  1521. {
  1522. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1523. struct scsi_device *sdev;
  1524. u8 skip = 0;
  1525. shost_for_each_device(sdev, ioc->shost) {
  1526. if (skip)
  1527. continue;
  1528. sas_device_priv_data = sdev->hostdata;
  1529. if (!sas_device_priv_data)
  1530. continue;
  1531. if (sas_device_priv_data->sas_target->handle == handle) {
  1532. sas_device_priv_data->sas_target->tm_busy = 1;
  1533. skip = 1;
  1534. ioc->ignore_loginfos = 1;
  1535. }
  1536. }
  1537. }
  1538. /**
  1539. * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
  1540. * @ioc: per adapter object
  1541. * @handle: device handle
  1542. *
  1543. * During taskmangement request, we need to freeze the device queue.
  1544. */
  1545. void
  1546. mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  1547. {
  1548. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1549. struct scsi_device *sdev;
  1550. u8 skip = 0;
  1551. shost_for_each_device(sdev, ioc->shost) {
  1552. if (skip)
  1553. continue;
  1554. sas_device_priv_data = sdev->hostdata;
  1555. if (!sas_device_priv_data)
  1556. continue;
  1557. if (sas_device_priv_data->sas_target->handle == handle) {
  1558. sas_device_priv_data->sas_target->tm_busy = 0;
  1559. skip = 1;
  1560. ioc->ignore_loginfos = 0;
  1561. }
  1562. }
  1563. }
  1564. /**
  1565. * mpt2sas_scsih_issue_tm - main routine for sending tm requests
  1566. * @ioc: per adapter struct
  1567. * @device_handle: device handle
  1568. * @lun: lun number
  1569. * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
  1570. * @smid_task: smid assigned to the task
  1571. * @timeout: timeout in seconds
  1572. * Context: The calling function needs to acquire the tm_cmds.mutex
  1573. *
  1574. * A generic API for sending task management requests to firmware.
  1575. *
  1576. * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
  1577. * this API.
  1578. *
  1579. * The callback index is set inside `ioc->tm_cb_idx`.
  1580. *
  1581. * Return nothing.
  1582. */
  1583. void
  1584. mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
  1585. u8 type, u16 smid_task, ulong timeout)
  1586. {
  1587. Mpi2SCSITaskManagementRequest_t *mpi_request;
  1588. Mpi2SCSITaskManagementReply_t *mpi_reply;
  1589. u16 smid = 0;
  1590. u32 ioc_state;
  1591. unsigned long timeleft;
  1592. u8 VF_ID = 0;
  1593. unsigned long flags;
  1594. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  1595. if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED ||
  1596. ioc->shost_recovery) {
  1597. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  1598. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  1599. __func__, ioc->name);
  1600. return;
  1601. }
  1602. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  1603. ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
  1604. if (ioc_state & MPI2_DOORBELL_USED) {
  1605. dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
  1606. "active!\n", ioc->name));
  1607. goto issue_host_reset;
  1608. }
  1609. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  1610. mpt2sas_base_fault_info(ioc, ioc_state &
  1611. MPI2_DOORBELL_DATA_MASK);
  1612. goto issue_host_reset;
  1613. }
  1614. smid = mpt2sas_base_get_smid(ioc, ioc->tm_cb_idx);
  1615. if (!smid) {
  1616. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1617. ioc->name, __func__);
  1618. return;
  1619. }
  1620. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
  1621. " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type, smid));
  1622. ioc->tm_cmds.status = MPT2_CMD_PENDING;
  1623. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1624. ioc->tm_cmds.smid = smid;
  1625. memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
  1626. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  1627. mpi_request->DevHandle = cpu_to_le16(handle);
  1628. mpi_request->TaskType = type;
  1629. mpi_request->TaskMID = cpu_to_le16(smid_task);
  1630. int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
  1631. mpt2sas_scsih_set_tm_flag(ioc, handle);
  1632. mpt2sas_base_put_smid_hi_priority(ioc, smid, VF_ID);
  1633. timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
  1634. mpt2sas_scsih_clear_tm_flag(ioc, handle);
  1635. if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
  1636. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  1637. ioc->name, __func__);
  1638. _debug_dump_mf(mpi_request,
  1639. sizeof(Mpi2SCSITaskManagementRequest_t)/4);
  1640. if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
  1641. goto issue_host_reset;
  1642. }
  1643. if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
  1644. mpi_reply = ioc->tm_cmds.reply;
  1645. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
  1646. "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
  1647. ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
  1648. le32_to_cpu(mpi_reply->IOCLogInfo),
  1649. le32_to_cpu(mpi_reply->TerminationCount)));
  1650. if (ioc->logging_level & MPT_DEBUG_TM)
  1651. _scsih_response_code(ioc, mpi_reply->ResponseCode);
  1652. }
  1653. return;
  1654. issue_host_reset:
  1655. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
  1656. }
  1657. /**
  1658. * scsih_abort - eh threads main abort routine
  1659. * @sdev: scsi device struct
  1660. *
  1661. * Returns SUCCESS if command aborted else FAILED
  1662. */
  1663. static int
  1664. scsih_abort(struct scsi_cmnd *scmd)
  1665. {
  1666. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  1667. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1668. u16 smid;
  1669. u16 handle;
  1670. int r;
  1671. struct scsi_cmnd *scmd_lookup;
  1672. printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
  1673. ioc->name, scmd);
  1674. scsi_print_command(scmd);
  1675. sas_device_priv_data = scmd->device->hostdata;
  1676. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  1677. printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
  1678. ioc->name, scmd);
  1679. scmd->result = DID_NO_CONNECT << 16;
  1680. scmd->scsi_done(scmd);
  1681. r = SUCCESS;
  1682. goto out;
  1683. }
  1684. /* search for the command */
  1685. smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
  1686. if (!smid) {
  1687. scmd->result = DID_RESET << 16;
  1688. r = SUCCESS;
  1689. goto out;
  1690. }
  1691. /* for hidden raid components and volumes this is not supported */
  1692. if (sas_device_priv_data->sas_target->flags &
  1693. MPT_TARGET_FLAGS_RAID_COMPONENT ||
  1694. sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
  1695. scmd->result = DID_RESET << 16;
  1696. r = FAILED;
  1697. goto out;
  1698. }
  1699. mutex_lock(&ioc->tm_cmds.mutex);
  1700. handle = sas_device_priv_data->sas_target->handle;
  1701. mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
  1702. MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
  1703. /* sanity check - see whether command actually completed */
  1704. scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
  1705. if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
  1706. r = FAILED;
  1707. else
  1708. r = SUCCESS;
  1709. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  1710. mutex_unlock(&ioc->tm_cmds.mutex);
  1711. out:
  1712. printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
  1713. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  1714. return r;
  1715. }
  1716. /**
  1717. * scsih_dev_reset - eh threads main device reset routine
  1718. * @sdev: scsi device struct
  1719. *
  1720. * Returns SUCCESS if command aborted else FAILED
  1721. */
  1722. static int
  1723. scsih_dev_reset(struct scsi_cmnd *scmd)
  1724. {
  1725. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  1726. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1727. struct _sas_device *sas_device;
  1728. unsigned long flags;
  1729. u16 handle;
  1730. int r;
  1731. printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
  1732. ioc->name, scmd);
  1733. scsi_print_command(scmd);
  1734. sas_device_priv_data = scmd->device->hostdata;
  1735. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  1736. printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
  1737. ioc->name, scmd);
  1738. scmd->result = DID_NO_CONNECT << 16;
  1739. scmd->scsi_done(scmd);
  1740. r = SUCCESS;
  1741. goto out;
  1742. }
  1743. /* for hidden raid components obtain the volume_handle */
  1744. handle = 0;
  1745. if (sas_device_priv_data->sas_target->flags &
  1746. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1747. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1748. sas_device = _scsih_sas_device_find_by_handle(ioc,
  1749. sas_device_priv_data->sas_target->handle);
  1750. if (sas_device)
  1751. handle = sas_device->volume_handle;
  1752. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1753. } else
  1754. handle = sas_device_priv_data->sas_target->handle;
  1755. if (!handle) {
  1756. scmd->result = DID_RESET << 16;
  1757. r = FAILED;
  1758. goto out;
  1759. }
  1760. mutex_lock(&ioc->tm_cmds.mutex);
  1761. mpt2sas_scsih_issue_tm(ioc, handle, 0,
  1762. MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
  1763. /*
  1764. * sanity check see whether all commands to this target been
  1765. * completed
  1766. */
  1767. if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
  1768. scmd->device->channel))
  1769. r = FAILED;
  1770. else
  1771. r = SUCCESS;
  1772. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  1773. mutex_unlock(&ioc->tm_cmds.mutex);
  1774. out:
  1775. printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
  1776. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  1777. return r;
  1778. }
  1779. /**
  1780. * scsih_abort - eh threads main host reset routine
  1781. * @sdev: scsi device struct
  1782. *
  1783. * Returns SUCCESS if command aborted else FAILED
  1784. */
  1785. static int
  1786. scsih_host_reset(struct scsi_cmnd *scmd)
  1787. {
  1788. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  1789. int r, retval;
  1790. printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
  1791. ioc->name, scmd);
  1792. scsi_print_command(scmd);
  1793. retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1794. FORCE_BIG_HAMMER);
  1795. r = (retval < 0) ? FAILED : SUCCESS;
  1796. printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
  1797. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  1798. return r;
  1799. }
  1800. /**
  1801. * _scsih_fw_event_add - insert and queue up fw_event
  1802. * @ioc: per adapter object
  1803. * @fw_event: object describing the event
  1804. * Context: This function will acquire ioc->fw_event_lock.
  1805. *
  1806. * This adds the firmware event object into link list, then queues it up to
  1807. * be processed from user context.
  1808. *
  1809. * Return nothing.
  1810. */
  1811. static void
  1812. _scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
  1813. {
  1814. unsigned long flags;
  1815. if (ioc->firmware_event_thread == NULL)
  1816. return;
  1817. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  1818. list_add_tail(&fw_event->list, &ioc->fw_event_list);
  1819. INIT_WORK(&fw_event->work, _firmware_event_work);
  1820. queue_work(ioc->firmware_event_thread, &fw_event->work);
  1821. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  1822. }
  1823. /**
  1824. * _scsih_fw_event_free - delete fw_event
  1825. * @ioc: per adapter object
  1826. * @fw_event: object describing the event
  1827. * Context: This function will acquire ioc->fw_event_lock.
  1828. *
  1829. * This removes firmware event object from link list, frees associated memory.
  1830. *
  1831. * Return nothing.
  1832. */
  1833. static void
  1834. _scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
  1835. *fw_event)
  1836. {
  1837. unsigned long flags;
  1838. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  1839. list_del(&fw_event->list);
  1840. kfree(fw_event->event_data);
  1841. kfree(fw_event);
  1842. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  1843. }
  1844. /**
  1845. * _scsih_fw_event_add - requeue an event
  1846. * @ioc: per adapter object
  1847. * @fw_event: object describing the event
  1848. * Context: This function will acquire ioc->fw_event_lock.
  1849. *
  1850. * Return nothing.
  1851. */
  1852. static void
  1853. _scsih_fw_event_requeue(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
  1854. *fw_event, unsigned long delay)
  1855. {
  1856. unsigned long flags;
  1857. if (ioc->firmware_event_thread == NULL)
  1858. return;
  1859. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  1860. queue_work(ioc->firmware_event_thread, &fw_event->work);
  1861. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  1862. }
  1863. /**
  1864. * _scsih_fw_event_off - turn flag off preventing event handling
  1865. * @ioc: per adapter object
  1866. *
  1867. * Used to prevent handling of firmware events during adapter reset
  1868. * driver unload.
  1869. *
  1870. * Return nothing.
  1871. */
  1872. static void
  1873. _scsih_fw_event_off(struct MPT2SAS_ADAPTER *ioc)
  1874. {
  1875. unsigned long flags;
  1876. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  1877. ioc->fw_events_off = 1;
  1878. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  1879. }
  1880. /**
  1881. * _scsih_fw_event_on - turn flag on allowing firmware event handling
  1882. * @ioc: per adapter object
  1883. *
  1884. * Returns nothing.
  1885. */
  1886. static void
  1887. _scsih_fw_event_on(struct MPT2SAS_ADAPTER *ioc)
  1888. {
  1889. unsigned long flags;
  1890. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  1891. ioc->fw_events_off = 0;
  1892. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  1893. }
  1894. /**
  1895. * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
  1896. * @ioc: per adapter object
  1897. * @handle: device handle
  1898. *
  1899. * During device pull we need to appropiately set the sdev state.
  1900. */
  1901. static void
  1902. _scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  1903. {
  1904. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1905. struct scsi_device *sdev;
  1906. shost_for_each_device(sdev, ioc->shost) {
  1907. sas_device_priv_data = sdev->hostdata;
  1908. if (!sas_device_priv_data)
  1909. continue;
  1910. if (!sas_device_priv_data->block)
  1911. continue;
  1912. if (sas_device_priv_data->sas_target->handle == handle) {
  1913. dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
  1914. MPT2SAS_INFO_FMT "SDEV_RUNNING: "
  1915. "handle(0x%04x)\n", ioc->name, handle));
  1916. sas_device_priv_data->block = 0;
  1917. scsi_device_set_state(sdev, SDEV_RUNNING);
  1918. }
  1919. }
  1920. }
  1921. /**
  1922. * _scsih_block_io_device - set the device state to SDEV_BLOCK
  1923. * @ioc: per adapter object
  1924. * @handle: device handle
  1925. *
  1926. * During device pull we need to appropiately set the sdev state.
  1927. */
  1928. static void
  1929. _scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  1930. {
  1931. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1932. struct scsi_device *sdev;
  1933. shost_for_each_device(sdev, ioc->shost) {
  1934. sas_device_priv_data = sdev->hostdata;
  1935. if (!sas_device_priv_data)
  1936. continue;
  1937. if (sas_device_priv_data->block)
  1938. continue;
  1939. if (sas_device_priv_data->sas_target->handle == handle) {
  1940. dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
  1941. MPT2SAS_INFO_FMT "SDEV_BLOCK: "
  1942. "handle(0x%04x)\n", ioc->name, handle));
  1943. sas_device_priv_data->block = 1;
  1944. scsi_device_set_state(sdev, SDEV_BLOCK);
  1945. }
  1946. }
  1947. }
  1948. /**
  1949. * _scsih_block_io_to_children_attached_to_ex
  1950. * @ioc: per adapter object
  1951. * @sas_expander: the sas_device object
  1952. *
  1953. * This routine set sdev state to SDEV_BLOCK for all devices
  1954. * attached to this expander. This function called when expander is
  1955. * pulled.
  1956. */
  1957. static void
  1958. _scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
  1959. struct _sas_node *sas_expander)
  1960. {
  1961. struct _sas_port *mpt2sas_port;
  1962. struct _sas_device *sas_device;
  1963. struct _sas_node *expander_sibling;
  1964. unsigned long flags;
  1965. if (!sas_expander)
  1966. return;
  1967. list_for_each_entry(mpt2sas_port,
  1968. &sas_expander->sas_port_list, port_list) {
  1969. if (mpt2sas_port->remote_identify.device_type ==
  1970. SAS_END_DEVICE) {
  1971. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1972. sas_device =
  1973. mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1974. mpt2sas_port->remote_identify.sas_address);
  1975. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1976. if (!sas_device)
  1977. continue;
  1978. _scsih_block_io_device(ioc, sas_device->handle);
  1979. }
  1980. }
  1981. list_for_each_entry(mpt2sas_port,
  1982. &sas_expander->sas_port_list, port_list) {
  1983. if (mpt2sas_port->remote_identify.device_type ==
  1984. MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  1985. mpt2sas_port->remote_identify.device_type ==
  1986. MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
  1987. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1988. expander_sibling =
  1989. mpt2sas_scsih_expander_find_by_sas_address(
  1990. ioc, mpt2sas_port->remote_identify.sas_address);
  1991. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1992. _scsih_block_io_to_children_attached_to_ex(ioc,
  1993. expander_sibling);
  1994. }
  1995. }
  1996. }
  1997. /**
  1998. * _scsih_block_io_to_children_attached_directly
  1999. * @ioc: per adapter object
  2000. * @event_data: topology change event data
  2001. *
  2002. * This routine set sdev state to SDEV_BLOCK for all devices
  2003. * direct attached during device pull.
  2004. */
  2005. static void
  2006. _scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
  2007. Mpi2EventDataSasTopologyChangeList_t *event_data)
  2008. {
  2009. int i;
  2010. u16 handle;
  2011. u16 reason_code;
  2012. u8 phy_number;
  2013. for (i = 0; i < event_data->NumEntries; i++) {
  2014. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  2015. if (!handle)
  2016. continue;
  2017. phy_number = event_data->StartPhyNum + i;
  2018. reason_code = event_data->PHY[i].PhyStatus &
  2019. MPI2_EVENT_SAS_TOPO_RC_MASK;
  2020. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
  2021. _scsih_block_io_device(ioc, handle);
  2022. }
  2023. }
  2024. /**
  2025. * _scsih_check_topo_delete_events - sanity check on topo events
  2026. * @ioc: per adapter object
  2027. * @event_data: the event data payload
  2028. *
  2029. * This routine added to better handle cable breaker.
  2030. *
  2031. * This handles the case where driver recieves multiple expander
  2032. * add and delete events in a single shot. When there is a delete event
  2033. * the routine will void any pending add events waiting in the event queue.
  2034. *
  2035. * Return nothing.
  2036. */
  2037. static void
  2038. _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
  2039. Mpi2EventDataSasTopologyChangeList_t *event_data)
  2040. {
  2041. struct fw_event_work *fw_event;
  2042. Mpi2EventDataSasTopologyChangeList_t *local_event_data;
  2043. u16 expander_handle;
  2044. struct _sas_node *sas_expander;
  2045. unsigned long flags;
  2046. expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  2047. if (expander_handle < ioc->sas_hba.num_phys) {
  2048. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  2049. return;
  2050. }
  2051. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
  2052. || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
  2053. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  2054. sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
  2055. expander_handle);
  2056. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  2057. _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
  2058. } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
  2059. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  2060. if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
  2061. return;
  2062. /* mark ignore flag for pending events */
  2063. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  2064. list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
  2065. if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
  2066. fw_event->ignore)
  2067. continue;
  2068. local_event_data = fw_event->event_data;
  2069. if (local_event_data->ExpStatus ==
  2070. MPI2_EVENT_SAS_TOPO_ES_ADDED ||
  2071. local_event_data->ExpStatus ==
  2072. MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
  2073. if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
  2074. expander_handle) {
  2075. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  2076. "setting ignoring flag\n", ioc->name));
  2077. fw_event->ignore = 1;
  2078. }
  2079. }
  2080. }
  2081. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  2082. }
  2083. /**
  2084. * _scsih_queue_rescan - queue a topology rescan from user context
  2085. * @ioc: per adapter object
  2086. *
  2087. * Return nothing.
  2088. */
  2089. static void
  2090. _scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
  2091. {
  2092. struct fw_event_work *fw_event;
  2093. if (ioc->wait_for_port_enable_to_complete)
  2094. return;
  2095. fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
  2096. if (!fw_event)
  2097. return;
  2098. fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
  2099. fw_event->ioc = ioc;
  2100. _scsih_fw_event_add(ioc, fw_event);
  2101. }
  2102. /**
  2103. * _scsih_flush_running_cmds - completing outstanding commands.
  2104. * @ioc: per adapter object
  2105. *
  2106. * The flushing out of all pending scmd commands following host reset,
  2107. * where all IO is dropped to the floor.
  2108. *
  2109. * Return nothing.
  2110. */
  2111. static void
  2112. _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
  2113. {
  2114. struct scsi_cmnd *scmd;
  2115. u16 smid;
  2116. u16 count = 0;
  2117. for (smid = 1; smid <= ioc->request_depth; smid++) {
  2118. scmd = _scsih_scsi_lookup_getclear(ioc, smid);
  2119. if (!scmd)
  2120. continue;
  2121. count++;
  2122. mpt2sas_base_free_smid(ioc, smid);
  2123. scsi_dma_unmap(scmd);
  2124. scmd->result = DID_RESET << 16;
  2125. scmd->scsi_done(scmd);
  2126. }
  2127. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
  2128. ioc->name, count));
  2129. }
  2130. /**
  2131. * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
  2132. * @ioc: per adapter object
  2133. * @reset_phase: phase
  2134. *
  2135. * The handler for doing any required cleanup or initialization.
  2136. *
  2137. * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
  2138. * MPT2_IOC_DONE_RESET
  2139. *
  2140. * Return nothing.
  2141. */
  2142. void
  2143. mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
  2144. {
  2145. switch (reset_phase) {
  2146. case MPT2_IOC_PRE_RESET:
  2147. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  2148. "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
  2149. _scsih_fw_event_off(ioc);
  2150. break;
  2151. case MPT2_IOC_AFTER_RESET:
  2152. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  2153. "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
  2154. if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
  2155. ioc->tm_cmds.status |= MPT2_CMD_RESET;
  2156. mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
  2157. complete(&ioc->tm_cmds.done);
  2158. }
  2159. _scsih_fw_event_on(ioc);
  2160. _scsih_flush_running_cmds(ioc);
  2161. break;
  2162. case MPT2_IOC_DONE_RESET:
  2163. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  2164. "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
  2165. _scsih_queue_rescan(ioc);
  2166. break;
  2167. }
  2168. }
  2169. /**
  2170. * _scsih_setup_eedp - setup MPI request for EEDP transfer
  2171. * @scmd: pointer to scsi command object
  2172. * @mpi_request: pointer to the SCSI_IO reqest message frame
  2173. *
  2174. * Supporting protection 1 and 3.
  2175. *
  2176. * Returns nothing
  2177. */
  2178. static void
  2179. _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
  2180. {
  2181. u16 eedp_flags;
  2182. unsigned char prot_op = scsi_get_prot_op(scmd);
  2183. unsigned char prot_type = scsi_get_prot_type(scmd);
  2184. if (prot_type == SCSI_PROT_DIF_TYPE0 ||
  2185. prot_type == SCSI_PROT_DIF_TYPE2 ||
  2186. prot_op == SCSI_PROT_NORMAL)
  2187. return;
  2188. if (prot_op == SCSI_PROT_READ_STRIP)
  2189. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
  2190. else if (prot_op == SCSI_PROT_WRITE_INSERT)
  2191. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
  2192. else
  2193. return;
  2194. mpi_request->EEDPBlockSize = scmd->device->sector_size;
  2195. switch (prot_type) {
  2196. case SCSI_PROT_DIF_TYPE1:
  2197. /*
  2198. * enable ref/guard checking
  2199. * auto increment ref tag
  2200. */
  2201. mpi_request->EEDPFlags = eedp_flags |
  2202. MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  2203. MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
  2204. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  2205. mpi_request->CDB.EEDP32.PrimaryReferenceTag =
  2206. cpu_to_be32(scsi_get_lba(scmd));
  2207. break;
  2208. case SCSI_PROT_DIF_TYPE3:
  2209. /*
  2210. * enable guard checking
  2211. */
  2212. mpi_request->EEDPFlags = eedp_flags |
  2213. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  2214. break;
  2215. }
  2216. }
  2217. /**
  2218. * _scsih_eedp_error_handling - return sense code for EEDP errors
  2219. * @scmd: pointer to scsi command object
  2220. * @ioc_status: ioc status
  2221. *
  2222. * Returns nothing
  2223. */
  2224. static void
  2225. _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
  2226. {
  2227. u8 ascq;
  2228. u8 sk;
  2229. u8 host_byte;
  2230. switch (ioc_status) {
  2231. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  2232. ascq = 0x01;
  2233. break;
  2234. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  2235. ascq = 0x02;
  2236. break;
  2237. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  2238. ascq = 0x03;
  2239. break;
  2240. default:
  2241. ascq = 0x00;
  2242. break;
  2243. }
  2244. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  2245. sk = ILLEGAL_REQUEST;
  2246. host_byte = DID_ABORT;
  2247. } else {
  2248. sk = ABORTED_COMMAND;
  2249. host_byte = DID_OK;
  2250. }
  2251. scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
  2252. scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
  2253. SAM_STAT_CHECK_CONDITION;
  2254. }
  2255. /**
  2256. * scsih_qcmd - main scsi request entry point
  2257. * @scmd: pointer to scsi command object
  2258. * @done: function pointer to be invoked on completion
  2259. *
  2260. * The callback index is set inside `ioc->scsi_io_cb_idx`.
  2261. *
  2262. * Returns 0 on success. If there's a failure, return either:
  2263. * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
  2264. * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
  2265. */
  2266. static int
  2267. scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
  2268. {
  2269. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2270. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2271. struct MPT2SAS_TARGET *sas_target_priv_data;
  2272. Mpi2SCSIIORequest_t *mpi_request;
  2273. u32 mpi_control;
  2274. u16 smid;
  2275. unsigned long flags;
  2276. scmd->scsi_done = done;
  2277. sas_device_priv_data = scmd->device->hostdata;
  2278. if (!sas_device_priv_data) {
  2279. scmd->result = DID_NO_CONNECT << 16;
  2280. scmd->scsi_done(scmd);
  2281. return 0;
  2282. }
  2283. sas_target_priv_data = sas_device_priv_data->sas_target;
  2284. if (!sas_target_priv_data || sas_target_priv_data->handle ==
  2285. MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
  2286. scmd->result = DID_NO_CONNECT << 16;
  2287. scmd->scsi_done(scmd);
  2288. return 0;
  2289. }
  2290. /* see if we are busy with task managment stuff */
  2291. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  2292. if (sas_target_priv_data->tm_busy ||
  2293. ioc->shost_recovery || ioc->ioc_link_reset_in_progress) {
  2294. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  2295. return SCSI_MLQUEUE_HOST_BUSY;
  2296. }
  2297. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  2298. if (scmd->sc_data_direction == DMA_FROM_DEVICE)
  2299. mpi_control = MPI2_SCSIIO_CONTROL_READ;
  2300. else if (scmd->sc_data_direction == DMA_TO_DEVICE)
  2301. mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
  2302. else
  2303. mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
  2304. /* set tags */
  2305. if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
  2306. if (scmd->device->tagged_supported) {
  2307. if (scmd->device->ordered_tags)
  2308. mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
  2309. else
  2310. mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
  2311. } else
  2312. /* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
  2313. /* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
  2314. */
  2315. mpi_control |= (0x500);
  2316. } else
  2317. mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
  2318. if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON))
  2319. mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
  2320. smid = mpt2sas_base_get_smid(ioc, ioc->scsi_io_cb_idx);
  2321. if (!smid) {
  2322. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2323. ioc->name, __func__);
  2324. goto out;
  2325. }
  2326. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  2327. memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
  2328. _scsih_setup_eedp(scmd, mpi_request);
  2329. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  2330. if (sas_device_priv_data->sas_target->flags &
  2331. MPT_TARGET_FLAGS_RAID_COMPONENT)
  2332. mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
  2333. else
  2334. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  2335. mpi_request->DevHandle =
  2336. cpu_to_le16(sas_device_priv_data->sas_target->handle);
  2337. mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
  2338. mpi_request->Control = cpu_to_le32(mpi_control);
  2339. mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
  2340. mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
  2341. mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  2342. mpi_request->SenseBufferLowAddress =
  2343. (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
  2344. mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
  2345. mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
  2346. MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
  2347. int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
  2348. mpi_request->LUN);
  2349. memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
  2350. if (!mpi_request->DataLength) {
  2351. mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
  2352. } else {
  2353. if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
  2354. mpt2sas_base_free_smid(ioc, smid);
  2355. goto out;
  2356. }
  2357. }
  2358. _scsih_scsi_lookup_set(ioc, smid, scmd);
  2359. mpt2sas_base_put_smid_scsi_io(ioc, smid, 0,
  2360. sas_device_priv_data->sas_target->handle);
  2361. return 0;
  2362. out:
  2363. return SCSI_MLQUEUE_HOST_BUSY;
  2364. }
  2365. /**
  2366. * _scsih_normalize_sense - normalize descriptor and fixed format sense data
  2367. * @sense_buffer: sense data returned by target
  2368. * @data: normalized skey/asc/ascq
  2369. *
  2370. * Return nothing.
  2371. */
  2372. static void
  2373. _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
  2374. {
  2375. if ((sense_buffer[0] & 0x7F) >= 0x72) {
  2376. /* descriptor format */
  2377. data->skey = sense_buffer[1] & 0x0F;
  2378. data->asc = sense_buffer[2];
  2379. data->ascq = sense_buffer[3];
  2380. } else {
  2381. /* fixed format */
  2382. data->skey = sense_buffer[2] & 0x0F;
  2383. data->asc = sense_buffer[12];
  2384. data->ascq = sense_buffer[13];
  2385. }
  2386. }
  2387. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  2388. /**
  2389. * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
  2390. * @ioc: per adapter object
  2391. * @scmd: pointer to scsi command object
  2392. * @mpi_reply: reply mf payload returned from firmware
  2393. *
  2394. * scsi_status - SCSI Status code returned from target device
  2395. * scsi_state - state info associated with SCSI_IO determined by ioc
  2396. * ioc_status - ioc supplied status info
  2397. *
  2398. * Return nothing.
  2399. */
  2400. static void
  2401. _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
  2402. Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
  2403. {
  2404. u32 response_info;
  2405. u8 *response_bytes;
  2406. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  2407. MPI2_IOCSTATUS_MASK;
  2408. u8 scsi_state = mpi_reply->SCSIState;
  2409. u8 scsi_status = mpi_reply->SCSIStatus;
  2410. char *desc_ioc_state = NULL;
  2411. char *desc_scsi_status = NULL;
  2412. char *desc_scsi_state = ioc->tmp_string;
  2413. switch (ioc_status) {
  2414. case MPI2_IOCSTATUS_SUCCESS:
  2415. desc_ioc_state = "success";
  2416. break;
  2417. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  2418. desc_ioc_state = "invalid function";
  2419. break;
  2420. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  2421. desc_ioc_state = "scsi recovered error";
  2422. break;
  2423. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  2424. desc_ioc_state = "scsi invalid dev handle";
  2425. break;
  2426. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  2427. desc_ioc_state = "scsi device not there";
  2428. break;
  2429. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  2430. desc_ioc_state = "scsi data overrun";
  2431. break;
  2432. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  2433. desc_ioc_state = "scsi data underrun";
  2434. break;
  2435. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  2436. desc_ioc_state = "scsi io data error";
  2437. break;
  2438. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  2439. desc_ioc_state = "scsi protocol error";
  2440. break;
  2441. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  2442. desc_ioc_state = "scsi task terminated";
  2443. break;
  2444. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  2445. desc_ioc_state = "scsi residual mismatch";
  2446. break;
  2447. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  2448. desc_ioc_state = "scsi task mgmt failed";
  2449. break;
  2450. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  2451. desc_ioc_state = "scsi ioc terminated";
  2452. break;
  2453. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  2454. desc_ioc_state = "scsi ext terminated";
  2455. break;
  2456. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  2457. desc_ioc_state = "eedp guard error";
  2458. break;
  2459. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  2460. desc_ioc_state = "eedp ref tag error";
  2461. break;
  2462. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  2463. desc_ioc_state = "eedp app tag error";
  2464. break;
  2465. default:
  2466. desc_ioc_state = "unknown";
  2467. break;
  2468. }
  2469. switch (scsi_status) {
  2470. case MPI2_SCSI_STATUS_GOOD:
  2471. desc_scsi_status = "good";
  2472. break;
  2473. case MPI2_SCSI_STATUS_CHECK_CONDITION:
  2474. desc_scsi_status = "check condition";
  2475. break;
  2476. case MPI2_SCSI_STATUS_CONDITION_MET:
  2477. desc_scsi_status = "condition met";
  2478. break;
  2479. case MPI2_SCSI_STATUS_BUSY:
  2480. desc_scsi_status = "busy";
  2481. break;
  2482. case MPI2_SCSI_STATUS_INTERMEDIATE:
  2483. desc_scsi_status = "intermediate";
  2484. break;
  2485. case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
  2486. desc_scsi_status = "intermediate condmet";
  2487. break;
  2488. case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
  2489. desc_scsi_status = "reservation conflict";
  2490. break;
  2491. case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
  2492. desc_scsi_status = "command terminated";
  2493. break;
  2494. case MPI2_SCSI_STATUS_TASK_SET_FULL:
  2495. desc_scsi_status = "task set full";
  2496. break;
  2497. case MPI2_SCSI_STATUS_ACA_ACTIVE:
  2498. desc_scsi_status = "aca active";
  2499. break;
  2500. case MPI2_SCSI_STATUS_TASK_ABORTED:
  2501. desc_scsi_status = "task aborted";
  2502. break;
  2503. default:
  2504. desc_scsi_status = "unknown";
  2505. break;
  2506. }
  2507. desc_scsi_state[0] = '\0';
  2508. if (!scsi_state)
  2509. desc_scsi_state = " ";
  2510. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
  2511. strcat(desc_scsi_state, "response info ");
  2512. if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  2513. strcat(desc_scsi_state, "state terminated ");
  2514. if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
  2515. strcat(desc_scsi_state, "no status ");
  2516. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
  2517. strcat(desc_scsi_state, "autosense failed ");
  2518. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
  2519. strcat(desc_scsi_state, "autosense valid ");
  2520. scsi_print_command(scmd);
  2521. printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
  2522. "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
  2523. le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
  2524. ioc_status, smid);
  2525. printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
  2526. "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
  2527. scsi_get_resid(scmd));
  2528. printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
  2529. "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
  2530. le32_to_cpu(mpi_reply->TransferCount), scmd->result);
  2531. printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
  2532. "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
  2533. scsi_status, desc_scsi_state, scsi_state);
  2534. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  2535. struct sense_info data;
  2536. _scsih_normalize_sense(scmd->sense_buffer, &data);
  2537. printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
  2538. "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
  2539. data.asc, data.ascq);
  2540. }
  2541. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
  2542. response_info = le32_to_cpu(mpi_reply->ResponseInfo);
  2543. response_bytes = (u8 *)&response_info;
  2544. _scsih_response_code(ioc, response_bytes[3]);
  2545. }
  2546. }
  2547. #endif
  2548. /**
  2549. * _scsih_smart_predicted_fault - illuminate Fault LED
  2550. * @ioc: per adapter object
  2551. * @handle: device handle
  2552. *
  2553. * Return nothing.
  2554. */
  2555. static void
  2556. _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2557. {
  2558. Mpi2SepReply_t mpi_reply;
  2559. Mpi2SepRequest_t mpi_request;
  2560. struct scsi_target *starget;
  2561. struct MPT2SAS_TARGET *sas_target_priv_data;
  2562. Mpi2EventNotificationReply_t *event_reply;
  2563. Mpi2EventDataSasDeviceStatusChange_t *event_data;
  2564. struct _sas_device *sas_device;
  2565. ssize_t sz;
  2566. unsigned long flags;
  2567. /* only handle non-raid devices */
  2568. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2569. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  2570. if (!sas_device) {
  2571. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2572. return;
  2573. }
  2574. starget = sas_device->starget;
  2575. sas_target_priv_data = starget->hostdata;
  2576. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
  2577. ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
  2578. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2579. return;
  2580. }
  2581. starget_printk(KERN_WARNING, starget, "predicted fault\n");
  2582. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2583. if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
  2584. memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
  2585. mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
  2586. mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
  2587. mpi_request.SlotStatus =
  2588. MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
  2589. mpi_request.DevHandle = cpu_to_le16(handle);
  2590. mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
  2591. if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
  2592. &mpi_request)) != 0) {
  2593. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2594. ioc->name, __FILE__, __LINE__, __func__);
  2595. return;
  2596. }
  2597. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
  2598. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2599. "enclosure_processor: ioc_status (0x%04x), "
  2600. "loginfo(0x%08x)\n", ioc->name,
  2601. le16_to_cpu(mpi_reply.IOCStatus),
  2602. le32_to_cpu(mpi_reply.IOCLogInfo)));
  2603. return;
  2604. }
  2605. }
  2606. /* insert into event log */
  2607. sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
  2608. sizeof(Mpi2EventDataSasDeviceStatusChange_t);
  2609. event_reply = kzalloc(sz, GFP_KERNEL);
  2610. if (!event_reply) {
  2611. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2612. ioc->name, __FILE__, __LINE__, __func__);
  2613. return;
  2614. }
  2615. event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  2616. event_reply->Event =
  2617. cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  2618. event_reply->MsgLength = sz/4;
  2619. event_reply->EventDataLength =
  2620. cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
  2621. event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
  2622. event_reply->EventData;
  2623. event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
  2624. event_data->ASC = 0x5D;
  2625. event_data->DevHandle = cpu_to_le16(handle);
  2626. event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
  2627. mpt2sas_ctl_add_to_event_log(ioc, event_reply);
  2628. kfree(event_reply);
  2629. }
  2630. /**
  2631. * scsih_io_done - scsi request callback
  2632. * @ioc: per adapter object
  2633. * @smid: system request message index
  2634. * @VF_ID: virtual function id
  2635. * @reply: reply message frame(lower 32bit addr)
  2636. *
  2637. * Callback handler when using scsih_qcmd.
  2638. *
  2639. * Return nothing.
  2640. */
  2641. static void
  2642. scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
  2643. {
  2644. Mpi2SCSIIORequest_t *mpi_request;
  2645. Mpi2SCSIIOReply_t *mpi_reply;
  2646. struct scsi_cmnd *scmd;
  2647. u16 ioc_status;
  2648. u32 xfer_cnt;
  2649. u8 scsi_state;
  2650. u8 scsi_status;
  2651. u32 log_info;
  2652. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2653. u32 response_code;
  2654. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  2655. scmd = _scsih_scsi_lookup_getclear(ioc, smid);
  2656. if (scmd == NULL)
  2657. return;
  2658. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  2659. if (mpi_reply == NULL) {
  2660. scmd->result = DID_OK << 16;
  2661. goto out;
  2662. }
  2663. sas_device_priv_data = scmd->device->hostdata;
  2664. if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
  2665. sas_device_priv_data->sas_target->deleted) {
  2666. scmd->result = DID_NO_CONNECT << 16;
  2667. goto out;
  2668. }
  2669. /* turning off TLR */
  2670. if (!sas_device_priv_data->tlr_snoop_check) {
  2671. sas_device_priv_data->tlr_snoop_check++;
  2672. if (sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) {
  2673. response_code = (le32_to_cpu(mpi_reply->ResponseInfo)
  2674. >> 24);
  2675. if (response_code ==
  2676. MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
  2677. sas_device_priv_data->flags &=
  2678. ~MPT_DEVICE_TLR_ON;
  2679. }
  2680. }
  2681. xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
  2682. scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
  2683. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  2684. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
  2685. log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  2686. else
  2687. log_info = 0;
  2688. ioc_status &= MPI2_IOCSTATUS_MASK;
  2689. scsi_state = mpi_reply->SCSIState;
  2690. scsi_status = mpi_reply->SCSIStatus;
  2691. if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
  2692. (scsi_status == MPI2_SCSI_STATUS_BUSY ||
  2693. scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
  2694. scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
  2695. ioc_status = MPI2_IOCSTATUS_SUCCESS;
  2696. }
  2697. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  2698. struct sense_info data;
  2699. const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
  2700. smid);
  2701. u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
  2702. le32_to_cpu(mpi_reply->SenseCount));
  2703. memcpy(scmd->sense_buffer, sense_data, sz);
  2704. _scsih_normalize_sense(scmd->sense_buffer, &data);
  2705. /* failure prediction threshold exceeded */
  2706. if (data.asc == 0x5D)
  2707. _scsih_smart_predicted_fault(ioc,
  2708. le16_to_cpu(mpi_reply->DevHandle));
  2709. }
  2710. switch (ioc_status) {
  2711. case MPI2_IOCSTATUS_BUSY:
  2712. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  2713. scmd->result = SAM_STAT_BUSY;
  2714. break;
  2715. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  2716. scmd->result = DID_NO_CONNECT << 16;
  2717. break;
  2718. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  2719. if (sas_device_priv_data->block) {
  2720. scmd->result = (DID_BUS_BUSY << 16);
  2721. break;
  2722. }
  2723. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  2724. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  2725. scmd->result = DID_RESET << 16;
  2726. break;
  2727. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  2728. if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
  2729. scmd->result = DID_SOFT_ERROR << 16;
  2730. else
  2731. scmd->result = (DID_OK << 16) | scsi_status;
  2732. break;
  2733. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  2734. scmd->result = (DID_OK << 16) | scsi_status;
  2735. if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
  2736. break;
  2737. if (xfer_cnt < scmd->underflow) {
  2738. if (scsi_status == SAM_STAT_BUSY)
  2739. scmd->result = SAM_STAT_BUSY;
  2740. else
  2741. scmd->result = DID_SOFT_ERROR << 16;
  2742. } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  2743. MPI2_SCSI_STATE_NO_SCSI_STATUS))
  2744. scmd->result = DID_SOFT_ERROR << 16;
  2745. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  2746. scmd->result = DID_RESET << 16;
  2747. else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
  2748. mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
  2749. mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
  2750. scmd->result = (DRIVER_SENSE << 24) |
  2751. SAM_STAT_CHECK_CONDITION;
  2752. scmd->sense_buffer[0] = 0x70;
  2753. scmd->sense_buffer[2] = ILLEGAL_REQUEST;
  2754. scmd->sense_buffer[12] = 0x20;
  2755. scmd->sense_buffer[13] = 0;
  2756. }
  2757. break;
  2758. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  2759. scsi_set_resid(scmd, 0);
  2760. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  2761. case MPI2_IOCSTATUS_SUCCESS:
  2762. scmd->result = (DID_OK << 16) | scsi_status;
  2763. if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  2764. MPI2_SCSI_STATE_NO_SCSI_STATUS))
  2765. scmd->result = DID_SOFT_ERROR << 16;
  2766. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  2767. scmd->result = DID_RESET << 16;
  2768. break;
  2769. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  2770. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  2771. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  2772. _scsih_eedp_error_handling(scmd, ioc_status);
  2773. break;
  2774. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  2775. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  2776. case MPI2_IOCSTATUS_INVALID_SGL:
  2777. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  2778. case MPI2_IOCSTATUS_INVALID_FIELD:
  2779. case MPI2_IOCSTATUS_INVALID_STATE:
  2780. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  2781. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  2782. default:
  2783. scmd->result = DID_SOFT_ERROR << 16;
  2784. break;
  2785. }
  2786. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  2787. if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
  2788. _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
  2789. #endif
  2790. out:
  2791. scsi_dma_unmap(scmd);
  2792. scmd->scsi_done(scmd);
  2793. }
  2794. /**
  2795. * _scsih_link_change - process phy link changes
  2796. * @ioc: per adapter object
  2797. * @handle: phy handle
  2798. * @attached_handle: valid for devices attached to link
  2799. * @phy_number: phy number
  2800. * @link_rate: new link rate
  2801. * Context: user.
  2802. *
  2803. * Return nothing.
  2804. */
  2805. static void
  2806. _scsih_link_change(struct MPT2SAS_ADAPTER *ioc, u16 handle, u16 attached_handle,
  2807. u8 phy_number, u8 link_rate)
  2808. {
  2809. mpt2sas_transport_update_phy_link_change(ioc, handle, attached_handle,
  2810. phy_number, link_rate);
  2811. }
  2812. /**
  2813. * _scsih_sas_host_refresh - refreshing sas host object contents
  2814. * @ioc: per adapter object
  2815. * @update: update link information
  2816. * Context: user
  2817. *
  2818. * During port enable, fw will send topology events for every device. Its
  2819. * possible that the handles may change from the previous setting, so this
  2820. * code keeping handles updating if changed.
  2821. *
  2822. * Return nothing.
  2823. */
  2824. static void
  2825. _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc, u8 update)
  2826. {
  2827. u16 sz;
  2828. u16 ioc_status;
  2829. int i;
  2830. Mpi2ConfigReply_t mpi_reply;
  2831. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  2832. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
  2833. "updating handles for sas_host(0x%016llx)\n",
  2834. ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
  2835. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
  2836. * sizeof(Mpi2SasIOUnit0PhyData_t));
  2837. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  2838. if (!sas_iounit_pg0) {
  2839. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2840. ioc->name, __FILE__, __LINE__, __func__);
  2841. return;
  2842. }
  2843. if (!(mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  2844. sas_iounit_pg0, sz))) {
  2845. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2846. MPI2_IOCSTATUS_MASK;
  2847. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  2848. goto out;
  2849. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  2850. ioc->sas_hba.phy[i].handle =
  2851. le16_to_cpu(sas_iounit_pg0->PhyData[i].
  2852. ControllerDevHandle);
  2853. if (update)
  2854. _scsih_link_change(ioc,
  2855. ioc->sas_hba.phy[i].handle,
  2856. le16_to_cpu(sas_iounit_pg0->PhyData[i].
  2857. AttachedDevHandle), i,
  2858. sas_iounit_pg0->PhyData[i].
  2859. NegotiatedLinkRate >> 4);
  2860. }
  2861. }
  2862. out:
  2863. kfree(sas_iounit_pg0);
  2864. }
  2865. /**
  2866. * _scsih_sas_host_add - create sas host object
  2867. * @ioc: per adapter object
  2868. *
  2869. * Creating host side data object, stored in ioc->sas_hba
  2870. *
  2871. * Return nothing.
  2872. */
  2873. static void
  2874. _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
  2875. {
  2876. int i;
  2877. Mpi2ConfigReply_t mpi_reply;
  2878. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  2879. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  2880. Mpi2SasPhyPage0_t phy_pg0;
  2881. Mpi2SasDevicePage0_t sas_device_pg0;
  2882. Mpi2SasEnclosurePage0_t enclosure_pg0;
  2883. u16 ioc_status;
  2884. u16 sz;
  2885. u16 device_missing_delay;
  2886. mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
  2887. if (!ioc->sas_hba.num_phys) {
  2888. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2889. ioc->name, __FILE__, __LINE__, __func__);
  2890. return;
  2891. }
  2892. /* sas_iounit page 0 */
  2893. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
  2894. sizeof(Mpi2SasIOUnit0PhyData_t));
  2895. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  2896. if (!sas_iounit_pg0) {
  2897. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2898. ioc->name, __FILE__, __LINE__, __func__);
  2899. return;
  2900. }
  2901. if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  2902. sas_iounit_pg0, sz))) {
  2903. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2904. ioc->name, __FILE__, __LINE__, __func__);
  2905. goto out;
  2906. }
  2907. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2908. MPI2_IOCSTATUS_MASK;
  2909. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2910. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2911. ioc->name, __FILE__, __LINE__, __func__);
  2912. goto out;
  2913. }
  2914. /* sas_iounit page 1 */
  2915. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  2916. sizeof(Mpi2SasIOUnit1PhyData_t));
  2917. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  2918. if (!sas_iounit_pg1) {
  2919. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2920. ioc->name, __FILE__, __LINE__, __func__);
  2921. goto out;
  2922. }
  2923. if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  2924. sas_iounit_pg1, sz))) {
  2925. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2926. ioc->name, __FILE__, __LINE__, __func__);
  2927. goto out;
  2928. }
  2929. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2930. MPI2_IOCSTATUS_MASK;
  2931. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2932. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2933. ioc->name, __FILE__, __LINE__, __func__);
  2934. goto out;
  2935. }
  2936. ioc->io_missing_delay =
  2937. le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
  2938. device_missing_delay =
  2939. le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
  2940. if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  2941. ioc->device_missing_delay = (device_missing_delay &
  2942. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  2943. else
  2944. ioc->device_missing_delay = device_missing_delay &
  2945. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2946. ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
  2947. ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
  2948. sizeof(struct _sas_phy), GFP_KERNEL);
  2949. if (!ioc->sas_hba.phy) {
  2950. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2951. ioc->name, __FILE__, __LINE__, __func__);
  2952. goto out;
  2953. }
  2954. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  2955. if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
  2956. i))) {
  2957. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2958. ioc->name, __FILE__, __LINE__, __func__);
  2959. goto out;
  2960. }
  2961. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2962. MPI2_IOCSTATUS_MASK;
  2963. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2964. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2965. ioc->name, __FILE__, __LINE__, __func__);
  2966. goto out;
  2967. }
  2968. ioc->sas_hba.phy[i].handle =
  2969. le16_to_cpu(sas_iounit_pg0->PhyData[i].ControllerDevHandle);
  2970. ioc->sas_hba.phy[i].phy_id = i;
  2971. mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
  2972. phy_pg0, ioc->sas_hba.parent_dev);
  2973. }
  2974. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  2975. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.phy[0].handle))) {
  2976. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2977. ioc->name, __FILE__, __LINE__, __func__);
  2978. goto out;
  2979. }
  2980. ioc->sas_hba.handle = le16_to_cpu(sas_device_pg0.DevHandle);
  2981. ioc->sas_hba.enclosure_handle =
  2982. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  2983. ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  2984. printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
  2985. "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
  2986. (unsigned long long) ioc->sas_hba.sas_address,
  2987. ioc->sas_hba.num_phys) ;
  2988. if (ioc->sas_hba.enclosure_handle) {
  2989. if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  2990. &enclosure_pg0,
  2991. MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  2992. ioc->sas_hba.enclosure_handle))) {
  2993. ioc->sas_hba.enclosure_logical_id =
  2994. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  2995. }
  2996. }
  2997. out:
  2998. kfree(sas_iounit_pg1);
  2999. kfree(sas_iounit_pg0);
  3000. }
  3001. /**
  3002. * _scsih_expander_add - creating expander object
  3003. * @ioc: per adapter object
  3004. * @handle: expander handle
  3005. *
  3006. * Creating expander object, stored in ioc->sas_expander_list.
  3007. *
  3008. * Return 0 for success, else error.
  3009. */
  3010. static int
  3011. _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3012. {
  3013. struct _sas_node *sas_expander;
  3014. Mpi2ConfigReply_t mpi_reply;
  3015. Mpi2ExpanderPage0_t expander_pg0;
  3016. Mpi2ExpanderPage1_t expander_pg1;
  3017. Mpi2SasEnclosurePage0_t enclosure_pg0;
  3018. u32 ioc_status;
  3019. u16 parent_handle;
  3020. __le64 sas_address;
  3021. int i;
  3022. unsigned long flags;
  3023. struct _sas_port *mpt2sas_port;
  3024. int rc = 0;
  3025. if (!handle)
  3026. return -1;
  3027. if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  3028. MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
  3029. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3030. ioc->name, __FILE__, __LINE__, __func__);
  3031. return -1;
  3032. }
  3033. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3034. MPI2_IOCSTATUS_MASK;
  3035. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3036. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3037. ioc->name, __FILE__, __LINE__, __func__);
  3038. return -1;
  3039. }
  3040. /* handle out of order topology events */
  3041. parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
  3042. if (parent_handle >= ioc->sas_hba.num_phys) {
  3043. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3044. sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
  3045. parent_handle);
  3046. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3047. if (!sas_expander) {
  3048. rc = _scsih_expander_add(ioc, parent_handle);
  3049. if (rc != 0)
  3050. return rc;
  3051. }
  3052. }
  3053. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  3054. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3055. sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
  3056. sas_address);
  3057. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3058. if (sas_expander)
  3059. return 0;
  3060. sas_expander = kzalloc(sizeof(struct _sas_node),
  3061. GFP_KERNEL);
  3062. if (!sas_expander) {
  3063. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3064. ioc->name, __FILE__, __LINE__, __func__);
  3065. return -1;
  3066. }
  3067. sas_expander->handle = handle;
  3068. sas_expander->num_phys = expander_pg0.NumPhys;
  3069. sas_expander->parent_handle = parent_handle;
  3070. sas_expander->enclosure_handle =
  3071. le16_to_cpu(expander_pg0.EnclosureHandle);
  3072. sas_expander->sas_address = sas_address;
  3073. printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
  3074. " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
  3075. handle, sas_expander->parent_handle, (unsigned long long)
  3076. sas_expander->sas_address, sas_expander->num_phys);
  3077. if (!sas_expander->num_phys)
  3078. goto out_fail;
  3079. sas_expander->phy = kcalloc(sas_expander->num_phys,
  3080. sizeof(struct _sas_phy), GFP_KERNEL);
  3081. if (!sas_expander->phy) {
  3082. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3083. ioc->name, __FILE__, __LINE__, __func__);
  3084. rc = -1;
  3085. goto out_fail;
  3086. }
  3087. INIT_LIST_HEAD(&sas_expander->sas_port_list);
  3088. mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
  3089. sas_expander->parent_handle);
  3090. if (!mpt2sas_port) {
  3091. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3092. ioc->name, __FILE__, __LINE__, __func__);
  3093. rc = -1;
  3094. goto out_fail;
  3095. }
  3096. sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
  3097. for (i = 0 ; i < sas_expander->num_phys ; i++) {
  3098. if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
  3099. &expander_pg1, i, handle))) {
  3100. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3101. ioc->name, __FILE__, __LINE__, __func__);
  3102. continue;
  3103. }
  3104. sas_expander->phy[i].handle = handle;
  3105. sas_expander->phy[i].phy_id = i;
  3106. mpt2sas_transport_add_expander_phy(ioc, &sas_expander->phy[i],
  3107. expander_pg1, sas_expander->parent_dev);
  3108. }
  3109. if (sas_expander->enclosure_handle) {
  3110. if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  3111. &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  3112. sas_expander->enclosure_handle))) {
  3113. sas_expander->enclosure_logical_id =
  3114. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  3115. }
  3116. }
  3117. _scsih_expander_node_add(ioc, sas_expander);
  3118. return 0;
  3119. out_fail:
  3120. if (sas_expander)
  3121. kfree(sas_expander->phy);
  3122. kfree(sas_expander);
  3123. return rc;
  3124. }
  3125. /**
  3126. * _scsih_expander_remove - removing expander object
  3127. * @ioc: per adapter object
  3128. * @handle: expander handle
  3129. *
  3130. * Return nothing.
  3131. */
  3132. static void
  3133. _scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3134. {
  3135. struct _sas_node *sas_expander;
  3136. unsigned long flags;
  3137. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3138. sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc, handle);
  3139. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3140. _scsih_expander_node_remove(ioc, sas_expander);
  3141. }
  3142. /**
  3143. * _scsih_add_device - creating sas device object
  3144. * @ioc: per adapter object
  3145. * @handle: sas device handle
  3146. * @phy_num: phy number end device attached to
  3147. * @is_pd: is this hidden raid component
  3148. *
  3149. * Creating end device object, stored in ioc->sas_device_list.
  3150. *
  3151. * Returns 0 for success, non-zero for failure.
  3152. */
  3153. static int
  3154. _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
  3155. {
  3156. Mpi2ConfigReply_t mpi_reply;
  3157. Mpi2SasDevicePage0_t sas_device_pg0;
  3158. Mpi2SasEnclosurePage0_t enclosure_pg0;
  3159. struct _sas_device *sas_device;
  3160. u32 ioc_status;
  3161. __le64 sas_address;
  3162. u32 device_info;
  3163. unsigned long flags;
  3164. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  3165. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  3166. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3167. ioc->name, __FILE__, __LINE__, __func__);
  3168. return -1;
  3169. }
  3170. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3171. MPI2_IOCSTATUS_MASK;
  3172. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3173. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3174. ioc->name, __FILE__, __LINE__, __func__);
  3175. return -1;
  3176. }
  3177. /* check if device is present */
  3178. if (!(le16_to_cpu(sas_device_pg0.Flags) &
  3179. MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
  3180. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3181. ioc->name, __FILE__, __LINE__, __func__);
  3182. printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
  3183. ioc->name, le16_to_cpu(sas_device_pg0.Flags));
  3184. return -1;
  3185. }
  3186. /* check if there were any issus with discovery */
  3187. if (sas_device_pg0.AccessStatus ==
  3188. MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
  3189. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3190. ioc->name, __FILE__, __LINE__, __func__);
  3191. printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
  3192. ioc->name, sas_device_pg0.AccessStatus);
  3193. return -1;
  3194. }
  3195. /* check if this is end device */
  3196. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  3197. if (!(_scsih_is_end_device(device_info))) {
  3198. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3199. ioc->name, __FILE__, __LINE__, __func__);
  3200. return -1;
  3201. }
  3202. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  3203. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3204. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  3205. sas_address);
  3206. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3207. if (sas_device) {
  3208. _scsih_ublock_io_device(ioc, handle);
  3209. return 0;
  3210. }
  3211. sas_device = kzalloc(sizeof(struct _sas_device),
  3212. GFP_KERNEL);
  3213. if (!sas_device) {
  3214. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3215. ioc->name, __FILE__, __LINE__, __func__);
  3216. return -1;
  3217. }
  3218. sas_device->handle = handle;
  3219. sas_device->parent_handle =
  3220. le16_to_cpu(sas_device_pg0.ParentDevHandle);
  3221. sas_device->enclosure_handle =
  3222. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  3223. sas_device->slot =
  3224. le16_to_cpu(sas_device_pg0.Slot);
  3225. sas_device->device_info = device_info;
  3226. sas_device->sas_address = sas_address;
  3227. sas_device->hidden_raid_component = is_pd;
  3228. /* get enclosure_logical_id */
  3229. if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply, &enclosure_pg0,
  3230. MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  3231. sas_device->enclosure_handle))) {
  3232. sas_device->enclosure_logical_id =
  3233. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  3234. }
  3235. /* get device name */
  3236. sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
  3237. if (ioc->wait_for_port_enable_to_complete)
  3238. _scsih_sas_device_init_add(ioc, sas_device);
  3239. else
  3240. _scsih_sas_device_add(ioc, sas_device);
  3241. return 0;
  3242. }
  3243. /**
  3244. * _scsih_remove_device - removing sas device object
  3245. * @ioc: per adapter object
  3246. * @handle: sas device handle
  3247. *
  3248. * Return nothing.
  3249. */
  3250. static void
  3251. _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3252. {
  3253. struct MPT2SAS_TARGET *sas_target_priv_data;
  3254. struct _sas_device *sas_device;
  3255. unsigned long flags;
  3256. Mpi2SasIoUnitControlReply_t mpi_reply;
  3257. Mpi2SasIoUnitControlRequest_t mpi_request;
  3258. u16 device_handle;
  3259. /* lookup sas_device */
  3260. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3261. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3262. if (!sas_device) {
  3263. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3264. return;
  3265. }
  3266. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle"
  3267. "(0x%04x)\n", ioc->name, __func__, handle));
  3268. if (sas_device->starget && sas_device->starget->hostdata) {
  3269. sas_target_priv_data = sas_device->starget->hostdata;
  3270. sas_target_priv_data->deleted = 1;
  3271. }
  3272. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3273. if (ioc->remove_host)
  3274. goto out;
  3275. /* Target Reset to flush out all the outstanding IO */
  3276. device_handle = (sas_device->hidden_raid_component) ?
  3277. sas_device->volume_handle : handle;
  3278. if (device_handle) {
  3279. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
  3280. "handle(0x%04x)\n", ioc->name, device_handle));
  3281. mutex_lock(&ioc->tm_cmds.mutex);
  3282. mpt2sas_scsih_issue_tm(ioc, device_handle, 0,
  3283. MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
  3284. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  3285. mutex_unlock(&ioc->tm_cmds.mutex);
  3286. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
  3287. "done: handle(0x%04x)\n", ioc->name, device_handle));
  3288. }
  3289. /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
  3290. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
  3291. "(0x%04x)\n", ioc->name, handle));
  3292. memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
  3293. mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  3294. mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
  3295. mpi_request.DevHandle = handle;
  3296. mpi_request.VF_ID = 0;
  3297. if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
  3298. &mpi_request)) != 0) {
  3299. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3300. ioc->name, __FILE__, __LINE__, __func__);
  3301. }
  3302. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
  3303. "(0x%04x), loginfo(0x%08x)\n", ioc->name,
  3304. le16_to_cpu(mpi_reply.IOCStatus),
  3305. le32_to_cpu(mpi_reply.IOCLogInfo)));
  3306. out:
  3307. mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
  3308. sas_device->parent_handle);
  3309. printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
  3310. "(0x%016llx)\n", ioc->name, sas_device->handle,
  3311. (unsigned long long) sas_device->sas_address);
  3312. _scsih_sas_device_remove(ioc, sas_device);
  3313. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle"
  3314. "(0x%04x)\n", ioc->name, __func__, handle));
  3315. }
  3316. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3317. /**
  3318. * _scsih_sas_topology_change_event_debug - debug for topology event
  3319. * @ioc: per adapter object
  3320. * @event_data: event data payload
  3321. * Context: user.
  3322. */
  3323. static void
  3324. _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  3325. Mpi2EventDataSasTopologyChangeList_t *event_data)
  3326. {
  3327. int i;
  3328. u16 handle;
  3329. u16 reason_code;
  3330. u8 phy_number;
  3331. char *status_str = NULL;
  3332. char link_rate[25];
  3333. switch (event_data->ExpStatus) {
  3334. case MPI2_EVENT_SAS_TOPO_ES_ADDED:
  3335. status_str = "add";
  3336. break;
  3337. case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
  3338. status_str = "remove";
  3339. break;
  3340. case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
  3341. status_str = "responding";
  3342. break;
  3343. case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
  3344. status_str = "remove delay";
  3345. break;
  3346. default:
  3347. status_str = "unknown status";
  3348. break;
  3349. }
  3350. printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
  3351. ioc->name, status_str);
  3352. printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
  3353. "start_phy(%02d), count(%d)\n",
  3354. le16_to_cpu(event_data->ExpanderDevHandle),
  3355. le16_to_cpu(event_data->EnclosureHandle),
  3356. event_data->StartPhyNum, event_data->NumEntries);
  3357. for (i = 0; i < event_data->NumEntries; i++) {
  3358. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  3359. if (!handle)
  3360. continue;
  3361. phy_number = event_data->StartPhyNum + i;
  3362. reason_code = event_data->PHY[i].PhyStatus &
  3363. MPI2_EVENT_SAS_TOPO_RC_MASK;
  3364. switch (reason_code) {
  3365. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  3366. snprintf(link_rate, 25, ": add, link(0x%02x)",
  3367. (event_data->PHY[i].LinkRate >> 4));
  3368. status_str = link_rate;
  3369. break;
  3370. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  3371. status_str = ": remove";
  3372. break;
  3373. case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
  3374. status_str = ": remove_delay";
  3375. break;
  3376. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  3377. snprintf(link_rate, 25, ": link(0x%02x)",
  3378. (event_data->PHY[i].LinkRate >> 4));
  3379. status_str = link_rate;
  3380. break;
  3381. case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
  3382. status_str = ": responding";
  3383. break;
  3384. default:
  3385. status_str = ": unknown";
  3386. break;
  3387. }
  3388. printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x)%s\n",
  3389. phy_number, handle, status_str);
  3390. }
  3391. }
  3392. #endif
  3393. /**
  3394. * _scsih_sas_topology_change_event - handle topology changes
  3395. * @ioc: per adapter object
  3396. * @VF_ID:
  3397. * @event_data: event data payload
  3398. * fw_event:
  3399. * Context: user.
  3400. *
  3401. */
  3402. static void
  3403. _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  3404. Mpi2EventDataSasTopologyChangeList_t *event_data,
  3405. struct fw_event_work *fw_event)
  3406. {
  3407. int i;
  3408. u16 parent_handle, handle;
  3409. u16 reason_code;
  3410. u8 phy_number;
  3411. struct _sas_node *sas_expander;
  3412. unsigned long flags;
  3413. u8 link_rate_;
  3414. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3415. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  3416. _scsih_sas_topology_change_event_debug(ioc, event_data);
  3417. #endif
  3418. if (!ioc->sas_hba.num_phys)
  3419. _scsih_sas_host_add(ioc);
  3420. else
  3421. _scsih_sas_host_refresh(ioc, 0);
  3422. if (fw_event->ignore) {
  3423. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
  3424. "event\n", ioc->name));
  3425. return;
  3426. }
  3427. parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  3428. /* handle expander add */
  3429. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
  3430. if (_scsih_expander_add(ioc, parent_handle) != 0)
  3431. return;
  3432. /* handle siblings events */
  3433. for (i = 0; i < event_data->NumEntries; i++) {
  3434. if (fw_event->ignore) {
  3435. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
  3436. "expander event\n", ioc->name));
  3437. return;
  3438. }
  3439. if (event_data->PHY[i].PhyStatus &
  3440. MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
  3441. continue;
  3442. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  3443. if (!handle)
  3444. continue;
  3445. phy_number = event_data->StartPhyNum + i;
  3446. reason_code = event_data->PHY[i].PhyStatus &
  3447. MPI2_EVENT_SAS_TOPO_RC_MASK;
  3448. link_rate_ = event_data->PHY[i].LinkRate >> 4;
  3449. switch (reason_code) {
  3450. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  3451. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  3452. if (!parent_handle) {
  3453. if (phy_number < ioc->sas_hba.num_phys)
  3454. _scsih_link_change(ioc,
  3455. ioc->sas_hba.phy[phy_number].handle,
  3456. handle, phy_number, link_rate_);
  3457. } else {
  3458. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3459. sas_expander =
  3460. mpt2sas_scsih_expander_find_by_handle(ioc,
  3461. parent_handle);
  3462. spin_unlock_irqrestore(&ioc->sas_node_lock,
  3463. flags);
  3464. if (sas_expander) {
  3465. if (phy_number < sas_expander->num_phys)
  3466. _scsih_link_change(ioc,
  3467. sas_expander->
  3468. phy[phy_number].handle,
  3469. handle, phy_number,
  3470. link_rate_);
  3471. }
  3472. }
  3473. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED) {
  3474. if (link_rate_ >= MPI2_SAS_NEG_LINK_RATE_1_5)
  3475. _scsih_ublock_io_device(ioc, handle);
  3476. }
  3477. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED) {
  3478. if (link_rate_ < MPI2_SAS_NEG_LINK_RATE_1_5)
  3479. break;
  3480. _scsih_add_device(ioc, handle, phy_number, 0);
  3481. }
  3482. break;
  3483. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  3484. _scsih_remove_device(ioc, handle);
  3485. break;
  3486. }
  3487. }
  3488. /* handle expander removal */
  3489. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
  3490. _scsih_expander_remove(ioc, parent_handle);
  3491. }
  3492. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3493. /**
  3494. * _scsih_sas_device_status_change_event_debug - debug for device event
  3495. * @event_data: event data payload
  3496. * Context: user.
  3497. *
  3498. * Return nothing.
  3499. */
  3500. static void
  3501. _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  3502. Mpi2EventDataSasDeviceStatusChange_t *event_data)
  3503. {
  3504. char *reason_str = NULL;
  3505. switch (event_data->ReasonCode) {
  3506. case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  3507. reason_str = "smart data";
  3508. break;
  3509. case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
  3510. reason_str = "unsupported device discovered";
  3511. break;
  3512. case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  3513. reason_str = "internal device reset";
  3514. break;
  3515. case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
  3516. reason_str = "internal task abort";
  3517. break;
  3518. case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
  3519. reason_str = "internal task abort set";
  3520. break;
  3521. case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
  3522. reason_str = "internal clear task set";
  3523. break;
  3524. case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
  3525. reason_str = "internal query task";
  3526. break;
  3527. case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
  3528. reason_str = "sata init failure";
  3529. break;
  3530. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
  3531. reason_str = "internal device reset complete";
  3532. break;
  3533. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
  3534. reason_str = "internal task abort complete";
  3535. break;
  3536. case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
  3537. reason_str = "internal async notification";
  3538. break;
  3539. default:
  3540. reason_str = "unknown reason";
  3541. break;
  3542. }
  3543. printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
  3544. "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
  3545. reason_str, le16_to_cpu(event_data->DevHandle),
  3546. (unsigned long long)le64_to_cpu(event_data->SASAddress));
  3547. if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
  3548. printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
  3549. event_data->ASC, event_data->ASCQ);
  3550. printk(KERN_INFO "\n");
  3551. }
  3552. #endif
  3553. /**
  3554. * _scsih_sas_device_status_change_event - handle device status change
  3555. * @ioc: per adapter object
  3556. * @VF_ID:
  3557. * @event_data: event data payload
  3558. * Context: user.
  3559. *
  3560. * Return nothing.
  3561. */
  3562. static void
  3563. _scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  3564. Mpi2EventDataSasDeviceStatusChange_t *event_data)
  3565. {
  3566. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3567. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  3568. _scsih_sas_device_status_change_event_debug(ioc, event_data);
  3569. #endif
  3570. }
  3571. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3572. /**
  3573. * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
  3574. * @ioc: per adapter object
  3575. * @event_data: event data payload
  3576. * Context: user.
  3577. *
  3578. * Return nothing.
  3579. */
  3580. static void
  3581. _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  3582. Mpi2EventDataSasEnclDevStatusChange_t *event_data)
  3583. {
  3584. char *reason_str = NULL;
  3585. switch (event_data->ReasonCode) {
  3586. case MPI2_EVENT_SAS_ENCL_RC_ADDED:
  3587. reason_str = "enclosure add";
  3588. break;
  3589. case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
  3590. reason_str = "enclosure remove";
  3591. break;
  3592. default:
  3593. reason_str = "unknown reason";
  3594. break;
  3595. }
  3596. printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
  3597. "\thandle(0x%04x), enclosure logical id(0x%016llx)"
  3598. " number slots(%d)\n", ioc->name, reason_str,
  3599. le16_to_cpu(event_data->EnclosureHandle),
  3600. (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
  3601. le16_to_cpu(event_data->StartSlot));
  3602. }
  3603. #endif
  3604. /**
  3605. * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
  3606. * @ioc: per adapter object
  3607. * @VF_ID:
  3608. * @event_data: event data payload
  3609. * Context: user.
  3610. *
  3611. * Return nothing.
  3612. */
  3613. static void
  3614. _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
  3615. u8 VF_ID, Mpi2EventDataSasEnclDevStatusChange_t *event_data)
  3616. {
  3617. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3618. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  3619. _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
  3620. event_data);
  3621. #endif
  3622. }
  3623. /**
  3624. * _scsih_sas_broadcast_primative_event - handle broadcast events
  3625. * @ioc: per adapter object
  3626. * @event_data: event data payload
  3627. * Context: user.
  3628. *
  3629. * Return nothing.
  3630. */
  3631. static void
  3632. _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  3633. Mpi2EventDataSasBroadcastPrimitive_t *event_data)
  3634. {
  3635. struct scsi_cmnd *scmd;
  3636. u16 smid, handle;
  3637. u32 lun;
  3638. struct MPT2SAS_DEVICE *sas_device_priv_data;
  3639. u32 termination_count;
  3640. u32 query_count;
  3641. Mpi2SCSITaskManagementReply_t *mpi_reply;
  3642. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
  3643. "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
  3644. event_data->PortWidth));
  3645. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
  3646. __func__));
  3647. mutex_lock(&ioc->tm_cmds.mutex);
  3648. termination_count = 0;
  3649. query_count = 0;
  3650. mpi_reply = ioc->tm_cmds.reply;
  3651. for (smid = 1; smid <= ioc->request_depth; smid++) {
  3652. scmd = _scsih_scsi_lookup_get(ioc, smid);
  3653. if (!scmd)
  3654. continue;
  3655. sas_device_priv_data = scmd->device->hostdata;
  3656. if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
  3657. continue;
  3658. /* skip hidden raid components */
  3659. if (sas_device_priv_data->sas_target->flags &
  3660. MPT_TARGET_FLAGS_RAID_COMPONENT)
  3661. continue;
  3662. /* skip volumes */
  3663. if (sas_device_priv_data->sas_target->flags &
  3664. MPT_TARGET_FLAGS_VOLUME)
  3665. continue;
  3666. handle = sas_device_priv_data->sas_target->handle;
  3667. lun = sas_device_priv_data->lun;
  3668. query_count++;
  3669. mpt2sas_scsih_issue_tm(ioc, handle, lun,
  3670. MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
  3671. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  3672. if ((mpi_reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) &&
  3673. (mpi_reply->ResponseCode ==
  3674. MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
  3675. mpi_reply->ResponseCode ==
  3676. MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
  3677. continue;
  3678. mpt2sas_scsih_issue_tm(ioc, handle, lun,
  3679. MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30);
  3680. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  3681. termination_count += le32_to_cpu(mpi_reply->TerminationCount);
  3682. }
  3683. ioc->broadcast_aen_busy = 0;
  3684. mutex_unlock(&ioc->tm_cmds.mutex);
  3685. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  3686. "%s - exit, query_count = %d termination_count = %d\n",
  3687. ioc->name, __func__, query_count, termination_count));
  3688. }
  3689. /**
  3690. * _scsih_sas_discovery_event - handle discovery events
  3691. * @ioc: per adapter object
  3692. * @event_data: event data payload
  3693. * Context: user.
  3694. *
  3695. * Return nothing.
  3696. */
  3697. static void
  3698. _scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  3699. Mpi2EventDataSasDiscovery_t *event_data)
  3700. {
  3701. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3702. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
  3703. printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
  3704. (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
  3705. "start" : "stop");
  3706. if (event_data->DiscoveryStatus)
  3707. printk(MPT2SAS_DEBUG_FMT ", discovery_status(0x%08x)",
  3708. ioc->name, le32_to_cpu(event_data->DiscoveryStatus));
  3709. printk("\n");
  3710. }
  3711. #endif
  3712. if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
  3713. !ioc->sas_hba.num_phys)
  3714. _scsih_sas_host_add(ioc);
  3715. }
  3716. /**
  3717. * _scsih_reprobe_lun - reprobing lun
  3718. * @sdev: scsi device struct
  3719. * @no_uld_attach: sdev->no_uld_attach flag setting
  3720. *
  3721. **/
  3722. static void
  3723. _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
  3724. {
  3725. int rc;
  3726. sdev->no_uld_attach = no_uld_attach ? 1 : 0;
  3727. sdev_printk(KERN_INFO, sdev, "%s raid component\n",
  3728. sdev->no_uld_attach ? "hidding" : "exposing");
  3729. rc = scsi_device_reprobe(sdev);
  3730. }
  3731. /**
  3732. * _scsih_reprobe_target - reprobing target
  3733. * @starget: scsi target struct
  3734. * @no_uld_attach: sdev->no_uld_attach flag setting
  3735. *
  3736. * Note: no_uld_attach flag determines whether the disk device is attached
  3737. * to block layer. A value of `1` means to not attach.
  3738. **/
  3739. static void
  3740. _scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
  3741. {
  3742. struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
  3743. if (no_uld_attach)
  3744. sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
  3745. else
  3746. sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  3747. starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
  3748. _scsih_reprobe_lun);
  3749. }
  3750. /**
  3751. * _scsih_sas_volume_add - add new volume
  3752. * @ioc: per adapter object
  3753. * @element: IR config element data
  3754. * Context: user.
  3755. *
  3756. * Return nothing.
  3757. */
  3758. static void
  3759. _scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
  3760. Mpi2EventIrConfigElement_t *element)
  3761. {
  3762. struct _raid_device *raid_device;
  3763. unsigned long flags;
  3764. u64 wwid;
  3765. u16 handle = le16_to_cpu(element->VolDevHandle);
  3766. int rc;
  3767. #if 0 /* RAID_HACKS */
  3768. if (le32_to_cpu(event_data->Flags) &
  3769. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
  3770. return;
  3771. #endif
  3772. mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
  3773. if (!wwid) {
  3774. printk(MPT2SAS_ERR_FMT
  3775. "failure at %s:%d/%s()!\n", ioc->name,
  3776. __FILE__, __LINE__, __func__);
  3777. return;
  3778. }
  3779. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  3780. raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
  3781. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  3782. if (raid_device)
  3783. return;
  3784. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  3785. if (!raid_device) {
  3786. printk(MPT2SAS_ERR_FMT
  3787. "failure at %s:%d/%s()!\n", ioc->name,
  3788. __FILE__, __LINE__, __func__);
  3789. return;
  3790. }
  3791. raid_device->id = ioc->sas_id++;
  3792. raid_device->channel = RAID_CHANNEL;
  3793. raid_device->handle = handle;
  3794. raid_device->wwid = wwid;
  3795. _scsih_raid_device_add(ioc, raid_device);
  3796. if (!ioc->wait_for_port_enable_to_complete) {
  3797. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  3798. raid_device->id, 0);
  3799. if (rc)
  3800. _scsih_raid_device_remove(ioc, raid_device);
  3801. } else
  3802. _scsih_determine_boot_device(ioc, raid_device, 1);
  3803. }
  3804. /**
  3805. * _scsih_sas_volume_delete - delete volume
  3806. * @ioc: per adapter object
  3807. * @element: IR config element data
  3808. * Context: user.
  3809. *
  3810. * Return nothing.
  3811. */
  3812. static void
  3813. _scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
  3814. Mpi2EventIrConfigElement_t *element)
  3815. {
  3816. struct _raid_device *raid_device;
  3817. u16 handle = le16_to_cpu(element->VolDevHandle);
  3818. unsigned long flags;
  3819. struct MPT2SAS_TARGET *sas_target_priv_data;
  3820. #if 0 /* RAID_HACKS */
  3821. if (le32_to_cpu(event_data->Flags) &
  3822. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
  3823. return;
  3824. #endif
  3825. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  3826. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  3827. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  3828. if (!raid_device)
  3829. return;
  3830. if (raid_device->starget) {
  3831. sas_target_priv_data = raid_device->starget->hostdata;
  3832. sas_target_priv_data->deleted = 1;
  3833. scsi_remove_target(&raid_device->starget->dev);
  3834. }
  3835. _scsih_raid_device_remove(ioc, raid_device);
  3836. }
  3837. /**
  3838. * _scsih_sas_pd_expose - expose pd component to /dev/sdX
  3839. * @ioc: per adapter object
  3840. * @element: IR config element data
  3841. * Context: user.
  3842. *
  3843. * Return nothing.
  3844. */
  3845. static void
  3846. _scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
  3847. Mpi2EventIrConfigElement_t *element)
  3848. {
  3849. struct _sas_device *sas_device;
  3850. unsigned long flags;
  3851. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  3852. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3853. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3854. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3855. if (!sas_device)
  3856. return;
  3857. /* exposing raid component */
  3858. sas_device->volume_handle = 0;
  3859. sas_device->volume_wwid = 0;
  3860. sas_device->hidden_raid_component = 0;
  3861. _scsih_reprobe_target(sas_device->starget, 0);
  3862. }
  3863. /**
  3864. * _scsih_sas_pd_hide - hide pd component from /dev/sdX
  3865. * @ioc: per adapter object
  3866. * @element: IR config element data
  3867. * Context: user.
  3868. *
  3869. * Return nothing.
  3870. */
  3871. static void
  3872. _scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
  3873. Mpi2EventIrConfigElement_t *element)
  3874. {
  3875. struct _sas_device *sas_device;
  3876. unsigned long flags;
  3877. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  3878. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3879. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3880. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3881. if (!sas_device)
  3882. return;
  3883. /* hiding raid component */
  3884. mpt2sas_config_get_volume_handle(ioc, handle,
  3885. &sas_device->volume_handle);
  3886. mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
  3887. &sas_device->volume_wwid);
  3888. sas_device->hidden_raid_component = 1;
  3889. _scsih_reprobe_target(sas_device->starget, 1);
  3890. }
  3891. /**
  3892. * _scsih_sas_pd_delete - delete pd component
  3893. * @ioc: per adapter object
  3894. * @element: IR config element data
  3895. * Context: user.
  3896. *
  3897. * Return nothing.
  3898. */
  3899. static void
  3900. _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
  3901. Mpi2EventIrConfigElement_t *element)
  3902. {
  3903. struct _sas_device *sas_device;
  3904. unsigned long flags;
  3905. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  3906. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3907. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3908. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3909. if (!sas_device)
  3910. return;
  3911. _scsih_remove_device(ioc, handle);
  3912. }
  3913. /**
  3914. * _scsih_sas_pd_add - remove pd component
  3915. * @ioc: per adapter object
  3916. * @element: IR config element data
  3917. * Context: user.
  3918. *
  3919. * Return nothing.
  3920. */
  3921. static void
  3922. _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
  3923. Mpi2EventIrConfigElement_t *element)
  3924. {
  3925. struct _sas_device *sas_device;
  3926. unsigned long flags;
  3927. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  3928. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3929. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3930. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3931. if (sas_device)
  3932. sas_device->hidden_raid_component = 1;
  3933. else
  3934. _scsih_add_device(ioc, handle, 0, 1);
  3935. }
  3936. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3937. /**
  3938. * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
  3939. * @ioc: per adapter object
  3940. * @event_data: event data payload
  3941. * Context: user.
  3942. *
  3943. * Return nothing.
  3944. */
  3945. static void
  3946. _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  3947. Mpi2EventDataIrConfigChangeList_t *event_data)
  3948. {
  3949. Mpi2EventIrConfigElement_t *element;
  3950. u8 element_type;
  3951. int i;
  3952. char *reason_str = NULL, *element_str = NULL;
  3953. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  3954. printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
  3955. ioc->name, (le32_to_cpu(event_data->Flags) &
  3956. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
  3957. "foreign" : "native", event_data->NumElements);
  3958. for (i = 0; i < event_data->NumElements; i++, element++) {
  3959. switch (element->ReasonCode) {
  3960. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  3961. reason_str = "add";
  3962. break;
  3963. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  3964. reason_str = "remove";
  3965. break;
  3966. case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
  3967. reason_str = "no change";
  3968. break;
  3969. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  3970. reason_str = "hide";
  3971. break;
  3972. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  3973. reason_str = "unhide";
  3974. break;
  3975. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  3976. reason_str = "volume_created";
  3977. break;
  3978. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  3979. reason_str = "volume_deleted";
  3980. break;
  3981. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  3982. reason_str = "pd_created";
  3983. break;
  3984. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  3985. reason_str = "pd_deleted";
  3986. break;
  3987. default:
  3988. reason_str = "unknown reason";
  3989. break;
  3990. }
  3991. element_type = le16_to_cpu(element->ElementFlags) &
  3992. MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
  3993. switch (element_type) {
  3994. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
  3995. element_str = "volume";
  3996. break;
  3997. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
  3998. element_str = "phys disk";
  3999. break;
  4000. case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
  4001. element_str = "hot spare";
  4002. break;
  4003. default:
  4004. element_str = "unknown element";
  4005. break;
  4006. }
  4007. printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
  4008. "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
  4009. reason_str, le16_to_cpu(element->VolDevHandle),
  4010. le16_to_cpu(element->PhysDiskDevHandle),
  4011. element->PhysDiskNum);
  4012. }
  4013. }
  4014. #endif
  4015. /**
  4016. * _scsih_sas_ir_config_change_event - handle ir configuration change events
  4017. * @ioc: per adapter object
  4018. * @VF_ID:
  4019. * @event_data: event data payload
  4020. * Context: user.
  4021. *
  4022. * Return nothing.
  4023. */
  4024. static void
  4025. _scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  4026. Mpi2EventDataIrConfigChangeList_t *event_data)
  4027. {
  4028. Mpi2EventIrConfigElement_t *element;
  4029. int i;
  4030. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4031. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  4032. _scsih_sas_ir_config_change_event_debug(ioc, event_data);
  4033. #endif
  4034. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  4035. for (i = 0; i < event_data->NumElements; i++, element++) {
  4036. switch (element->ReasonCode) {
  4037. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  4038. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  4039. _scsih_sas_volume_add(ioc, element);
  4040. break;
  4041. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  4042. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  4043. _scsih_sas_volume_delete(ioc, element);
  4044. break;
  4045. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  4046. _scsih_sas_pd_hide(ioc, element);
  4047. break;
  4048. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  4049. _scsih_sas_pd_expose(ioc, element);
  4050. break;
  4051. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  4052. _scsih_sas_pd_add(ioc, element);
  4053. break;
  4054. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  4055. _scsih_sas_pd_delete(ioc, element);
  4056. break;
  4057. }
  4058. }
  4059. }
  4060. /**
  4061. * _scsih_sas_ir_volume_event - IR volume event
  4062. * @ioc: per adapter object
  4063. * @event_data: event data payload
  4064. * Context: user.
  4065. *
  4066. * Return nothing.
  4067. */
  4068. static void
  4069. _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  4070. Mpi2EventDataIrVolume_t *event_data)
  4071. {
  4072. u64 wwid;
  4073. unsigned long flags;
  4074. struct _raid_device *raid_device;
  4075. u16 handle;
  4076. u32 state;
  4077. int rc;
  4078. struct MPT2SAS_TARGET *sas_target_priv_data;
  4079. if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
  4080. return;
  4081. handle = le16_to_cpu(event_data->VolDevHandle);
  4082. state = le32_to_cpu(event_data->NewValue);
  4083. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
  4084. "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
  4085. le32_to_cpu(event_data->PreviousValue), state));
  4086. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4087. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  4088. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4089. switch (state) {
  4090. case MPI2_RAID_VOL_STATE_MISSING:
  4091. case MPI2_RAID_VOL_STATE_FAILED:
  4092. if (!raid_device)
  4093. break;
  4094. if (raid_device->starget) {
  4095. sas_target_priv_data = raid_device->starget->hostdata;
  4096. sas_target_priv_data->deleted = 1;
  4097. scsi_remove_target(&raid_device->starget->dev);
  4098. }
  4099. _scsih_raid_device_remove(ioc, raid_device);
  4100. break;
  4101. case MPI2_RAID_VOL_STATE_ONLINE:
  4102. case MPI2_RAID_VOL_STATE_DEGRADED:
  4103. case MPI2_RAID_VOL_STATE_OPTIMAL:
  4104. if (raid_device)
  4105. break;
  4106. mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
  4107. if (!wwid) {
  4108. printk(MPT2SAS_ERR_FMT
  4109. "failure at %s:%d/%s()!\n", ioc->name,
  4110. __FILE__, __LINE__, __func__);
  4111. break;
  4112. }
  4113. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  4114. if (!raid_device) {
  4115. printk(MPT2SAS_ERR_FMT
  4116. "failure at %s:%d/%s()!\n", ioc->name,
  4117. __FILE__, __LINE__, __func__);
  4118. break;
  4119. }
  4120. raid_device->id = ioc->sas_id++;
  4121. raid_device->channel = RAID_CHANNEL;
  4122. raid_device->handle = handle;
  4123. raid_device->wwid = wwid;
  4124. _scsih_raid_device_add(ioc, raid_device);
  4125. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  4126. raid_device->id, 0);
  4127. if (rc)
  4128. _scsih_raid_device_remove(ioc, raid_device);
  4129. break;
  4130. case MPI2_RAID_VOL_STATE_INITIALIZING:
  4131. default:
  4132. break;
  4133. }
  4134. }
  4135. /**
  4136. * _scsih_sas_ir_physical_disk_event - PD event
  4137. * @ioc: per adapter object
  4138. * @event_data: event data payload
  4139. * Context: user.
  4140. *
  4141. * Return nothing.
  4142. */
  4143. static void
  4144. _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  4145. Mpi2EventDataIrPhysicalDisk_t *event_data)
  4146. {
  4147. u16 handle;
  4148. u32 state;
  4149. struct _sas_device *sas_device;
  4150. unsigned long flags;
  4151. if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
  4152. return;
  4153. handle = le16_to_cpu(event_data->PhysDiskDevHandle);
  4154. state = le32_to_cpu(event_data->NewValue);
  4155. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
  4156. "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
  4157. le32_to_cpu(event_data->PreviousValue), state));
  4158. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4159. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4160. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4161. switch (state) {
  4162. #if 0
  4163. case MPI2_RAID_PD_STATE_OFFLINE:
  4164. if (sas_device)
  4165. _scsih_remove_device(ioc, handle);
  4166. break;
  4167. #endif
  4168. case MPI2_RAID_PD_STATE_ONLINE:
  4169. case MPI2_RAID_PD_STATE_DEGRADED:
  4170. case MPI2_RAID_PD_STATE_REBUILDING:
  4171. case MPI2_RAID_PD_STATE_OPTIMAL:
  4172. if (sas_device)
  4173. sas_device->hidden_raid_component = 1;
  4174. else
  4175. _scsih_add_device(ioc, handle, 0, 1);
  4176. break;
  4177. case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
  4178. case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
  4179. case MPI2_RAID_PD_STATE_HOT_SPARE:
  4180. default:
  4181. break;
  4182. }
  4183. }
  4184. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4185. /**
  4186. * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
  4187. * @ioc: per adapter object
  4188. * @event_data: event data payload
  4189. * Context: user.
  4190. *
  4191. * Return nothing.
  4192. */
  4193. static void
  4194. _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
  4195. Mpi2EventDataIrOperationStatus_t *event_data)
  4196. {
  4197. char *reason_str = NULL;
  4198. switch (event_data->RAIDOperation) {
  4199. case MPI2_EVENT_IR_RAIDOP_RESYNC:
  4200. reason_str = "resync";
  4201. break;
  4202. case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
  4203. reason_str = "online capacity expansion";
  4204. break;
  4205. case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
  4206. reason_str = "consistency check";
  4207. break;
  4208. default:
  4209. reason_str = "unknown reason";
  4210. break;
  4211. }
  4212. printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
  4213. "\thandle(0x%04x), percent complete(%d)\n",
  4214. ioc->name, reason_str,
  4215. le16_to_cpu(event_data->VolDevHandle),
  4216. event_data->PercentComplete);
  4217. }
  4218. #endif
  4219. /**
  4220. * _scsih_sas_ir_operation_status_event - handle RAID operation events
  4221. * @ioc: per adapter object
  4222. * @VF_ID:
  4223. * @event_data: event data payload
  4224. * Context: user.
  4225. *
  4226. * Return nothing.
  4227. */
  4228. static void
  4229. _scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  4230. Mpi2EventDataIrOperationStatus_t *event_data)
  4231. {
  4232. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4233. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  4234. _scsih_sas_ir_operation_status_event_debug(ioc, event_data);
  4235. #endif
  4236. }
  4237. /**
  4238. * _scsih_task_set_full - handle task set full
  4239. * @ioc: per adapter object
  4240. * @event_data: event data payload
  4241. * Context: user.
  4242. *
  4243. * Throttle back qdepth.
  4244. */
  4245. static void
  4246. _scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  4247. Mpi2EventDataTaskSetFull_t *event_data)
  4248. {
  4249. unsigned long flags;
  4250. struct _sas_device *sas_device;
  4251. static struct _raid_device *raid_device;
  4252. struct scsi_device *sdev;
  4253. int depth;
  4254. u16 current_depth;
  4255. u16 handle;
  4256. int id, channel;
  4257. u64 sas_address;
  4258. current_depth = le16_to_cpu(event_data->CurrentDepth);
  4259. handle = le16_to_cpu(event_data->DevHandle);
  4260. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4261. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4262. if (!sas_device) {
  4263. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4264. return;
  4265. }
  4266. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4267. id = sas_device->id;
  4268. channel = sas_device->channel;
  4269. sas_address = sas_device->sas_address;
  4270. /* if hidden raid component, then change to volume characteristics */
  4271. if (sas_device->hidden_raid_component && sas_device->volume_handle) {
  4272. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4273. raid_device = _scsih_raid_device_find_by_handle(
  4274. ioc, sas_device->volume_handle);
  4275. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4276. if (raid_device) {
  4277. id = raid_device->id;
  4278. channel = raid_device->channel;
  4279. handle = raid_device->handle;
  4280. sas_address = raid_device->wwid;
  4281. }
  4282. }
  4283. if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
  4284. starget_printk(KERN_DEBUG, sas_device->starget, "task set "
  4285. "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
  4286. handle, (unsigned long long)sas_address, current_depth);
  4287. shost_for_each_device(sdev, ioc->shost) {
  4288. if (sdev->id == id && sdev->channel == channel) {
  4289. if (current_depth > sdev->queue_depth) {
  4290. if (ioc->logging_level &
  4291. MPT_DEBUG_TASK_SET_FULL)
  4292. sdev_printk(KERN_INFO, sdev, "strange "
  4293. "observation, the queue depth is"
  4294. " (%d) meanwhile fw queue depth "
  4295. "is (%d)\n", sdev->queue_depth,
  4296. current_depth);
  4297. continue;
  4298. }
  4299. depth = scsi_track_queue_full(sdev,
  4300. current_depth - 1);
  4301. if (depth > 0)
  4302. sdev_printk(KERN_INFO, sdev, "Queue depth "
  4303. "reduced to (%d)\n", depth);
  4304. else if (depth < 0)
  4305. sdev_printk(KERN_INFO, sdev, "Tagged Command "
  4306. "Queueing is being disabled\n");
  4307. else if (depth == 0)
  4308. if (ioc->logging_level &
  4309. MPT_DEBUG_TASK_SET_FULL)
  4310. sdev_printk(KERN_INFO, sdev,
  4311. "Queue depth not changed yet\n");
  4312. }
  4313. }
  4314. }
  4315. /**
  4316. * _scsih_mark_responding_sas_device - mark a sas_devices as responding
  4317. * @ioc: per adapter object
  4318. * @sas_address: sas address
  4319. * @slot: enclosure slot id
  4320. * @handle: device handle
  4321. *
  4322. * After host reset, find out whether devices are still responding.
  4323. * Used in _scsi_remove_unresponsive_sas_devices.
  4324. *
  4325. * Return nothing.
  4326. */
  4327. static void
  4328. _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  4329. u16 slot, u16 handle)
  4330. {
  4331. struct MPT2SAS_TARGET *sas_target_priv_data;
  4332. struct scsi_target *starget;
  4333. struct _sas_device *sas_device;
  4334. unsigned long flags;
  4335. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4336. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  4337. if (sas_device->sas_address == sas_address &&
  4338. sas_device->slot == slot && sas_device->starget) {
  4339. sas_device->responding = 1;
  4340. starget_printk(KERN_INFO, sas_device->starget,
  4341. "handle(0x%04x), sas_addr(0x%016llx), enclosure "
  4342. "logical id(0x%016llx), slot(%d)\n", handle,
  4343. (unsigned long long)sas_device->sas_address,
  4344. (unsigned long long)
  4345. sas_device->enclosure_logical_id,
  4346. sas_device->slot);
  4347. if (sas_device->handle == handle)
  4348. goto out;
  4349. printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
  4350. sas_device->handle);
  4351. sas_device->handle = handle;
  4352. starget = sas_device->starget;
  4353. sas_target_priv_data = starget->hostdata;
  4354. sas_target_priv_data->handle = handle;
  4355. goto out;
  4356. }
  4357. }
  4358. out:
  4359. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4360. }
  4361. /**
  4362. * _scsih_search_responding_sas_devices -
  4363. * @ioc: per adapter object
  4364. *
  4365. * After host reset, find out whether devices are still responding.
  4366. * If not remove.
  4367. *
  4368. * Return nothing.
  4369. */
  4370. static void
  4371. _scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
  4372. {
  4373. Mpi2SasDevicePage0_t sas_device_pg0;
  4374. Mpi2ConfigReply_t mpi_reply;
  4375. u16 ioc_status;
  4376. __le64 sas_address;
  4377. u16 handle;
  4378. u32 device_info;
  4379. u16 slot;
  4380. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
  4381. if (list_empty(&ioc->sas_device_list))
  4382. return;
  4383. handle = 0xFFFF;
  4384. while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  4385. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
  4386. handle))) {
  4387. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4388. MPI2_IOCSTATUS_MASK;
  4389. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  4390. break;
  4391. handle = le16_to_cpu(sas_device_pg0.DevHandle);
  4392. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  4393. if (!(_scsih_is_end_device(device_info)))
  4394. continue;
  4395. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  4396. slot = le16_to_cpu(sas_device_pg0.Slot);
  4397. _scsih_mark_responding_sas_device(ioc, sas_address, slot,
  4398. handle);
  4399. }
  4400. }
  4401. /**
  4402. * _scsih_mark_responding_raid_device - mark a raid_device as responding
  4403. * @ioc: per adapter object
  4404. * @wwid: world wide identifier for raid volume
  4405. * @handle: device handle
  4406. *
  4407. * After host reset, find out whether devices are still responding.
  4408. * Used in _scsi_remove_unresponsive_raid_devices.
  4409. *
  4410. * Return nothing.
  4411. */
  4412. static void
  4413. _scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
  4414. u16 handle)
  4415. {
  4416. struct MPT2SAS_TARGET *sas_target_priv_data;
  4417. struct scsi_target *starget;
  4418. struct _raid_device *raid_device;
  4419. unsigned long flags;
  4420. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4421. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  4422. if (raid_device->wwid == wwid && raid_device->starget) {
  4423. raid_device->responding = 1;
  4424. starget_printk(KERN_INFO, raid_device->starget,
  4425. "handle(0x%04x), wwid(0x%016llx)\n", handle,
  4426. (unsigned long long)raid_device->wwid);
  4427. if (raid_device->handle == handle)
  4428. goto out;
  4429. printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
  4430. raid_device->handle);
  4431. raid_device->handle = handle;
  4432. starget = raid_device->starget;
  4433. sas_target_priv_data = starget->hostdata;
  4434. sas_target_priv_data->handle = handle;
  4435. goto out;
  4436. }
  4437. }
  4438. out:
  4439. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4440. }
  4441. /**
  4442. * _scsih_search_responding_raid_devices -
  4443. * @ioc: per adapter object
  4444. *
  4445. * After host reset, find out whether devices are still responding.
  4446. * If not remove.
  4447. *
  4448. * Return nothing.
  4449. */
  4450. static void
  4451. _scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
  4452. {
  4453. Mpi2RaidVolPage1_t volume_pg1;
  4454. Mpi2ConfigReply_t mpi_reply;
  4455. u16 ioc_status;
  4456. u16 handle;
  4457. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
  4458. if (list_empty(&ioc->raid_device_list))
  4459. return;
  4460. handle = 0xFFFF;
  4461. while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
  4462. &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  4463. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4464. MPI2_IOCSTATUS_MASK;
  4465. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  4466. break;
  4467. handle = le16_to_cpu(volume_pg1.DevHandle);
  4468. _scsih_mark_responding_raid_device(ioc,
  4469. le64_to_cpu(volume_pg1.WWID), handle);
  4470. }
  4471. }
  4472. /**
  4473. * _scsih_mark_responding_expander - mark a expander as responding
  4474. * @ioc: per adapter object
  4475. * @sas_address: sas address
  4476. * @handle:
  4477. *
  4478. * After host reset, find out whether devices are still responding.
  4479. * Used in _scsi_remove_unresponsive_expanders.
  4480. *
  4481. * Return nothing.
  4482. */
  4483. static void
  4484. _scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  4485. u16 handle)
  4486. {
  4487. struct _sas_node *sas_expander;
  4488. unsigned long flags;
  4489. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  4490. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  4491. if (sas_expander->sas_address == sas_address) {
  4492. sas_expander->responding = 1;
  4493. if (sas_expander->handle != handle) {
  4494. printk(KERN_INFO "old handle(0x%04x)\n",
  4495. sas_expander->handle);
  4496. sas_expander->handle = handle;
  4497. }
  4498. goto out;
  4499. }
  4500. }
  4501. out:
  4502. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  4503. }
  4504. /**
  4505. * _scsih_search_responding_expanders -
  4506. * @ioc: per adapter object
  4507. *
  4508. * After host reset, find out whether devices are still responding.
  4509. * If not remove.
  4510. *
  4511. * Return nothing.
  4512. */
  4513. static void
  4514. _scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
  4515. {
  4516. Mpi2ExpanderPage0_t expander_pg0;
  4517. Mpi2ConfigReply_t mpi_reply;
  4518. u16 ioc_status;
  4519. __le64 sas_address;
  4520. u16 handle;
  4521. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
  4522. if (list_empty(&ioc->sas_expander_list))
  4523. return;
  4524. handle = 0xFFFF;
  4525. while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  4526. MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
  4527. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4528. MPI2_IOCSTATUS_MASK;
  4529. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  4530. break;
  4531. handle = le16_to_cpu(expander_pg0.DevHandle);
  4532. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  4533. printk(KERN_INFO "\texpander present: handle(0x%04x), "
  4534. "sas_addr(0x%016llx)\n", handle,
  4535. (unsigned long long)sas_address);
  4536. _scsih_mark_responding_expander(ioc, sas_address, handle);
  4537. }
  4538. }
  4539. /**
  4540. * _scsih_remove_unresponding_devices - removing unresponding devices
  4541. * @ioc: per adapter object
  4542. *
  4543. * Return nothing.
  4544. */
  4545. static void
  4546. _scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
  4547. {
  4548. struct _sas_device *sas_device, *sas_device_next;
  4549. struct _sas_node *sas_expander, *sas_expander_next;
  4550. struct _raid_device *raid_device, *raid_device_next;
  4551. unsigned long flags;
  4552. _scsih_search_responding_sas_devices(ioc);
  4553. _scsih_search_responding_raid_devices(ioc);
  4554. _scsih_search_responding_expanders(ioc);
  4555. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  4556. ioc->shost_recovery = 0;
  4557. if (ioc->shost->shost_state == SHOST_RECOVERY) {
  4558. printk(MPT2SAS_INFO_FMT "putting controller into "
  4559. "SHOST_RUNNING\n", ioc->name);
  4560. scsi_host_set_state(ioc->shost, SHOST_RUNNING);
  4561. }
  4562. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  4563. list_for_each_entry_safe(sas_device, sas_device_next,
  4564. &ioc->sas_device_list, list) {
  4565. if (sas_device->responding) {
  4566. sas_device->responding = 0;
  4567. continue;
  4568. }
  4569. if (sas_device->starget)
  4570. starget_printk(KERN_INFO, sas_device->starget,
  4571. "removing: handle(0x%04x), sas_addr(0x%016llx), "
  4572. "enclosure logical id(0x%016llx), slot(%d)\n",
  4573. sas_device->handle,
  4574. (unsigned long long)sas_device->sas_address,
  4575. (unsigned long long)
  4576. sas_device->enclosure_logical_id,
  4577. sas_device->slot);
  4578. _scsih_remove_device(ioc, sas_device->handle);
  4579. }
  4580. list_for_each_entry_safe(raid_device, raid_device_next,
  4581. &ioc->raid_device_list, list) {
  4582. if (raid_device->responding) {
  4583. raid_device->responding = 0;
  4584. continue;
  4585. }
  4586. if (raid_device->starget) {
  4587. starget_printk(KERN_INFO, raid_device->starget,
  4588. "removing: handle(0x%04x), wwid(0x%016llx)\n",
  4589. raid_device->handle,
  4590. (unsigned long long)raid_device->wwid);
  4591. scsi_remove_target(&raid_device->starget->dev);
  4592. }
  4593. _scsih_raid_device_remove(ioc, raid_device);
  4594. }
  4595. list_for_each_entry_safe(sas_expander, sas_expander_next,
  4596. &ioc->sas_expander_list, list) {
  4597. if (sas_expander->responding) {
  4598. sas_expander->responding = 0;
  4599. continue;
  4600. }
  4601. printk("\tremoving expander: handle(0x%04x), "
  4602. " sas_addr(0x%016llx)\n", sas_expander->handle,
  4603. (unsigned long long)sas_expander->sas_address);
  4604. _scsih_expander_remove(ioc, sas_expander->handle);
  4605. }
  4606. }
  4607. /**
  4608. * _firmware_event_work - delayed task for processing firmware events
  4609. * @ioc: per adapter object
  4610. * @work: equal to the fw_event_work object
  4611. * Context: user.
  4612. *
  4613. * Return nothing.
  4614. */
  4615. static void
  4616. _firmware_event_work(struct work_struct *work)
  4617. {
  4618. struct fw_event_work *fw_event = container_of(work,
  4619. struct fw_event_work, work);
  4620. unsigned long flags;
  4621. struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
  4622. /* This is invoked by calling _scsih_queue_rescan(). */
  4623. if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
  4624. _scsih_fw_event_free(ioc, fw_event);
  4625. _scsih_sas_host_refresh(ioc, 1);
  4626. _scsih_remove_unresponding_devices(ioc);
  4627. return;
  4628. }
  4629. /* the queue is being flushed so ignore this event */
  4630. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  4631. if (ioc->fw_events_off || ioc->remove_host) {
  4632. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  4633. _scsih_fw_event_free(ioc, fw_event);
  4634. return;
  4635. }
  4636. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  4637. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  4638. if (ioc->shost_recovery) {
  4639. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  4640. _scsih_fw_event_requeue(ioc, fw_event, 1000);
  4641. return;
  4642. }
  4643. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  4644. switch (fw_event->event) {
  4645. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  4646. _scsih_sas_topology_change_event(ioc, fw_event->VF_ID,
  4647. fw_event->event_data, fw_event);
  4648. break;
  4649. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  4650. _scsih_sas_device_status_change_event(ioc, fw_event->VF_ID,
  4651. fw_event->event_data);
  4652. break;
  4653. case MPI2_EVENT_SAS_DISCOVERY:
  4654. _scsih_sas_discovery_event(ioc, fw_event->VF_ID,
  4655. fw_event->event_data);
  4656. break;
  4657. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  4658. _scsih_sas_broadcast_primative_event(ioc, fw_event->VF_ID,
  4659. fw_event->event_data);
  4660. break;
  4661. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  4662. _scsih_sas_enclosure_dev_status_change_event(ioc,
  4663. fw_event->VF_ID, fw_event->event_data);
  4664. break;
  4665. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  4666. _scsih_sas_ir_config_change_event(ioc, fw_event->VF_ID,
  4667. fw_event->event_data);
  4668. break;
  4669. case MPI2_EVENT_IR_VOLUME:
  4670. _scsih_sas_ir_volume_event(ioc, fw_event->VF_ID,
  4671. fw_event->event_data);
  4672. break;
  4673. case MPI2_EVENT_IR_PHYSICAL_DISK:
  4674. _scsih_sas_ir_physical_disk_event(ioc, fw_event->VF_ID,
  4675. fw_event->event_data);
  4676. break;
  4677. case MPI2_EVENT_IR_OPERATION_STATUS:
  4678. _scsih_sas_ir_operation_status_event(ioc, fw_event->VF_ID,
  4679. fw_event->event_data);
  4680. break;
  4681. case MPI2_EVENT_TASK_SET_FULL:
  4682. _scsih_task_set_full(ioc, fw_event->VF_ID,
  4683. fw_event->event_data);
  4684. break;
  4685. }
  4686. _scsih_fw_event_free(ioc, fw_event);
  4687. }
  4688. /**
  4689. * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
  4690. * @ioc: per adapter object
  4691. * @VF_ID: virtual function id
  4692. * @reply: reply message frame(lower 32bit addr)
  4693. * Context: interrupt.
  4694. *
  4695. * This function merely adds a new work task into ioc->firmware_event_thread.
  4696. * The tasks are worked from _firmware_event_work in user context.
  4697. *
  4698. * Return nothing.
  4699. */
  4700. void
  4701. mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, u32 reply)
  4702. {
  4703. struct fw_event_work *fw_event;
  4704. Mpi2EventNotificationReply_t *mpi_reply;
  4705. unsigned long flags;
  4706. u16 event;
  4707. /* events turned off due to host reset or driver unloading */
  4708. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  4709. if (ioc->fw_events_off || ioc->remove_host) {
  4710. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  4711. return;
  4712. }
  4713. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  4714. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  4715. event = le16_to_cpu(mpi_reply->Event);
  4716. switch (event) {
  4717. /* handle these */
  4718. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  4719. {
  4720. Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
  4721. (Mpi2EventDataSasBroadcastPrimitive_t *)
  4722. mpi_reply->EventData;
  4723. if (baen_data->Primitive !=
  4724. MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
  4725. ioc->broadcast_aen_busy)
  4726. return;
  4727. ioc->broadcast_aen_busy = 1;
  4728. break;
  4729. }
  4730. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  4731. _scsih_check_topo_delete_events(ioc,
  4732. (Mpi2EventDataSasTopologyChangeList_t *)
  4733. mpi_reply->EventData);
  4734. break;
  4735. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  4736. case MPI2_EVENT_IR_OPERATION_STATUS:
  4737. case MPI2_EVENT_SAS_DISCOVERY:
  4738. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  4739. case MPI2_EVENT_IR_VOLUME:
  4740. case MPI2_EVENT_IR_PHYSICAL_DISK:
  4741. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  4742. case MPI2_EVENT_TASK_SET_FULL:
  4743. break;
  4744. default: /* ignore the rest */
  4745. return;
  4746. }
  4747. fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
  4748. if (!fw_event) {
  4749. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4750. ioc->name, __FILE__, __LINE__, __func__);
  4751. return;
  4752. }
  4753. fw_event->event_data =
  4754. kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
  4755. if (!fw_event->event_data) {
  4756. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4757. ioc->name, __FILE__, __LINE__, __func__);
  4758. kfree(fw_event);
  4759. return;
  4760. }
  4761. memcpy(fw_event->event_data, mpi_reply->EventData,
  4762. mpi_reply->EventDataLength*4);
  4763. fw_event->ioc = ioc;
  4764. fw_event->VF_ID = VF_ID;
  4765. fw_event->event = event;
  4766. _scsih_fw_event_add(ioc, fw_event);
  4767. }
  4768. /* shost template */
  4769. static struct scsi_host_template scsih_driver_template = {
  4770. .module = THIS_MODULE,
  4771. .name = "Fusion MPT SAS Host",
  4772. .proc_name = MPT2SAS_DRIVER_NAME,
  4773. .queuecommand = scsih_qcmd,
  4774. .target_alloc = scsih_target_alloc,
  4775. .slave_alloc = scsih_slave_alloc,
  4776. .slave_configure = scsih_slave_configure,
  4777. .target_destroy = scsih_target_destroy,
  4778. .slave_destroy = scsih_slave_destroy,
  4779. .change_queue_depth = scsih_change_queue_depth,
  4780. .change_queue_type = scsih_change_queue_type,
  4781. .eh_abort_handler = scsih_abort,
  4782. .eh_device_reset_handler = scsih_dev_reset,
  4783. .eh_host_reset_handler = scsih_host_reset,
  4784. .bios_param = scsih_bios_param,
  4785. .can_queue = 1,
  4786. .this_id = -1,
  4787. .sg_tablesize = MPT2SAS_SG_DEPTH,
  4788. .max_sectors = 8192,
  4789. .cmd_per_lun = 7,
  4790. .use_clustering = ENABLE_CLUSTERING,
  4791. .shost_attrs = mpt2sas_host_attrs,
  4792. .sdev_attrs = mpt2sas_dev_attrs,
  4793. };
  4794. /**
  4795. * _scsih_expander_node_remove - removing expander device from list.
  4796. * @ioc: per adapter object
  4797. * @sas_expander: the sas_device object
  4798. * Context: Calling function should acquire ioc->sas_node_lock.
  4799. *
  4800. * Removing object and freeing associated memory from the
  4801. * ioc->sas_expander_list.
  4802. *
  4803. * Return nothing.
  4804. */
  4805. static void
  4806. _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
  4807. struct _sas_node *sas_expander)
  4808. {
  4809. struct _sas_port *mpt2sas_port;
  4810. struct _sas_device *sas_device;
  4811. struct _sas_node *expander_sibling;
  4812. unsigned long flags;
  4813. if (!sas_expander)
  4814. return;
  4815. /* remove sibling ports attached to this expander */
  4816. retry_device_search:
  4817. list_for_each_entry(mpt2sas_port,
  4818. &sas_expander->sas_port_list, port_list) {
  4819. if (mpt2sas_port->remote_identify.device_type ==
  4820. SAS_END_DEVICE) {
  4821. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4822. sas_device =
  4823. mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  4824. mpt2sas_port->remote_identify.sas_address);
  4825. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4826. if (!sas_device)
  4827. continue;
  4828. _scsih_remove_device(ioc, sas_device->handle);
  4829. goto retry_device_search;
  4830. }
  4831. }
  4832. retry_expander_search:
  4833. list_for_each_entry(mpt2sas_port,
  4834. &sas_expander->sas_port_list, port_list) {
  4835. if (mpt2sas_port->remote_identify.device_type ==
  4836. MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  4837. mpt2sas_port->remote_identify.device_type ==
  4838. MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
  4839. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  4840. expander_sibling =
  4841. mpt2sas_scsih_expander_find_by_sas_address(
  4842. ioc, mpt2sas_port->remote_identify.sas_address);
  4843. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  4844. if (!expander_sibling)
  4845. continue;
  4846. _scsih_expander_remove(ioc, expander_sibling->handle);
  4847. goto retry_expander_search;
  4848. }
  4849. }
  4850. mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
  4851. sas_expander->parent_handle);
  4852. printk(MPT2SAS_INFO_FMT "expander_remove: handle"
  4853. "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  4854. sas_expander->handle, (unsigned long long)
  4855. sas_expander->sas_address);
  4856. list_del(&sas_expander->list);
  4857. kfree(sas_expander->phy);
  4858. kfree(sas_expander);
  4859. }
  4860. /**
  4861. * scsih_remove - detach and remove add host
  4862. * @pdev: PCI device struct
  4863. *
  4864. * Return nothing.
  4865. */
  4866. static void __devexit
  4867. scsih_remove(struct pci_dev *pdev)
  4868. {
  4869. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  4870. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  4871. struct _sas_port *mpt2sas_port;
  4872. struct _sas_device *sas_device;
  4873. struct _sas_node *expander_sibling;
  4874. struct workqueue_struct *wq;
  4875. unsigned long flags;
  4876. ioc->remove_host = 1;
  4877. _scsih_fw_event_off(ioc);
  4878. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  4879. wq = ioc->firmware_event_thread;
  4880. ioc->firmware_event_thread = NULL;
  4881. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  4882. if (wq)
  4883. destroy_workqueue(wq);
  4884. /* free ports attached to the sas_host */
  4885. retry_again:
  4886. list_for_each_entry(mpt2sas_port,
  4887. &ioc->sas_hba.sas_port_list, port_list) {
  4888. if (mpt2sas_port->remote_identify.device_type ==
  4889. SAS_END_DEVICE) {
  4890. sas_device =
  4891. mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  4892. mpt2sas_port->remote_identify.sas_address);
  4893. if (sas_device) {
  4894. _scsih_remove_device(ioc, sas_device->handle);
  4895. goto retry_again;
  4896. }
  4897. } else {
  4898. expander_sibling =
  4899. mpt2sas_scsih_expander_find_by_sas_address(ioc,
  4900. mpt2sas_port->remote_identify.sas_address);
  4901. if (expander_sibling) {
  4902. _scsih_expander_remove(ioc,
  4903. expander_sibling->handle);
  4904. goto retry_again;
  4905. }
  4906. }
  4907. }
  4908. /* free phys attached to the sas_host */
  4909. if (ioc->sas_hba.num_phys) {
  4910. kfree(ioc->sas_hba.phy);
  4911. ioc->sas_hba.phy = NULL;
  4912. ioc->sas_hba.num_phys = 0;
  4913. }
  4914. sas_remove_host(shost);
  4915. mpt2sas_base_detach(ioc);
  4916. list_del(&ioc->list);
  4917. scsi_remove_host(shost);
  4918. scsi_host_put(shost);
  4919. }
  4920. /**
  4921. * _scsih_probe_boot_devices - reports 1st device
  4922. * @ioc: per adapter object
  4923. *
  4924. * If specified in bios page 2, this routine reports the 1st
  4925. * device scsi-ml or sas transport for persistent boot device
  4926. * purposes. Please refer to function _scsih_determine_boot_device()
  4927. */
  4928. static void
  4929. _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
  4930. {
  4931. u8 is_raid;
  4932. void *device;
  4933. struct _sas_device *sas_device;
  4934. struct _raid_device *raid_device;
  4935. u16 handle, parent_handle;
  4936. u64 sas_address;
  4937. unsigned long flags;
  4938. int rc;
  4939. device = NULL;
  4940. if (ioc->req_boot_device.device) {
  4941. device = ioc->req_boot_device.device;
  4942. is_raid = ioc->req_boot_device.is_raid;
  4943. } else if (ioc->req_alt_boot_device.device) {
  4944. device = ioc->req_alt_boot_device.device;
  4945. is_raid = ioc->req_alt_boot_device.is_raid;
  4946. } else if (ioc->current_boot_device.device) {
  4947. device = ioc->current_boot_device.device;
  4948. is_raid = ioc->current_boot_device.is_raid;
  4949. }
  4950. if (!device)
  4951. return;
  4952. if (is_raid) {
  4953. raid_device = device;
  4954. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  4955. raid_device->id, 0);
  4956. if (rc)
  4957. _scsih_raid_device_remove(ioc, raid_device);
  4958. } else {
  4959. sas_device = device;
  4960. handle = sas_device->handle;
  4961. parent_handle = sas_device->parent_handle;
  4962. sas_address = sas_device->sas_address;
  4963. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4964. list_move_tail(&sas_device->list, &ioc->sas_device_list);
  4965. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4966. if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
  4967. sas_device->parent_handle)) {
  4968. _scsih_sas_device_remove(ioc, sas_device);
  4969. } else if (!sas_device->starget) {
  4970. mpt2sas_transport_port_remove(ioc, sas_address,
  4971. parent_handle);
  4972. _scsih_sas_device_remove(ioc, sas_device);
  4973. }
  4974. }
  4975. }
  4976. /**
  4977. * _scsih_probe_raid - reporting raid volumes to scsi-ml
  4978. * @ioc: per adapter object
  4979. *
  4980. * Called during initial loading of the driver.
  4981. */
  4982. static void
  4983. _scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
  4984. {
  4985. struct _raid_device *raid_device, *raid_next;
  4986. int rc;
  4987. list_for_each_entry_safe(raid_device, raid_next,
  4988. &ioc->raid_device_list, list) {
  4989. if (raid_device->starget)
  4990. continue;
  4991. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  4992. raid_device->id, 0);
  4993. if (rc)
  4994. _scsih_raid_device_remove(ioc, raid_device);
  4995. }
  4996. }
  4997. /**
  4998. * _scsih_probe_sas - reporting raid volumes to sas transport
  4999. * @ioc: per adapter object
  5000. *
  5001. * Called during initial loading of the driver.
  5002. */
  5003. static void
  5004. _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
  5005. {
  5006. struct _sas_device *sas_device, *next;
  5007. unsigned long flags;
  5008. u16 handle, parent_handle;
  5009. u64 sas_address;
  5010. /* SAS Device List */
  5011. list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
  5012. list) {
  5013. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5014. list_move_tail(&sas_device->list, &ioc->sas_device_list);
  5015. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5016. handle = sas_device->handle;
  5017. parent_handle = sas_device->parent_handle;
  5018. sas_address = sas_device->sas_address;
  5019. if (!mpt2sas_transport_port_add(ioc, handle, parent_handle)) {
  5020. _scsih_sas_device_remove(ioc, sas_device);
  5021. } else if (!sas_device->starget) {
  5022. mpt2sas_transport_port_remove(ioc, sas_address,
  5023. parent_handle);
  5024. _scsih_sas_device_remove(ioc, sas_device);
  5025. }
  5026. }
  5027. }
  5028. /**
  5029. * _scsih_probe_devices - probing for devices
  5030. * @ioc: per adapter object
  5031. *
  5032. * Called during initial loading of the driver.
  5033. */
  5034. static void
  5035. _scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
  5036. {
  5037. u16 volume_mapping_flags =
  5038. le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
  5039. MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
  5040. if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
  5041. return; /* return when IOC doesn't support initiator mode */
  5042. _scsih_probe_boot_devices(ioc);
  5043. if (ioc->ir_firmware) {
  5044. if ((volume_mapping_flags &
  5045. MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
  5046. _scsih_probe_sas(ioc);
  5047. _scsih_probe_raid(ioc);
  5048. } else {
  5049. _scsih_probe_raid(ioc);
  5050. _scsih_probe_sas(ioc);
  5051. }
  5052. } else
  5053. _scsih_probe_sas(ioc);
  5054. }
  5055. /**
  5056. * scsih_probe - attach and add scsi host
  5057. * @pdev: PCI device struct
  5058. * @id: pci device id
  5059. *
  5060. * Returns 0 success, anything else error.
  5061. */
  5062. static int
  5063. scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  5064. {
  5065. struct MPT2SAS_ADAPTER *ioc;
  5066. struct Scsi_Host *shost;
  5067. shost = scsi_host_alloc(&scsih_driver_template,
  5068. sizeof(struct MPT2SAS_ADAPTER));
  5069. if (!shost)
  5070. return -ENODEV;
  5071. /* init local params */
  5072. ioc = shost_priv(shost);
  5073. memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
  5074. INIT_LIST_HEAD(&ioc->list);
  5075. list_add_tail(&ioc->list, &mpt2sas_ioc_list);
  5076. ioc->shost = shost;
  5077. ioc->id = mpt_ids++;
  5078. sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
  5079. ioc->pdev = pdev;
  5080. ioc->scsi_io_cb_idx = scsi_io_cb_idx;
  5081. ioc->tm_cb_idx = tm_cb_idx;
  5082. ioc->ctl_cb_idx = ctl_cb_idx;
  5083. ioc->base_cb_idx = base_cb_idx;
  5084. ioc->transport_cb_idx = transport_cb_idx;
  5085. ioc->config_cb_idx = config_cb_idx;
  5086. ioc->logging_level = logging_level;
  5087. /* misc semaphores and spin locks */
  5088. spin_lock_init(&ioc->ioc_reset_in_progress_lock);
  5089. spin_lock_init(&ioc->scsi_lookup_lock);
  5090. spin_lock_init(&ioc->sas_device_lock);
  5091. spin_lock_init(&ioc->sas_node_lock);
  5092. spin_lock_init(&ioc->fw_event_lock);
  5093. spin_lock_init(&ioc->raid_device_lock);
  5094. INIT_LIST_HEAD(&ioc->sas_device_list);
  5095. INIT_LIST_HEAD(&ioc->sas_device_init_list);
  5096. INIT_LIST_HEAD(&ioc->sas_expander_list);
  5097. INIT_LIST_HEAD(&ioc->fw_event_list);
  5098. INIT_LIST_HEAD(&ioc->raid_device_list);
  5099. INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
  5100. /* init shost parameters */
  5101. shost->max_cmd_len = 16;
  5102. shost->max_lun = max_lun;
  5103. shost->transportt = mpt2sas_transport_template;
  5104. shost->unique_id = ioc->id;
  5105. if ((scsi_add_host(shost, &pdev->dev))) {
  5106. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5107. ioc->name, __FILE__, __LINE__, __func__);
  5108. list_del(&ioc->list);
  5109. goto out_add_shost_fail;
  5110. }
  5111. scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
  5112. | SHOST_DIF_TYPE3_PROTECTION);
  5113. /* event thread */
  5114. snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
  5115. "fw_event%d", ioc->id);
  5116. ioc->firmware_event_thread = create_singlethread_workqueue(
  5117. ioc->firmware_event_name);
  5118. if (!ioc->firmware_event_thread) {
  5119. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5120. ioc->name, __FILE__, __LINE__, __func__);
  5121. goto out_thread_fail;
  5122. }
  5123. ioc->wait_for_port_enable_to_complete = 1;
  5124. if ((mpt2sas_base_attach(ioc))) {
  5125. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5126. ioc->name, __FILE__, __LINE__, __func__);
  5127. goto out_attach_fail;
  5128. }
  5129. ioc->wait_for_port_enable_to_complete = 0;
  5130. _scsih_probe_devices(ioc);
  5131. return 0;
  5132. out_attach_fail:
  5133. destroy_workqueue(ioc->firmware_event_thread);
  5134. out_thread_fail:
  5135. list_del(&ioc->list);
  5136. scsi_remove_host(shost);
  5137. out_add_shost_fail:
  5138. return -ENODEV;
  5139. }
  5140. #ifdef CONFIG_PM
  5141. /**
  5142. * scsih_suspend - power management suspend main entry point
  5143. * @pdev: PCI device struct
  5144. * @state: PM state change to (usually PCI_D3)
  5145. *
  5146. * Returns 0 success, anything else error.
  5147. */
  5148. static int
  5149. scsih_suspend(struct pci_dev *pdev, pm_message_t state)
  5150. {
  5151. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  5152. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  5153. u32 device_state;
  5154. flush_scheduled_work();
  5155. scsi_block_requests(shost);
  5156. device_state = pci_choose_state(pdev, state);
  5157. printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
  5158. "operating state [D%d]\n", ioc->name, pdev,
  5159. pci_name(pdev), device_state);
  5160. mpt2sas_base_free_resources(ioc);
  5161. pci_save_state(pdev);
  5162. pci_disable_device(pdev);
  5163. pci_set_power_state(pdev, device_state);
  5164. return 0;
  5165. }
  5166. /**
  5167. * scsih_resume - power management resume main entry point
  5168. * @pdev: PCI device struct
  5169. *
  5170. * Returns 0 success, anything else error.
  5171. */
  5172. static int
  5173. scsih_resume(struct pci_dev *pdev)
  5174. {
  5175. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  5176. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  5177. u32 device_state = pdev->current_state;
  5178. int r;
  5179. printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
  5180. "operating state [D%d]\n", ioc->name, pdev,
  5181. pci_name(pdev), device_state);
  5182. pci_set_power_state(pdev, PCI_D0);
  5183. pci_enable_wake(pdev, PCI_D0, 0);
  5184. pci_restore_state(pdev);
  5185. ioc->pdev = pdev;
  5186. r = mpt2sas_base_map_resources(ioc);
  5187. if (r)
  5188. return r;
  5189. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
  5190. scsi_unblock_requests(shost);
  5191. return 0;
  5192. }
  5193. #endif /* CONFIG_PM */
  5194. static struct pci_driver scsih_driver = {
  5195. .name = MPT2SAS_DRIVER_NAME,
  5196. .id_table = scsih_pci_table,
  5197. .probe = scsih_probe,
  5198. .remove = __devexit_p(scsih_remove),
  5199. #ifdef CONFIG_PM
  5200. .suspend = scsih_suspend,
  5201. .resume = scsih_resume,
  5202. #endif
  5203. };
  5204. /**
  5205. * scsih_init - main entry point for this driver.
  5206. *
  5207. * Returns 0 success, anything else error.
  5208. */
  5209. static int __init
  5210. scsih_init(void)
  5211. {
  5212. int error;
  5213. mpt_ids = 0;
  5214. printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
  5215. MPT2SAS_DRIVER_VERSION);
  5216. mpt2sas_transport_template =
  5217. sas_attach_transport(&mpt2sas_transport_functions);
  5218. if (!mpt2sas_transport_template)
  5219. return -ENODEV;
  5220. mpt2sas_base_initialize_callback_handler();
  5221. /* queuecommand callback hander */
  5222. scsi_io_cb_idx = mpt2sas_base_register_callback_handler(scsih_io_done);
  5223. /* task managment callback handler */
  5224. tm_cb_idx = mpt2sas_base_register_callback_handler(scsih_tm_done);
  5225. /* base internal commands callback handler */
  5226. base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
  5227. /* transport internal commands callback handler */
  5228. transport_cb_idx = mpt2sas_base_register_callback_handler(
  5229. mpt2sas_transport_done);
  5230. /* configuration page API internal commands callback handler */
  5231. config_cb_idx = mpt2sas_base_register_callback_handler(
  5232. mpt2sas_config_done);
  5233. /* ctl module callback handler */
  5234. ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
  5235. mpt2sas_ctl_init();
  5236. error = pci_register_driver(&scsih_driver);
  5237. if (error)
  5238. sas_release_transport(mpt2sas_transport_template);
  5239. return error;
  5240. }
  5241. /**
  5242. * scsih_exit - exit point for this driver (when it is a module).
  5243. *
  5244. * Returns 0 success, anything else error.
  5245. */
  5246. static void __exit
  5247. scsih_exit(void)
  5248. {
  5249. printk(KERN_INFO "mpt2sas version %s unloading\n",
  5250. MPT2SAS_DRIVER_VERSION);
  5251. pci_unregister_driver(&scsih_driver);
  5252. sas_release_transport(mpt2sas_transport_template);
  5253. mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
  5254. mpt2sas_base_release_callback_handler(tm_cb_idx);
  5255. mpt2sas_base_release_callback_handler(base_cb_idx);
  5256. mpt2sas_base_release_callback_handler(transport_cb_idx);
  5257. mpt2sas_base_release_callback_handler(config_cb_idx);
  5258. mpt2sas_base_release_callback_handler(ctl_cb_idx);
  5259. mpt2sas_ctl_exit();
  5260. }
  5261. module_init(scsih_init);
  5262. module_exit(scsih_exit);