mpt2sas_scsih.c 160 KB

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