mptsas.c 147 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438
  1. /*
  2. * linux/drivers/message/fusion/mptsas.c
  3. * For use with LSI PCI chip/adapter(s)
  4. * running LSI Fusion MPT (Message Passing Technology) firmware.
  5. *
  6. * Copyright (c) 1999-2008 LSI Corporation
  7. * (mailto:DL-MPTFusionLinux@lsi.com)
  8. */
  9. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  10. /*
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; version 2 of the License.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  39. */
  40. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  41. #include <linux/module.h>
  42. #include <linux/kernel.h>
  43. #include <linux/slab.h>
  44. #include <linux/init.h>
  45. #include <linux/errno.h>
  46. #include <linux/jiffies.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/delay.h> /* for mdelay */
  49. #include <scsi/scsi.h>
  50. #include <scsi/scsi_cmnd.h>
  51. #include <scsi/scsi_device.h>
  52. #include <scsi/scsi_host.h>
  53. #include <scsi/scsi_transport_sas.h>
  54. #include <scsi/scsi_transport.h>
  55. #include <scsi/scsi_dbg.h>
  56. #include "mptbase.h"
  57. #include "mptscsih.h"
  58. #include "mptsas.h"
  59. #define my_NAME "Fusion MPT SAS Host driver"
  60. #define my_VERSION MPT_LINUX_VERSION_COMMON
  61. #define MYNAM "mptsas"
  62. /*
  63. * Reserved channel for integrated raid
  64. */
  65. #define MPTSAS_RAID_CHANNEL 1
  66. #define SAS_CONFIG_PAGE_TIMEOUT 30
  67. MODULE_AUTHOR(MODULEAUTHOR);
  68. MODULE_DESCRIPTION(my_NAME);
  69. MODULE_LICENSE("GPL");
  70. MODULE_VERSION(my_VERSION);
  71. static int mpt_pt_clear;
  72. module_param(mpt_pt_clear, int, 0);
  73. MODULE_PARM_DESC(mpt_pt_clear,
  74. " Clear persistency table: enable=1 "
  75. "(default=MPTSCSIH_PT_CLEAR=0)");
  76. /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
  77. #define MPTSAS_MAX_LUN (16895)
  78. static int max_lun = MPTSAS_MAX_LUN;
  79. module_param(max_lun, int, 0);
  80. MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
  81. static int mpt_loadtime_max_sectors = 8192;
  82. module_param(mpt_loadtime_max_sectors, int, 0);
  83. MODULE_PARM_DESC(mpt_loadtime_max_sectors,
  84. " Maximum sector define for Host Bus Adaptor.Range 64 to 8192 default=8192");
  85. static u8 mptsasDoneCtx = MPT_MAX_PROTOCOL_DRIVERS;
  86. static u8 mptsasTaskCtx = MPT_MAX_PROTOCOL_DRIVERS;
  87. static u8 mptsasInternalCtx = MPT_MAX_PROTOCOL_DRIVERS; /* Used only for internal commands */
  88. static u8 mptsasMgmtCtx = MPT_MAX_PROTOCOL_DRIVERS;
  89. static u8 mptsasDeviceResetCtx = MPT_MAX_PROTOCOL_DRIVERS;
  90. static void mptsas_firmware_event_work(struct work_struct *work);
  91. static void mptsas_send_sas_event(struct fw_event_work *fw_event);
  92. static void mptsas_send_raid_event(struct fw_event_work *fw_event);
  93. static void mptsas_send_ir2_event(struct fw_event_work *fw_event);
  94. static void mptsas_parse_device_info(struct sas_identify *identify,
  95. struct mptsas_devinfo *device_info);
  96. static inline void mptsas_set_rphy(MPT_ADAPTER *ioc,
  97. struct mptsas_phyinfo *phy_info, struct sas_rphy *rphy);
  98. static struct mptsas_phyinfo *mptsas_find_phyinfo_by_sas_address
  99. (MPT_ADAPTER *ioc, u64 sas_address);
  100. static int mptsas_sas_device_pg0(MPT_ADAPTER *ioc,
  101. struct mptsas_devinfo *device_info, u32 form, u32 form_specific);
  102. static int mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc,
  103. struct mptsas_enclosure *enclosure, u32 form, u32 form_specific);
  104. static int mptsas_add_end_device(MPT_ADAPTER *ioc,
  105. struct mptsas_phyinfo *phy_info);
  106. static void mptsas_del_end_device(MPT_ADAPTER *ioc,
  107. struct mptsas_phyinfo *phy_info);
  108. static void mptsas_send_link_status_event(struct fw_event_work *fw_event);
  109. static struct mptsas_portinfo *mptsas_find_portinfo_by_sas_address
  110. (MPT_ADAPTER *ioc, u64 sas_address);
  111. static void mptsas_expander_delete(MPT_ADAPTER *ioc,
  112. struct mptsas_portinfo *port_info, u8 force);
  113. static void mptsas_send_expander_event(struct fw_event_work *fw_event);
  114. static void mptsas_not_responding_devices(MPT_ADAPTER *ioc);
  115. static void mptsas_scan_sas_topology(MPT_ADAPTER *ioc);
  116. static void mptsas_broadcast_primitive_work(struct fw_event_work *fw_event);
  117. static void mptsas_handle_queue_full_event(struct fw_event_work *fw_event);
  118. static void mptsas_volume_delete(MPT_ADAPTER *ioc, u8 id);
  119. void mptsas_schedule_target_reset(void *ioc);
  120. static void mptsas_print_phy_data(MPT_ADAPTER *ioc,
  121. MPI_SAS_IO_UNIT0_PHY_DATA *phy_data)
  122. {
  123. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  124. "---- IO UNIT PAGE 0 ------------\n", ioc->name));
  125. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Handle=0x%X\n",
  126. ioc->name, le16_to_cpu(phy_data->AttachedDeviceHandle)));
  127. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Controller Handle=0x%X\n",
  128. ioc->name, le16_to_cpu(phy_data->ControllerDevHandle)));
  129. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Port=0x%X\n",
  130. ioc->name, phy_data->Port));
  131. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Port Flags=0x%X\n",
  132. ioc->name, phy_data->PortFlags));
  133. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Flags=0x%X\n",
  134. ioc->name, phy_data->PhyFlags));
  135. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Negotiated Link Rate=0x%X\n",
  136. ioc->name, phy_data->NegotiatedLinkRate));
  137. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  138. "Controller PHY Device Info=0x%X\n", ioc->name,
  139. le32_to_cpu(phy_data->ControllerPhyDeviceInfo)));
  140. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "DiscoveryStatus=0x%X\n\n",
  141. ioc->name, le32_to_cpu(phy_data->DiscoveryStatus)));
  142. }
  143. static void mptsas_print_phy_pg0(MPT_ADAPTER *ioc, SasPhyPage0_t *pg0)
  144. {
  145. __le64 sas_address;
  146. memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
  147. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  148. "---- SAS PHY PAGE 0 ------------\n", ioc->name));
  149. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  150. "Attached Device Handle=0x%X\n", ioc->name,
  151. le16_to_cpu(pg0->AttachedDevHandle)));
  152. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "SAS Address=0x%llX\n",
  153. ioc->name, (unsigned long long)le64_to_cpu(sas_address)));
  154. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  155. "Attached PHY Identifier=0x%X\n", ioc->name,
  156. pg0->AttachedPhyIdentifier));
  157. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Attached Device Info=0x%X\n",
  158. ioc->name, le32_to_cpu(pg0->AttachedDeviceInfo)));
  159. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Programmed Link Rate=0x%X\n",
  160. ioc->name, pg0->ProgrammedLinkRate));
  161. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Change Count=0x%X\n",
  162. ioc->name, pg0->ChangeCount));
  163. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Info=0x%X\n\n",
  164. ioc->name, le32_to_cpu(pg0->PhyInfo)));
  165. }
  166. static void mptsas_print_phy_pg1(MPT_ADAPTER *ioc, SasPhyPage1_t *pg1)
  167. {
  168. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  169. "---- SAS PHY PAGE 1 ------------\n", ioc->name));
  170. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Invalid Dword Count=0x%x\n",
  171. ioc->name, pg1->InvalidDwordCount));
  172. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  173. "Running Disparity Error Count=0x%x\n", ioc->name,
  174. pg1->RunningDisparityErrorCount));
  175. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  176. "Loss Dword Synch Count=0x%x\n", ioc->name,
  177. pg1->LossDwordSynchCount));
  178. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  179. "PHY Reset Problem Count=0x%x\n\n", ioc->name,
  180. pg1->PhyResetProblemCount));
  181. }
  182. static void mptsas_print_device_pg0(MPT_ADAPTER *ioc, SasDevicePage0_t *pg0)
  183. {
  184. __le64 sas_address;
  185. memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
  186. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  187. "---- SAS DEVICE PAGE 0 ---------\n", ioc->name));
  188. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Handle=0x%X\n",
  189. ioc->name, le16_to_cpu(pg0->DevHandle)));
  190. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Parent Handle=0x%X\n",
  191. ioc->name, le16_to_cpu(pg0->ParentDevHandle)));
  192. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Enclosure Handle=0x%X\n",
  193. ioc->name, le16_to_cpu(pg0->EnclosureHandle)));
  194. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Slot=0x%X\n",
  195. ioc->name, le16_to_cpu(pg0->Slot)));
  196. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "SAS Address=0x%llX\n",
  197. ioc->name, (unsigned long long)le64_to_cpu(sas_address)));
  198. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Target ID=0x%X\n",
  199. ioc->name, pg0->TargetID));
  200. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Bus=0x%X\n",
  201. ioc->name, pg0->Bus));
  202. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Parent Phy Num=0x%X\n",
  203. ioc->name, pg0->PhyNum));
  204. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Access Status=0x%X\n",
  205. ioc->name, le16_to_cpu(pg0->AccessStatus)));
  206. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Device Info=0x%X\n",
  207. ioc->name, le32_to_cpu(pg0->DeviceInfo)));
  208. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Flags=0x%X\n",
  209. ioc->name, le16_to_cpu(pg0->Flags)));
  210. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Physical Port=0x%X\n\n",
  211. ioc->name, pg0->PhysicalPort));
  212. }
  213. static void mptsas_print_expander_pg1(MPT_ADAPTER *ioc, SasExpanderPage1_t *pg1)
  214. {
  215. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  216. "---- SAS EXPANDER PAGE 1 ------------\n", ioc->name));
  217. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Physical Port=0x%X\n",
  218. ioc->name, pg1->PhysicalPort));
  219. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Identifier=0x%X\n",
  220. ioc->name, pg1->PhyIdentifier));
  221. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Negotiated Link Rate=0x%X\n",
  222. ioc->name, pg1->NegotiatedLinkRate));
  223. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Programmed Link Rate=0x%X\n",
  224. ioc->name, pg1->ProgrammedLinkRate));
  225. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Hardware Link Rate=0x%X\n",
  226. ioc->name, pg1->HwLinkRate));
  227. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Owner Device Handle=0x%X\n",
  228. ioc->name, le16_to_cpu(pg1->OwnerDevHandle)));
  229. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  230. "Attached Device Handle=0x%X\n\n", ioc->name,
  231. le16_to_cpu(pg1->AttachedDevHandle)));
  232. }
  233. /* inhibit sas firmware event handling */
  234. static void
  235. mptsas_fw_event_off(MPT_ADAPTER *ioc)
  236. {
  237. unsigned long flags;
  238. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  239. ioc->fw_events_off = 1;
  240. ioc->sas_discovery_quiesce_io = 0;
  241. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  242. }
  243. /* enable sas firmware event handling */
  244. static void
  245. mptsas_fw_event_on(MPT_ADAPTER *ioc)
  246. {
  247. unsigned long flags;
  248. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  249. ioc->fw_events_off = 0;
  250. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  251. }
  252. /* queue a sas firmware event */
  253. static void
  254. mptsas_add_fw_event(MPT_ADAPTER *ioc, struct fw_event_work *fw_event,
  255. unsigned long delay)
  256. {
  257. unsigned long flags;
  258. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  259. list_add_tail(&fw_event->list, &ioc->fw_event_list);
  260. INIT_DELAYED_WORK(&fw_event->work, mptsas_firmware_event_work);
  261. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: add (fw_event=0x%p)"
  262. "on cpuid %d\n", ioc->name, __func__,
  263. fw_event, smp_processor_id()));
  264. queue_delayed_work_on(smp_processor_id(), ioc->fw_event_q,
  265. &fw_event->work, delay);
  266. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  267. }
  268. /* requeue a sas firmware event */
  269. static void
  270. mptsas_requeue_fw_event(MPT_ADAPTER *ioc, struct fw_event_work *fw_event,
  271. unsigned long delay)
  272. {
  273. unsigned long flags;
  274. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  275. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: reschedule task "
  276. "(fw_event=0x%p)on cpuid %d\n", ioc->name, __func__,
  277. fw_event, smp_processor_id()));
  278. fw_event->retries++;
  279. queue_delayed_work_on(smp_processor_id(), ioc->fw_event_q,
  280. &fw_event->work, msecs_to_jiffies(delay));
  281. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  282. }
  283. /* free memory associated to a sas firmware event */
  284. static void
  285. mptsas_free_fw_event(MPT_ADAPTER *ioc, struct fw_event_work *fw_event)
  286. {
  287. unsigned long flags;
  288. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  289. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: kfree (fw_event=0x%p)\n",
  290. ioc->name, __func__, fw_event));
  291. list_del(&fw_event->list);
  292. kfree(fw_event);
  293. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  294. }
  295. /* walk the firmware event queue, and either stop or wait for
  296. * outstanding events to complete */
  297. static void
  298. mptsas_cleanup_fw_event_q(MPT_ADAPTER *ioc)
  299. {
  300. struct fw_event_work *fw_event, *next;
  301. struct mptsas_target_reset_event *target_reset_list, *n;
  302. MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
  303. /* flush the target_reset_list */
  304. if (!list_empty(&hd->target_reset_list)) {
  305. list_for_each_entry_safe(target_reset_list, n,
  306. &hd->target_reset_list, list) {
  307. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  308. "%s: removing target reset for id=%d\n",
  309. ioc->name, __func__,
  310. target_reset_list->sas_event_data.TargetID));
  311. list_del(&target_reset_list->list);
  312. kfree(target_reset_list);
  313. }
  314. }
  315. if (list_empty(&ioc->fw_event_list) ||
  316. !ioc->fw_event_q || in_interrupt())
  317. return;
  318. list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
  319. if (cancel_delayed_work(&fw_event->work))
  320. mptsas_free_fw_event(ioc, fw_event);
  321. }
  322. }
  323. static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
  324. {
  325. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  326. return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
  327. }
  328. static inline MPT_ADAPTER *rphy_to_ioc(struct sas_rphy *rphy)
  329. {
  330. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  331. return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
  332. }
  333. /*
  334. * mptsas_find_portinfo_by_handle
  335. *
  336. * This function should be called with the sas_topology_mutex already held
  337. */
  338. static struct mptsas_portinfo *
  339. mptsas_find_portinfo_by_handle(MPT_ADAPTER *ioc, u16 handle)
  340. {
  341. struct mptsas_portinfo *port_info, *rc=NULL;
  342. int i;
  343. list_for_each_entry(port_info, &ioc->sas_topology, list)
  344. for (i = 0; i < port_info->num_phys; i++)
  345. if (port_info->phy_info[i].identify.handle == handle) {
  346. rc = port_info;
  347. goto out;
  348. }
  349. out:
  350. return rc;
  351. }
  352. /**
  353. * mptsas_find_portinfo_by_sas_address -
  354. * @ioc: Pointer to MPT_ADAPTER structure
  355. * @handle:
  356. *
  357. * This function should be called with the sas_topology_mutex already held
  358. *
  359. **/
  360. static struct mptsas_portinfo *
  361. mptsas_find_portinfo_by_sas_address(MPT_ADAPTER *ioc, u64 sas_address)
  362. {
  363. struct mptsas_portinfo *port_info, *rc = NULL;
  364. int i;
  365. if (sas_address >= ioc->hba_port_sas_addr &&
  366. sas_address < (ioc->hba_port_sas_addr +
  367. ioc->hba_port_num_phy))
  368. return ioc->hba_port_info;
  369. mutex_lock(&ioc->sas_topology_mutex);
  370. list_for_each_entry(port_info, &ioc->sas_topology, list)
  371. for (i = 0; i < port_info->num_phys; i++)
  372. if (port_info->phy_info[i].identify.sas_address ==
  373. sas_address) {
  374. rc = port_info;
  375. goto out;
  376. }
  377. out:
  378. mutex_unlock(&ioc->sas_topology_mutex);
  379. return rc;
  380. }
  381. /*
  382. * Returns true if there is a scsi end device
  383. */
  384. static inline int
  385. mptsas_is_end_device(struct mptsas_devinfo * attached)
  386. {
  387. if ((attached->sas_address) &&
  388. (attached->device_info &
  389. MPI_SAS_DEVICE_INFO_END_DEVICE) &&
  390. ((attached->device_info &
  391. MPI_SAS_DEVICE_INFO_SSP_TARGET) |
  392. (attached->device_info &
  393. MPI_SAS_DEVICE_INFO_STP_TARGET) |
  394. (attached->device_info &
  395. MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
  396. return 1;
  397. else
  398. return 0;
  399. }
  400. /* no mutex */
  401. static void
  402. mptsas_port_delete(MPT_ADAPTER *ioc, struct mptsas_portinfo_details * port_details)
  403. {
  404. struct mptsas_portinfo *port_info;
  405. struct mptsas_phyinfo *phy_info;
  406. u8 i;
  407. if (!port_details)
  408. return;
  409. port_info = port_details->port_info;
  410. phy_info = port_info->phy_info;
  411. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: [%p]: num_phys=%02d "
  412. "bitmask=0x%016llX\n", ioc->name, __func__, port_details,
  413. port_details->num_phys, (unsigned long long)
  414. port_details->phy_bitmask));
  415. for (i = 0; i < port_info->num_phys; i++, phy_info++) {
  416. if(phy_info->port_details != port_details)
  417. continue;
  418. memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
  419. mptsas_set_rphy(ioc, phy_info, NULL);
  420. phy_info->port_details = NULL;
  421. }
  422. kfree(port_details);
  423. }
  424. static inline struct sas_rphy *
  425. mptsas_get_rphy(struct mptsas_phyinfo *phy_info)
  426. {
  427. if (phy_info->port_details)
  428. return phy_info->port_details->rphy;
  429. else
  430. return NULL;
  431. }
  432. static inline void
  433. mptsas_set_rphy(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info, struct sas_rphy *rphy)
  434. {
  435. if (phy_info->port_details) {
  436. phy_info->port_details->rphy = rphy;
  437. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "sas_rphy_add: rphy=%p\n",
  438. ioc->name, rphy));
  439. }
  440. if (rphy) {
  441. dsaswideprintk(ioc, dev_printk(KERN_DEBUG,
  442. &rphy->dev, MYIOC_s_FMT "add:", ioc->name));
  443. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "rphy=%p release=%p\n",
  444. ioc->name, rphy, rphy->dev.release));
  445. }
  446. }
  447. static inline struct sas_port *
  448. mptsas_get_port(struct mptsas_phyinfo *phy_info)
  449. {
  450. if (phy_info->port_details)
  451. return phy_info->port_details->port;
  452. else
  453. return NULL;
  454. }
  455. static inline void
  456. mptsas_set_port(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info, struct sas_port *port)
  457. {
  458. if (phy_info->port_details)
  459. phy_info->port_details->port = port;
  460. if (port) {
  461. dsaswideprintk(ioc, dev_printk(KERN_DEBUG,
  462. &port->dev, MYIOC_s_FMT "add:", ioc->name));
  463. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "port=%p release=%p\n",
  464. ioc->name, port, port->dev.release));
  465. }
  466. }
  467. static inline struct scsi_target *
  468. mptsas_get_starget(struct mptsas_phyinfo *phy_info)
  469. {
  470. if (phy_info->port_details)
  471. return phy_info->port_details->starget;
  472. else
  473. return NULL;
  474. }
  475. static inline void
  476. mptsas_set_starget(struct mptsas_phyinfo *phy_info, struct scsi_target *
  477. starget)
  478. {
  479. if (phy_info->port_details)
  480. phy_info->port_details->starget = starget;
  481. }
  482. /**
  483. * mptsas_add_device_component -
  484. * @ioc: Pointer to MPT_ADAPTER structure
  485. * @channel: fw mapped id's
  486. * @id:
  487. * @sas_address:
  488. * @device_info:
  489. *
  490. **/
  491. static void
  492. mptsas_add_device_component(MPT_ADAPTER *ioc, u8 channel, u8 id,
  493. u64 sas_address, u32 device_info, u16 slot, u64 enclosure_logical_id)
  494. {
  495. struct mptsas_device_info *sas_info, *next;
  496. struct scsi_device *sdev;
  497. struct scsi_target *starget;
  498. struct sas_rphy *rphy;
  499. /*
  500. * Delete all matching devices out of the list
  501. */
  502. mutex_lock(&ioc->sas_device_info_mutex);
  503. list_for_each_entry_safe(sas_info, next, &ioc->sas_device_info_list,
  504. list) {
  505. if (!sas_info->is_logical_volume &&
  506. (sas_info->sas_address == sas_address ||
  507. (sas_info->fw.channel == channel &&
  508. sas_info->fw.id == id))) {
  509. list_del(&sas_info->list);
  510. kfree(sas_info);
  511. }
  512. }
  513. sas_info = kzalloc(sizeof(struct mptsas_device_info), GFP_KERNEL);
  514. if (!sas_info)
  515. goto out;
  516. /*
  517. * Set Firmware mapping
  518. */
  519. sas_info->fw.id = id;
  520. sas_info->fw.channel = channel;
  521. sas_info->sas_address = sas_address;
  522. sas_info->device_info = device_info;
  523. sas_info->slot = slot;
  524. sas_info->enclosure_logical_id = enclosure_logical_id;
  525. INIT_LIST_HEAD(&sas_info->list);
  526. list_add_tail(&sas_info->list, &ioc->sas_device_info_list);
  527. /*
  528. * Set OS mapping
  529. */
  530. shost_for_each_device(sdev, ioc->sh) {
  531. starget = scsi_target(sdev);
  532. rphy = dev_to_rphy(starget->dev.parent);
  533. if (rphy->identify.sas_address == sas_address) {
  534. sas_info->os.id = starget->id;
  535. sas_info->os.channel = starget->channel;
  536. }
  537. }
  538. out:
  539. mutex_unlock(&ioc->sas_device_info_mutex);
  540. return;
  541. }
  542. /**
  543. * mptsas_add_device_component_by_fw -
  544. * @ioc: Pointer to MPT_ADAPTER structure
  545. * @channel: fw mapped id's
  546. * @id:
  547. *
  548. **/
  549. static void
  550. mptsas_add_device_component_by_fw(MPT_ADAPTER *ioc, u8 channel, u8 id)
  551. {
  552. struct mptsas_devinfo sas_device;
  553. struct mptsas_enclosure enclosure_info;
  554. int rc;
  555. rc = mptsas_sas_device_pg0(ioc, &sas_device,
  556. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  557. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  558. (channel << 8) + id);
  559. if (rc)
  560. return;
  561. memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
  562. mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
  563. (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
  564. MPI_SAS_ENCLOS_PGAD_FORM_SHIFT),
  565. sas_device.handle_enclosure);
  566. mptsas_add_device_component(ioc, sas_device.channel,
  567. sas_device.id, sas_device.sas_address, sas_device.device_info,
  568. sas_device.slot, enclosure_info.enclosure_logical_id);
  569. }
  570. /**
  571. * mptsas_add_device_component_starget_ir - Handle Integrated RAID, adding each individual device to list
  572. * @ioc: Pointer to MPT_ADAPTER structure
  573. * @channel: fw mapped id's
  574. * @id:
  575. *
  576. **/
  577. static void
  578. mptsas_add_device_component_starget_ir(MPT_ADAPTER *ioc,
  579. struct scsi_target *starget)
  580. {
  581. CONFIGPARMS cfg;
  582. ConfigPageHeader_t hdr;
  583. dma_addr_t dma_handle;
  584. pRaidVolumePage0_t buffer = NULL;
  585. int i;
  586. RaidPhysDiskPage0_t phys_disk;
  587. struct mptsas_device_info *sas_info, *next;
  588. memset(&cfg, 0 , sizeof(CONFIGPARMS));
  589. memset(&hdr, 0 , sizeof(ConfigPageHeader_t));
  590. hdr.PageType = MPI_CONFIG_PAGETYPE_RAID_VOLUME;
  591. /* assumption that all volumes on channel = 0 */
  592. cfg.pageAddr = starget->id;
  593. cfg.cfghdr.hdr = &hdr;
  594. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  595. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  596. if (mpt_config(ioc, &cfg) != 0)
  597. goto out;
  598. if (!hdr.PageLength)
  599. goto out;
  600. buffer = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4,
  601. &dma_handle);
  602. if (!buffer)
  603. goto out;
  604. cfg.physAddr = dma_handle;
  605. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  606. if (mpt_config(ioc, &cfg) != 0)
  607. goto out;
  608. if (!buffer->NumPhysDisks)
  609. goto out;
  610. /*
  611. * Adding entry for hidden components
  612. */
  613. for (i = 0; i < buffer->NumPhysDisks; i++) {
  614. if (mpt_raid_phys_disk_pg0(ioc,
  615. buffer->PhysDisk[i].PhysDiskNum, &phys_disk) != 0)
  616. continue;
  617. mptsas_add_device_component_by_fw(ioc, phys_disk.PhysDiskBus,
  618. phys_disk.PhysDiskID);
  619. mutex_lock(&ioc->sas_device_info_mutex);
  620. list_for_each_entry(sas_info, &ioc->sas_device_info_list,
  621. list) {
  622. if (!sas_info->is_logical_volume &&
  623. (sas_info->fw.channel == phys_disk.PhysDiskBus &&
  624. sas_info->fw.id == phys_disk.PhysDiskID)) {
  625. sas_info->is_hidden_raid_component = 1;
  626. sas_info->volume_id = starget->id;
  627. }
  628. }
  629. mutex_unlock(&ioc->sas_device_info_mutex);
  630. }
  631. /*
  632. * Delete all matching devices out of the list
  633. */
  634. mutex_lock(&ioc->sas_device_info_mutex);
  635. list_for_each_entry_safe(sas_info, next, &ioc->sas_device_info_list,
  636. list) {
  637. if (sas_info->is_logical_volume && sas_info->fw.id ==
  638. starget->id) {
  639. list_del(&sas_info->list);
  640. kfree(sas_info);
  641. }
  642. }
  643. sas_info = kzalloc(sizeof(struct mptsas_device_info), GFP_KERNEL);
  644. if (sas_info) {
  645. sas_info->fw.id = starget->id;
  646. sas_info->os.id = starget->id;
  647. sas_info->os.channel = starget->channel;
  648. sas_info->is_logical_volume = 1;
  649. INIT_LIST_HEAD(&sas_info->list);
  650. list_add_tail(&sas_info->list, &ioc->sas_device_info_list);
  651. }
  652. mutex_unlock(&ioc->sas_device_info_mutex);
  653. out:
  654. if (buffer)
  655. pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, buffer,
  656. dma_handle);
  657. }
  658. /**
  659. * mptsas_add_device_component_starget -
  660. * @ioc: Pointer to MPT_ADAPTER structure
  661. * @starget:
  662. *
  663. **/
  664. static void
  665. mptsas_add_device_component_starget(MPT_ADAPTER *ioc,
  666. struct scsi_target *starget)
  667. {
  668. VirtTarget *vtarget;
  669. struct sas_rphy *rphy;
  670. struct mptsas_phyinfo *phy_info = NULL;
  671. struct mptsas_enclosure enclosure_info;
  672. rphy = dev_to_rphy(starget->dev.parent);
  673. vtarget = starget->hostdata;
  674. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  675. rphy->identify.sas_address);
  676. if (!phy_info)
  677. return;
  678. memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
  679. mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
  680. (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
  681. MPI_SAS_ENCLOS_PGAD_FORM_SHIFT),
  682. phy_info->attached.handle_enclosure);
  683. mptsas_add_device_component(ioc, phy_info->attached.channel,
  684. phy_info->attached.id, phy_info->attached.sas_address,
  685. phy_info->attached.device_info,
  686. phy_info->attached.slot, enclosure_info.enclosure_logical_id);
  687. }
  688. /**
  689. * mptsas_del_device_component_by_os - Once a device has been removed, we mark the entry in the list as being cached
  690. * @ioc: Pointer to MPT_ADAPTER structure
  691. * @channel: os mapped id's
  692. * @id:
  693. *
  694. **/
  695. static void
  696. mptsas_del_device_component_by_os(MPT_ADAPTER *ioc, u8 channel, u8 id)
  697. {
  698. struct mptsas_device_info *sas_info, *next;
  699. /*
  700. * Set is_cached flag
  701. */
  702. list_for_each_entry_safe(sas_info, next, &ioc->sas_device_info_list,
  703. list) {
  704. if (sas_info->os.channel == channel && sas_info->os.id == id)
  705. sas_info->is_cached = 1;
  706. }
  707. }
  708. /**
  709. * mptsas_del_device_components - Cleaning the list
  710. * @ioc: Pointer to MPT_ADAPTER structure
  711. *
  712. **/
  713. static void
  714. mptsas_del_device_components(MPT_ADAPTER *ioc)
  715. {
  716. struct mptsas_device_info *sas_info, *next;
  717. mutex_lock(&ioc->sas_device_info_mutex);
  718. list_for_each_entry_safe(sas_info, next, &ioc->sas_device_info_list,
  719. list) {
  720. list_del(&sas_info->list);
  721. kfree(sas_info);
  722. }
  723. mutex_unlock(&ioc->sas_device_info_mutex);
  724. }
  725. /*
  726. * mptsas_setup_wide_ports
  727. *
  728. * Updates for new and existing narrow/wide port configuration
  729. * in the sas_topology
  730. */
  731. static void
  732. mptsas_setup_wide_ports(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  733. {
  734. struct mptsas_portinfo_details * port_details;
  735. struct mptsas_phyinfo *phy_info, *phy_info_cmp;
  736. u64 sas_address;
  737. int i, j;
  738. mutex_lock(&ioc->sas_topology_mutex);
  739. phy_info = port_info->phy_info;
  740. for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
  741. if (phy_info->attached.handle)
  742. continue;
  743. port_details = phy_info->port_details;
  744. if (!port_details)
  745. continue;
  746. if (port_details->num_phys < 2)
  747. continue;
  748. /*
  749. * Removing a phy from a port, letting the last
  750. * phy be removed by firmware events.
  751. */
  752. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  753. "%s: [%p]: deleting phy = %d\n",
  754. ioc->name, __func__, port_details, i));
  755. port_details->num_phys--;
  756. port_details->phy_bitmask &= ~ (1 << phy_info->phy_id);
  757. memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
  758. if (phy_info->phy) {
  759. devtprintk(ioc, dev_printk(KERN_DEBUG,
  760. &phy_info->phy->dev, MYIOC_s_FMT
  761. "delete phy %d, phy-obj (0x%p)\n", ioc->name,
  762. phy_info->phy_id, phy_info->phy));
  763. sas_port_delete_phy(port_details->port, phy_info->phy);
  764. }
  765. phy_info->port_details = NULL;
  766. }
  767. /*
  768. * Populate and refresh the tree
  769. */
  770. phy_info = port_info->phy_info;
  771. for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
  772. sas_address = phy_info->attached.sas_address;
  773. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "phy_id=%d sas_address=0x%018llX\n",
  774. ioc->name, i, (unsigned long long)sas_address));
  775. if (!sas_address)
  776. continue;
  777. port_details = phy_info->port_details;
  778. /*
  779. * Forming a port
  780. */
  781. if (!port_details) {
  782. port_details = kzalloc(sizeof(struct
  783. mptsas_portinfo_details), GFP_KERNEL);
  784. if (!port_details)
  785. goto out;
  786. port_details->num_phys = 1;
  787. port_details->port_info = port_info;
  788. if (phy_info->phy_id < 64 )
  789. port_details->phy_bitmask |=
  790. (1 << phy_info->phy_id);
  791. phy_info->sas_port_add_phy=1;
  792. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\t\tForming port\n\t\t"
  793. "phy_id=%d sas_address=0x%018llX\n",
  794. ioc->name, i, (unsigned long long)sas_address));
  795. phy_info->port_details = port_details;
  796. }
  797. if (i == port_info->num_phys - 1)
  798. continue;
  799. phy_info_cmp = &port_info->phy_info[i + 1];
  800. for (j = i + 1 ; j < port_info->num_phys ; j++,
  801. phy_info_cmp++) {
  802. if (!phy_info_cmp->attached.sas_address)
  803. continue;
  804. if (sas_address != phy_info_cmp->attached.sas_address)
  805. continue;
  806. if (phy_info_cmp->port_details == port_details )
  807. continue;
  808. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  809. "\t\tphy_id=%d sas_address=0x%018llX\n",
  810. ioc->name, j, (unsigned long long)
  811. phy_info_cmp->attached.sas_address));
  812. if (phy_info_cmp->port_details) {
  813. port_details->rphy =
  814. mptsas_get_rphy(phy_info_cmp);
  815. port_details->port =
  816. mptsas_get_port(phy_info_cmp);
  817. port_details->starget =
  818. mptsas_get_starget(phy_info_cmp);
  819. port_details->num_phys =
  820. phy_info_cmp->port_details->num_phys;
  821. if (!phy_info_cmp->port_details->num_phys)
  822. kfree(phy_info_cmp->port_details);
  823. } else
  824. phy_info_cmp->sas_port_add_phy=1;
  825. /*
  826. * Adding a phy to a port
  827. */
  828. phy_info_cmp->port_details = port_details;
  829. if (phy_info_cmp->phy_id < 64 )
  830. port_details->phy_bitmask |=
  831. (1 << phy_info_cmp->phy_id);
  832. port_details->num_phys++;
  833. }
  834. }
  835. out:
  836. for (i = 0; i < port_info->num_phys; i++) {
  837. port_details = port_info->phy_info[i].port_details;
  838. if (!port_details)
  839. continue;
  840. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  841. "%s: [%p]: phy_id=%02d num_phys=%02d "
  842. "bitmask=0x%016llX\n", ioc->name, __func__,
  843. port_details, i, port_details->num_phys,
  844. (unsigned long long)port_details->phy_bitmask));
  845. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\t\tport = %p rphy=%p\n",
  846. ioc->name, port_details->port, port_details->rphy));
  847. }
  848. dsaswideprintk(ioc, printk("\n"));
  849. mutex_unlock(&ioc->sas_topology_mutex);
  850. }
  851. /**
  852. * csmisas_find_vtarget
  853. *
  854. * @ioc
  855. * @volume_id
  856. * @volume_bus
  857. *
  858. **/
  859. static VirtTarget *
  860. mptsas_find_vtarget(MPT_ADAPTER *ioc, u8 channel, u8 id)
  861. {
  862. struct scsi_device *sdev;
  863. VirtDevice *vdevice;
  864. VirtTarget *vtarget = NULL;
  865. shost_for_each_device(sdev, ioc->sh) {
  866. vdevice = sdev->hostdata;
  867. if ((vdevice == NULL) ||
  868. (vdevice->vtarget == NULL))
  869. continue;
  870. if ((vdevice->vtarget->tflags &
  871. MPT_TARGET_FLAGS_RAID_COMPONENT ||
  872. vdevice->vtarget->raidVolume))
  873. continue;
  874. if (vdevice->vtarget->id == id &&
  875. vdevice->vtarget->channel == channel)
  876. vtarget = vdevice->vtarget;
  877. }
  878. return vtarget;
  879. }
  880. static void
  881. mptsas_queue_device_delete(MPT_ADAPTER *ioc,
  882. MpiEventDataSasDeviceStatusChange_t *sas_event_data)
  883. {
  884. struct fw_event_work *fw_event;
  885. fw_event = kzalloc(sizeof(*fw_event) +
  886. sizeof(MpiEventDataSasDeviceStatusChange_t),
  887. GFP_ATOMIC);
  888. if (!fw_event) {
  889. printk(MYIOC_s_WARN_FMT "%s: failed at (line=%d)\n",
  890. ioc->name, __func__, __LINE__);
  891. return;
  892. }
  893. memcpy(fw_event->event_data, sas_event_data,
  894. sizeof(MpiEventDataSasDeviceStatusChange_t));
  895. fw_event->event = MPI_EVENT_SAS_DEVICE_STATUS_CHANGE;
  896. fw_event->ioc = ioc;
  897. mptsas_add_fw_event(ioc, fw_event, msecs_to_jiffies(1));
  898. }
  899. static void
  900. mptsas_queue_rescan(MPT_ADAPTER *ioc)
  901. {
  902. struct fw_event_work *fw_event;
  903. fw_event = kzalloc(sizeof(*fw_event), GFP_ATOMIC);
  904. if (!fw_event) {
  905. printk(MYIOC_s_WARN_FMT "%s: failed at (line=%d)\n",
  906. ioc->name, __func__, __LINE__);
  907. return;
  908. }
  909. fw_event->event = -1;
  910. fw_event->ioc = ioc;
  911. mptsas_add_fw_event(ioc, fw_event, msecs_to_jiffies(1));
  912. }
  913. /**
  914. * mptsas_target_reset
  915. *
  916. * Issues TARGET_RESET to end device using handshaking method
  917. *
  918. * @ioc
  919. * @channel
  920. * @id
  921. *
  922. * Returns (1) success
  923. * (0) failure
  924. *
  925. **/
  926. static int
  927. mptsas_target_reset(MPT_ADAPTER *ioc, u8 channel, u8 id)
  928. {
  929. MPT_FRAME_HDR *mf;
  930. SCSITaskMgmt_t *pScsiTm;
  931. if (mpt_set_taskmgmt_in_progress_flag(ioc) != 0)
  932. return 0;
  933. mf = mpt_get_msg_frame(mptsasDeviceResetCtx, ioc);
  934. if (mf == NULL) {
  935. dfailprintk(ioc, printk(MYIOC_s_WARN_FMT
  936. "%s, no msg frames @%d!!\n", ioc->name,
  937. __func__, __LINE__));
  938. goto out_fail;
  939. }
  940. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TaskMgmt request (mf=%p)\n",
  941. ioc->name, mf));
  942. /* Format the Request
  943. */
  944. pScsiTm = (SCSITaskMgmt_t *) mf;
  945. memset (pScsiTm, 0, sizeof(SCSITaskMgmt_t));
  946. pScsiTm->TargetID = id;
  947. pScsiTm->Bus = channel;
  948. pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
  949. pScsiTm->TaskType = MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
  950. pScsiTm->MsgFlags = MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION;
  951. DBG_DUMP_TM_REQUEST_FRAME(ioc, (u32 *)mf);
  952. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  953. "TaskMgmt type=%d (sas device delete) fw_channel = %d fw_id = %d)\n",
  954. ioc->name, MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET, channel, id));
  955. mpt_put_msg_frame_hi_pri(mptsasDeviceResetCtx, ioc, mf);
  956. return 1;
  957. out_fail:
  958. mpt_clear_taskmgmt_in_progress_flag(ioc);
  959. return 0;
  960. }
  961. static void
  962. mptsas_block_io_sdev(struct scsi_device *sdev, void *data)
  963. {
  964. scsi_device_set_state(sdev, SDEV_BLOCK);
  965. }
  966. static void
  967. mptsas_block_io_starget(struct scsi_target *starget)
  968. {
  969. if (starget)
  970. starget_for_each_device(starget, NULL, mptsas_block_io_sdev);
  971. }
  972. /**
  973. * mptsas_target_reset_queue
  974. *
  975. * Receive request for TARGET_RESET after receiving an firmware
  976. * event NOT_RESPONDING_EVENT, then put command in link list
  977. * and queue if task_queue already in use.
  978. *
  979. * @ioc
  980. * @sas_event_data
  981. *
  982. **/
  983. static void
  984. mptsas_target_reset_queue(MPT_ADAPTER *ioc,
  985. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
  986. {
  987. MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
  988. VirtTarget *vtarget = NULL;
  989. struct mptsas_target_reset_event *target_reset_list;
  990. u8 id, channel;
  991. id = sas_event_data->TargetID;
  992. channel = sas_event_data->Bus;
  993. vtarget = mptsas_find_vtarget(ioc, channel, id);
  994. if (vtarget) {
  995. mptsas_block_io_starget(vtarget->starget);
  996. vtarget->deleted = 1; /* block IO */
  997. }
  998. target_reset_list = kzalloc(sizeof(struct mptsas_target_reset_event),
  999. GFP_ATOMIC);
  1000. if (!target_reset_list) {
  1001. dfailprintk(ioc, printk(MYIOC_s_WARN_FMT
  1002. "%s, failed to allocate mem @%d..!!\n",
  1003. ioc->name, __func__, __LINE__));
  1004. return;
  1005. }
  1006. memcpy(&target_reset_list->sas_event_data, sas_event_data,
  1007. sizeof(*sas_event_data));
  1008. list_add_tail(&target_reset_list->list, &hd->target_reset_list);
  1009. target_reset_list->time_count = jiffies;
  1010. if (mptsas_target_reset(ioc, channel, id)) {
  1011. target_reset_list->target_reset_issued = 1;
  1012. }
  1013. }
  1014. /**
  1015. * mptsas_schedule_target_reset- send pending target reset
  1016. * @iocp: per adapter object
  1017. *
  1018. * This function will delete scheduled target reset from the list and
  1019. * try to send next target reset. This will be called from completion
  1020. * context of any Task management command.
  1021. */
  1022. void
  1023. mptsas_schedule_target_reset(void *iocp)
  1024. {
  1025. MPT_ADAPTER *ioc = (MPT_ADAPTER *)(iocp);
  1026. MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
  1027. struct list_head *head = &hd->target_reset_list;
  1028. struct mptsas_target_reset_event *target_reset_list;
  1029. u8 id, channel;
  1030. /*
  1031. * issue target reset to next device in the queue
  1032. */
  1033. if (list_empty(head))
  1034. return;
  1035. target_reset_list = list_entry(head->next,
  1036. struct mptsas_target_reset_event, list);
  1037. id = target_reset_list->sas_event_data.TargetID;
  1038. channel = target_reset_list->sas_event_data.Bus;
  1039. target_reset_list->time_count = jiffies;
  1040. if (mptsas_target_reset(ioc, channel, id))
  1041. target_reset_list->target_reset_issued = 1;
  1042. return;
  1043. }
  1044. /**
  1045. * mptsas_taskmgmt_complete - complete SAS task management function
  1046. * @ioc: Pointer to MPT_ADAPTER structure
  1047. *
  1048. * Completion for TARGET_RESET after NOT_RESPONDING_EVENT, enable work
  1049. * queue to finish off removing device from upper layers. then send next
  1050. * TARGET_RESET in the queue.
  1051. **/
  1052. static int
  1053. mptsas_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
  1054. {
  1055. MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
  1056. struct list_head *head = &hd->target_reset_list;
  1057. u8 id, channel;
  1058. struct mptsas_target_reset_event *target_reset_list;
  1059. SCSITaskMgmtReply_t *pScsiTmReply;
  1060. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TaskMgmt completed: "
  1061. "(mf = %p, mr = %p)\n", ioc->name, mf, mr));
  1062. pScsiTmReply = (SCSITaskMgmtReply_t *)mr;
  1063. if (!pScsiTmReply)
  1064. return 0;
  1065. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1066. "\tTaskMgmt completed: fw_channel = %d, fw_id = %d,\n"
  1067. "\ttask_type = 0x%02X, iocstatus = 0x%04X "
  1068. "loginfo = 0x%08X,\n\tresponse_code = 0x%02X, "
  1069. "term_cmnds = %d\n", ioc->name,
  1070. pScsiTmReply->Bus, pScsiTmReply->TargetID,
  1071. pScsiTmReply->TaskType,
  1072. le16_to_cpu(pScsiTmReply->IOCStatus),
  1073. le32_to_cpu(pScsiTmReply->IOCLogInfo),
  1074. pScsiTmReply->ResponseCode,
  1075. le32_to_cpu(pScsiTmReply->TerminationCount)));
  1076. if (pScsiTmReply->ResponseCode)
  1077. mptscsih_taskmgmt_response_code(ioc,
  1078. pScsiTmReply->ResponseCode);
  1079. if (pScsiTmReply->TaskType ==
  1080. MPI_SCSITASKMGMT_TASKTYPE_QUERY_TASK || pScsiTmReply->TaskType ==
  1081. MPI_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET) {
  1082. ioc->taskmgmt_cmds.status |= MPT_MGMT_STATUS_COMMAND_GOOD;
  1083. ioc->taskmgmt_cmds.status |= MPT_MGMT_STATUS_RF_VALID;
  1084. memcpy(ioc->taskmgmt_cmds.reply, mr,
  1085. min(MPT_DEFAULT_FRAME_SIZE, 4 * mr->u.reply.MsgLength));
  1086. if (ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_PENDING) {
  1087. ioc->taskmgmt_cmds.status &= ~MPT_MGMT_STATUS_PENDING;
  1088. complete(&ioc->taskmgmt_cmds.done);
  1089. return 1;
  1090. }
  1091. return 0;
  1092. }
  1093. mpt_clear_taskmgmt_in_progress_flag(ioc);
  1094. if (list_empty(head))
  1095. return 1;
  1096. target_reset_list = list_entry(head->next,
  1097. struct mptsas_target_reset_event, list);
  1098. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1099. "TaskMgmt: completed (%d seconds)\n",
  1100. ioc->name, jiffies_to_msecs(jiffies -
  1101. target_reset_list->time_count)/1000));
  1102. id = pScsiTmReply->TargetID;
  1103. channel = pScsiTmReply->Bus;
  1104. target_reset_list->time_count = jiffies;
  1105. /*
  1106. * retry target reset
  1107. */
  1108. if (!target_reset_list->target_reset_issued) {
  1109. if (mptsas_target_reset(ioc, channel, id))
  1110. target_reset_list->target_reset_issued = 1;
  1111. return 1;
  1112. }
  1113. /*
  1114. * enable work queue to remove device from upper layers
  1115. */
  1116. list_del(&target_reset_list->list);
  1117. if (!ioc->fw_events_off)
  1118. mptsas_queue_device_delete(ioc,
  1119. &target_reset_list->sas_event_data);
  1120. ioc->schedule_target_reset(ioc);
  1121. return 1;
  1122. }
  1123. /**
  1124. * mptscsih_ioc_reset
  1125. *
  1126. * @ioc
  1127. * @reset_phase
  1128. *
  1129. **/
  1130. static int
  1131. mptsas_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
  1132. {
  1133. MPT_SCSI_HOST *hd;
  1134. int rc;
  1135. rc = mptscsih_ioc_reset(ioc, reset_phase);
  1136. if ((ioc->bus_type != SAS) || (!rc))
  1137. return rc;
  1138. hd = shost_priv(ioc->sh);
  1139. if (!hd->ioc)
  1140. goto out;
  1141. switch (reset_phase) {
  1142. case MPT_IOC_SETUP_RESET:
  1143. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1144. "%s: MPT_IOC_SETUP_RESET\n", ioc->name, __func__));
  1145. mptsas_fw_event_off(ioc);
  1146. break;
  1147. case MPT_IOC_PRE_RESET:
  1148. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1149. "%s: MPT_IOC_PRE_RESET\n", ioc->name, __func__));
  1150. break;
  1151. case MPT_IOC_POST_RESET:
  1152. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1153. "%s: MPT_IOC_POST_RESET\n", ioc->name, __func__));
  1154. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_PENDING) {
  1155. ioc->sas_mgmt.status |= MPT_MGMT_STATUS_DID_IOCRESET;
  1156. complete(&ioc->sas_mgmt.done);
  1157. }
  1158. mptsas_cleanup_fw_event_q(ioc);
  1159. mptsas_queue_rescan(ioc);
  1160. break;
  1161. default:
  1162. break;
  1163. }
  1164. out:
  1165. return rc;
  1166. }
  1167. /**
  1168. * enum device_state -
  1169. * @DEVICE_RETRY: need to retry the TUR
  1170. * @DEVICE_ERROR: TUR return error, don't add device
  1171. * @DEVICE_READY: device can be added
  1172. *
  1173. */
  1174. enum device_state{
  1175. DEVICE_RETRY,
  1176. DEVICE_ERROR,
  1177. DEVICE_READY,
  1178. };
  1179. static int
  1180. mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc, struct mptsas_enclosure *enclosure,
  1181. u32 form, u32 form_specific)
  1182. {
  1183. ConfigExtendedPageHeader_t hdr;
  1184. CONFIGPARMS cfg;
  1185. SasEnclosurePage0_t *buffer;
  1186. dma_addr_t dma_handle;
  1187. int error;
  1188. __le64 le_identifier;
  1189. memset(&hdr, 0, sizeof(hdr));
  1190. hdr.PageVersion = MPI_SASENCLOSURE0_PAGEVERSION;
  1191. hdr.PageNumber = 0;
  1192. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1193. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_ENCLOSURE;
  1194. cfg.cfghdr.ehdr = &hdr;
  1195. cfg.physAddr = -1;
  1196. cfg.pageAddr = form + form_specific;
  1197. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1198. cfg.dir = 0; /* read */
  1199. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  1200. error = mpt_config(ioc, &cfg);
  1201. if (error)
  1202. goto out;
  1203. if (!hdr.ExtPageLength) {
  1204. error = -ENXIO;
  1205. goto out;
  1206. }
  1207. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1208. &dma_handle);
  1209. if (!buffer) {
  1210. error = -ENOMEM;
  1211. goto out;
  1212. }
  1213. cfg.physAddr = dma_handle;
  1214. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1215. error = mpt_config(ioc, &cfg);
  1216. if (error)
  1217. goto out_free_consistent;
  1218. /* save config data */
  1219. memcpy(&le_identifier, &buffer->EnclosureLogicalID, sizeof(__le64));
  1220. enclosure->enclosure_logical_id = le64_to_cpu(le_identifier);
  1221. enclosure->enclosure_handle = le16_to_cpu(buffer->EnclosureHandle);
  1222. enclosure->flags = le16_to_cpu(buffer->Flags);
  1223. enclosure->num_slot = le16_to_cpu(buffer->NumSlots);
  1224. enclosure->start_slot = le16_to_cpu(buffer->StartSlot);
  1225. enclosure->start_id = buffer->StartTargetID;
  1226. enclosure->start_channel = buffer->StartBus;
  1227. enclosure->sep_id = buffer->SEPTargetID;
  1228. enclosure->sep_channel = buffer->SEPBus;
  1229. out_free_consistent:
  1230. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1231. buffer, dma_handle);
  1232. out:
  1233. return error;
  1234. }
  1235. /**
  1236. * mptsas_add_end_device - report a new end device to sas transport layer
  1237. * @ioc: Pointer to MPT_ADAPTER structure
  1238. * @phy_info: describes attached device
  1239. *
  1240. * return (0) success (1) failure
  1241. *
  1242. **/
  1243. static int
  1244. mptsas_add_end_device(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info)
  1245. {
  1246. struct sas_rphy *rphy;
  1247. struct sas_port *port;
  1248. struct sas_identify identify;
  1249. char *ds = NULL;
  1250. u8 fw_id;
  1251. if (!phy_info) {
  1252. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1253. "%s: exit at line=%d\n", ioc->name,
  1254. __func__, __LINE__));
  1255. return 1;
  1256. }
  1257. fw_id = phy_info->attached.id;
  1258. if (mptsas_get_rphy(phy_info)) {
  1259. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1260. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1261. __func__, fw_id, __LINE__));
  1262. return 2;
  1263. }
  1264. port = mptsas_get_port(phy_info);
  1265. if (!port) {
  1266. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1267. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1268. __func__, fw_id, __LINE__));
  1269. return 3;
  1270. }
  1271. if (phy_info->attached.device_info &
  1272. MPI_SAS_DEVICE_INFO_SSP_TARGET)
  1273. ds = "ssp";
  1274. if (phy_info->attached.device_info &
  1275. MPI_SAS_DEVICE_INFO_STP_TARGET)
  1276. ds = "stp";
  1277. if (phy_info->attached.device_info &
  1278. MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  1279. ds = "sata";
  1280. printk(MYIOC_s_INFO_FMT "attaching %s device: fw_channel %d, fw_id %d,"
  1281. " phy %d, sas_addr 0x%llx\n", ioc->name, ds,
  1282. phy_info->attached.channel, phy_info->attached.id,
  1283. phy_info->attached.phy_id, (unsigned long long)
  1284. phy_info->attached.sas_address);
  1285. mptsas_parse_device_info(&identify, &phy_info->attached);
  1286. rphy = sas_end_device_alloc(port);
  1287. if (!rphy) {
  1288. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1289. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1290. __func__, fw_id, __LINE__));
  1291. return 5; /* non-fatal: an rphy can be added later */
  1292. }
  1293. rphy->identify = identify;
  1294. if (sas_rphy_add(rphy)) {
  1295. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1296. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1297. __func__, fw_id, __LINE__));
  1298. sas_rphy_free(rphy);
  1299. return 6;
  1300. }
  1301. mptsas_set_rphy(ioc, phy_info, rphy);
  1302. return 0;
  1303. }
  1304. /**
  1305. * mptsas_del_end_device - report a deleted end device to sas transport layer
  1306. * @ioc: Pointer to MPT_ADAPTER structure
  1307. * @phy_info: describes attached device
  1308. *
  1309. **/
  1310. static void
  1311. mptsas_del_end_device(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info)
  1312. {
  1313. struct sas_rphy *rphy;
  1314. struct sas_port *port;
  1315. struct mptsas_portinfo *port_info;
  1316. struct mptsas_phyinfo *phy_info_parent;
  1317. int i;
  1318. char *ds = NULL;
  1319. u8 fw_id;
  1320. u64 sas_address;
  1321. if (!phy_info)
  1322. return;
  1323. fw_id = phy_info->attached.id;
  1324. sas_address = phy_info->attached.sas_address;
  1325. if (!phy_info->port_details) {
  1326. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1327. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1328. __func__, fw_id, __LINE__));
  1329. return;
  1330. }
  1331. rphy = mptsas_get_rphy(phy_info);
  1332. if (!rphy) {
  1333. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1334. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1335. __func__, fw_id, __LINE__));
  1336. return;
  1337. }
  1338. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_INITIATOR
  1339. || phy_info->attached.device_info
  1340. & MPI_SAS_DEVICE_INFO_SMP_INITIATOR
  1341. || phy_info->attached.device_info
  1342. & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
  1343. ds = "initiator";
  1344. if (phy_info->attached.device_info &
  1345. MPI_SAS_DEVICE_INFO_SSP_TARGET)
  1346. ds = "ssp";
  1347. if (phy_info->attached.device_info &
  1348. MPI_SAS_DEVICE_INFO_STP_TARGET)
  1349. ds = "stp";
  1350. if (phy_info->attached.device_info &
  1351. MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  1352. ds = "sata";
  1353. dev_printk(KERN_DEBUG, &rphy->dev, MYIOC_s_FMT
  1354. "removing %s device: fw_channel %d, fw_id %d, phy %d,"
  1355. "sas_addr 0x%llx\n", ioc->name, ds, phy_info->attached.channel,
  1356. phy_info->attached.id, phy_info->attached.phy_id,
  1357. (unsigned long long) sas_address);
  1358. port = mptsas_get_port(phy_info);
  1359. if (!port) {
  1360. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1361. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1362. __func__, fw_id, __LINE__));
  1363. return;
  1364. }
  1365. port_info = phy_info->portinfo;
  1366. phy_info_parent = port_info->phy_info;
  1367. for (i = 0; i < port_info->num_phys; i++, phy_info_parent++) {
  1368. if (!phy_info_parent->phy)
  1369. continue;
  1370. if (phy_info_parent->attached.sas_address !=
  1371. sas_address)
  1372. continue;
  1373. dev_printk(KERN_DEBUG, &phy_info_parent->phy->dev,
  1374. MYIOC_s_FMT "delete phy %d, phy-obj (0x%p)\n",
  1375. ioc->name, phy_info_parent->phy_id,
  1376. phy_info_parent->phy);
  1377. sas_port_delete_phy(port, phy_info_parent->phy);
  1378. }
  1379. dev_printk(KERN_DEBUG, &port->dev, MYIOC_s_FMT
  1380. "delete port %d, sas_addr (0x%llx)\n", ioc->name,
  1381. port->port_identifier, (unsigned long long)sas_address);
  1382. sas_port_delete(port);
  1383. mptsas_set_port(ioc, phy_info, NULL);
  1384. mptsas_port_delete(ioc, phy_info->port_details);
  1385. }
  1386. static struct mptsas_phyinfo *
  1387. mptsas_refreshing_device_handles(MPT_ADAPTER *ioc,
  1388. struct mptsas_devinfo *sas_device)
  1389. {
  1390. struct mptsas_phyinfo *phy_info;
  1391. struct mptsas_portinfo *port_info;
  1392. int i;
  1393. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  1394. sas_device->sas_address);
  1395. if (!phy_info)
  1396. goto out;
  1397. port_info = phy_info->portinfo;
  1398. if (!port_info)
  1399. goto out;
  1400. mutex_lock(&ioc->sas_topology_mutex);
  1401. for (i = 0; i < port_info->num_phys; i++) {
  1402. if (port_info->phy_info[i].attached.sas_address !=
  1403. sas_device->sas_address)
  1404. continue;
  1405. port_info->phy_info[i].attached.channel = sas_device->channel;
  1406. port_info->phy_info[i].attached.id = sas_device->id;
  1407. port_info->phy_info[i].attached.sas_address =
  1408. sas_device->sas_address;
  1409. port_info->phy_info[i].attached.handle = sas_device->handle;
  1410. port_info->phy_info[i].attached.handle_parent =
  1411. sas_device->handle_parent;
  1412. port_info->phy_info[i].attached.handle_enclosure =
  1413. sas_device->handle_enclosure;
  1414. }
  1415. mutex_unlock(&ioc->sas_topology_mutex);
  1416. out:
  1417. return phy_info;
  1418. }
  1419. /**
  1420. * mptsas_firmware_event_work - work thread for processing fw events
  1421. * @work: work queue payload containing info describing the event
  1422. * Context: user
  1423. *
  1424. */
  1425. static void
  1426. mptsas_firmware_event_work(struct work_struct *work)
  1427. {
  1428. struct fw_event_work *fw_event =
  1429. container_of(work, struct fw_event_work, work.work);
  1430. MPT_ADAPTER *ioc = fw_event->ioc;
  1431. /* special rescan topology handling */
  1432. if (fw_event->event == -1) {
  1433. if (ioc->in_rescan) {
  1434. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1435. "%s: rescan ignored as it is in progress\n",
  1436. ioc->name, __func__));
  1437. return;
  1438. }
  1439. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: rescan after "
  1440. "reset\n", ioc->name, __func__));
  1441. ioc->in_rescan = 1;
  1442. mptsas_not_responding_devices(ioc);
  1443. mptsas_scan_sas_topology(ioc);
  1444. ioc->in_rescan = 0;
  1445. mptsas_free_fw_event(ioc, fw_event);
  1446. mptsas_fw_event_on(ioc);
  1447. return;
  1448. }
  1449. /* events handling turned off during host reset */
  1450. if (ioc->fw_events_off) {
  1451. mptsas_free_fw_event(ioc, fw_event);
  1452. return;
  1453. }
  1454. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: fw_event=(0x%p), "
  1455. "event = (0x%02x)\n", ioc->name, __func__, fw_event,
  1456. (fw_event->event & 0xFF)));
  1457. switch (fw_event->event) {
  1458. case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
  1459. mptsas_send_sas_event(fw_event);
  1460. break;
  1461. case MPI_EVENT_INTEGRATED_RAID:
  1462. mptsas_send_raid_event(fw_event);
  1463. break;
  1464. case MPI_EVENT_IR2:
  1465. mptsas_send_ir2_event(fw_event);
  1466. break;
  1467. case MPI_EVENT_PERSISTENT_TABLE_FULL:
  1468. mptbase_sas_persist_operation(ioc,
  1469. MPI_SAS_OP_CLEAR_NOT_PRESENT);
  1470. mptsas_free_fw_event(ioc, fw_event);
  1471. break;
  1472. case MPI_EVENT_SAS_BROADCAST_PRIMITIVE:
  1473. mptsas_broadcast_primitive_work(fw_event);
  1474. break;
  1475. case MPI_EVENT_SAS_EXPANDER_STATUS_CHANGE:
  1476. mptsas_send_expander_event(fw_event);
  1477. break;
  1478. case MPI_EVENT_SAS_PHY_LINK_STATUS:
  1479. mptsas_send_link_status_event(fw_event);
  1480. break;
  1481. case MPI_EVENT_QUEUE_FULL:
  1482. mptsas_handle_queue_full_event(fw_event);
  1483. break;
  1484. }
  1485. }
  1486. static int
  1487. mptsas_slave_configure(struct scsi_device *sdev)
  1488. {
  1489. struct Scsi_Host *host = sdev->host;
  1490. MPT_SCSI_HOST *hd = shost_priv(host);
  1491. MPT_ADAPTER *ioc = hd->ioc;
  1492. VirtDevice *vdevice = sdev->hostdata;
  1493. if (vdevice->vtarget->deleted) {
  1494. sdev_printk(KERN_INFO, sdev, "clearing deleted flag\n");
  1495. vdevice->vtarget->deleted = 0;
  1496. }
  1497. /*
  1498. * RAID volumes placed beyond the last expected port.
  1499. * Ignore sending sas mode pages in that case..
  1500. */
  1501. if (sdev->channel == MPTSAS_RAID_CHANNEL) {
  1502. mptsas_add_device_component_starget_ir(ioc, scsi_target(sdev));
  1503. goto out;
  1504. }
  1505. sas_read_port_mode_page(sdev);
  1506. mptsas_add_device_component_starget(ioc, scsi_target(sdev));
  1507. out:
  1508. return mptscsih_slave_configure(sdev);
  1509. }
  1510. static int
  1511. mptsas_target_alloc(struct scsi_target *starget)
  1512. {
  1513. struct Scsi_Host *host = dev_to_shost(&starget->dev);
  1514. MPT_SCSI_HOST *hd = shost_priv(host);
  1515. VirtTarget *vtarget;
  1516. u8 id, channel;
  1517. struct sas_rphy *rphy;
  1518. struct mptsas_portinfo *p;
  1519. int i;
  1520. MPT_ADAPTER *ioc = hd->ioc;
  1521. vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
  1522. if (!vtarget)
  1523. return -ENOMEM;
  1524. vtarget->starget = starget;
  1525. vtarget->ioc_id = ioc->id;
  1526. vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
  1527. id = starget->id;
  1528. channel = 0;
  1529. /*
  1530. * RAID volumes placed beyond the last expected port.
  1531. */
  1532. if (starget->channel == MPTSAS_RAID_CHANNEL) {
  1533. if (!ioc->raid_data.pIocPg2) {
  1534. kfree(vtarget);
  1535. return -ENXIO;
  1536. }
  1537. for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  1538. if (id == ioc->raid_data.pIocPg2->
  1539. RaidVolume[i].VolumeID) {
  1540. channel = ioc->raid_data.pIocPg2->
  1541. RaidVolume[i].VolumeBus;
  1542. }
  1543. }
  1544. vtarget->raidVolume = 1;
  1545. goto out;
  1546. }
  1547. rphy = dev_to_rphy(starget->dev.parent);
  1548. mutex_lock(&ioc->sas_topology_mutex);
  1549. list_for_each_entry(p, &ioc->sas_topology, list) {
  1550. for (i = 0; i < p->num_phys; i++) {
  1551. if (p->phy_info[i].attached.sas_address !=
  1552. rphy->identify.sas_address)
  1553. continue;
  1554. id = p->phy_info[i].attached.id;
  1555. channel = p->phy_info[i].attached.channel;
  1556. mptsas_set_starget(&p->phy_info[i], starget);
  1557. /*
  1558. * Exposing hidden raid components
  1559. */
  1560. if (mptscsih_is_phys_disk(ioc, channel, id)) {
  1561. id = mptscsih_raid_id_to_num(ioc,
  1562. channel, id);
  1563. vtarget->tflags |=
  1564. MPT_TARGET_FLAGS_RAID_COMPONENT;
  1565. p->phy_info[i].attached.phys_disk_num = id;
  1566. }
  1567. mutex_unlock(&ioc->sas_topology_mutex);
  1568. goto out;
  1569. }
  1570. }
  1571. mutex_unlock(&ioc->sas_topology_mutex);
  1572. kfree(vtarget);
  1573. return -ENXIO;
  1574. out:
  1575. vtarget->id = id;
  1576. vtarget->channel = channel;
  1577. starget->hostdata = vtarget;
  1578. return 0;
  1579. }
  1580. static void
  1581. mptsas_target_destroy(struct scsi_target *starget)
  1582. {
  1583. struct Scsi_Host *host = dev_to_shost(&starget->dev);
  1584. MPT_SCSI_HOST *hd = shost_priv(host);
  1585. struct sas_rphy *rphy;
  1586. struct mptsas_portinfo *p;
  1587. int i;
  1588. MPT_ADAPTER *ioc = hd->ioc;
  1589. VirtTarget *vtarget;
  1590. if (!starget->hostdata)
  1591. return;
  1592. vtarget = starget->hostdata;
  1593. mptsas_del_device_component_by_os(ioc, starget->channel,
  1594. starget->id);
  1595. if (starget->channel == MPTSAS_RAID_CHANNEL)
  1596. goto out;
  1597. rphy = dev_to_rphy(starget->dev.parent);
  1598. list_for_each_entry(p, &ioc->sas_topology, list) {
  1599. for (i = 0; i < p->num_phys; i++) {
  1600. if (p->phy_info[i].attached.sas_address !=
  1601. rphy->identify.sas_address)
  1602. continue;
  1603. starget_printk(KERN_INFO, starget, MYIOC_s_FMT
  1604. "delete device: fw_channel %d, fw_id %d, phy %d, "
  1605. "sas_addr 0x%llx\n", ioc->name,
  1606. p->phy_info[i].attached.channel,
  1607. p->phy_info[i].attached.id,
  1608. p->phy_info[i].attached.phy_id, (unsigned long long)
  1609. p->phy_info[i].attached.sas_address);
  1610. mptsas_set_starget(&p->phy_info[i], NULL);
  1611. }
  1612. }
  1613. out:
  1614. vtarget->starget = NULL;
  1615. kfree(starget->hostdata);
  1616. starget->hostdata = NULL;
  1617. }
  1618. static int
  1619. mptsas_slave_alloc(struct scsi_device *sdev)
  1620. {
  1621. struct Scsi_Host *host = sdev->host;
  1622. MPT_SCSI_HOST *hd = shost_priv(host);
  1623. struct sas_rphy *rphy;
  1624. struct mptsas_portinfo *p;
  1625. VirtDevice *vdevice;
  1626. struct scsi_target *starget;
  1627. int i;
  1628. MPT_ADAPTER *ioc = hd->ioc;
  1629. vdevice = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
  1630. if (!vdevice) {
  1631. printk(MYIOC_s_ERR_FMT "slave_alloc kzalloc(%zd) FAILED!\n",
  1632. ioc->name, sizeof(VirtDevice));
  1633. return -ENOMEM;
  1634. }
  1635. starget = scsi_target(sdev);
  1636. vdevice->vtarget = starget->hostdata;
  1637. if (sdev->channel == MPTSAS_RAID_CHANNEL)
  1638. goto out;
  1639. rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
  1640. mutex_lock(&ioc->sas_topology_mutex);
  1641. list_for_each_entry(p, &ioc->sas_topology, list) {
  1642. for (i = 0; i < p->num_phys; i++) {
  1643. if (p->phy_info[i].attached.sas_address !=
  1644. rphy->identify.sas_address)
  1645. continue;
  1646. vdevice->lun = sdev->lun;
  1647. /*
  1648. * Exposing hidden raid components
  1649. */
  1650. if (mptscsih_is_phys_disk(ioc,
  1651. p->phy_info[i].attached.channel,
  1652. p->phy_info[i].attached.id))
  1653. sdev->no_uld_attach = 1;
  1654. mutex_unlock(&ioc->sas_topology_mutex);
  1655. goto out;
  1656. }
  1657. }
  1658. mutex_unlock(&ioc->sas_topology_mutex);
  1659. kfree(vdevice);
  1660. return -ENXIO;
  1661. out:
  1662. vdevice->vtarget->num_luns++;
  1663. sdev->hostdata = vdevice;
  1664. return 0;
  1665. }
  1666. static int
  1667. mptsas_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *SCpnt)
  1668. {
  1669. MPT_SCSI_HOST *hd;
  1670. MPT_ADAPTER *ioc;
  1671. VirtDevice *vdevice = SCpnt->device->hostdata;
  1672. if (!vdevice || !vdevice->vtarget || vdevice->vtarget->deleted) {
  1673. SCpnt->result = DID_NO_CONNECT << 16;
  1674. SCpnt->scsi_done(SCpnt);
  1675. return 0;
  1676. }
  1677. hd = shost_priv(shost);
  1678. ioc = hd->ioc;
  1679. if (ioc->sas_discovery_quiesce_io)
  1680. return SCSI_MLQUEUE_HOST_BUSY;
  1681. if (ioc->debug_level & MPT_DEBUG_SCSI)
  1682. scsi_print_command(SCpnt);
  1683. return mptscsih_qcmd(SCpnt);
  1684. }
  1685. /**
  1686. * mptsas_mptsas_eh_timed_out - resets the scsi_cmnd timeout
  1687. * if the device under question is currently in the
  1688. * device removal delay.
  1689. * @sc: scsi command that the midlayer is about to time out
  1690. *
  1691. **/
  1692. static enum blk_eh_timer_return mptsas_eh_timed_out(struct scsi_cmnd *sc)
  1693. {
  1694. MPT_SCSI_HOST *hd;
  1695. MPT_ADAPTER *ioc;
  1696. VirtDevice *vdevice;
  1697. enum blk_eh_timer_return rc = BLK_EH_DONE;
  1698. hd = shost_priv(sc->device->host);
  1699. if (hd == NULL) {
  1700. printk(KERN_ERR MYNAM ": %s: Can't locate host! (sc=%p)\n",
  1701. __func__, sc);
  1702. goto done;
  1703. }
  1704. ioc = hd->ioc;
  1705. if (ioc->bus_type != SAS) {
  1706. printk(KERN_ERR MYNAM ": %s: Wrong bus type (sc=%p)\n",
  1707. __func__, sc);
  1708. goto done;
  1709. }
  1710. /* In case if IOC is in reset from internal context.
  1711. * Do not execute EEH for the same IOC. SML should to reset timer.
  1712. */
  1713. if (ioc->ioc_reset_in_progress) {
  1714. dtmprintk(ioc, printk(MYIOC_s_WARN_FMT ": %s: ioc is in reset,"
  1715. "SML need to reset the timer (sc=%p)\n",
  1716. ioc->name, __func__, sc));
  1717. rc = BLK_EH_RESET_TIMER;
  1718. }
  1719. vdevice = sc->device->hostdata;
  1720. if (vdevice && vdevice->vtarget && (vdevice->vtarget->inDMD
  1721. || vdevice->vtarget->deleted)) {
  1722. dtmprintk(ioc, printk(MYIOC_s_WARN_FMT ": %s: target removed "
  1723. "or in device removal delay (sc=%p)\n",
  1724. ioc->name, __func__, sc));
  1725. rc = BLK_EH_RESET_TIMER;
  1726. goto done;
  1727. }
  1728. done:
  1729. return rc;
  1730. }
  1731. static struct scsi_host_template mptsas_driver_template = {
  1732. .module = THIS_MODULE,
  1733. .proc_name = "mptsas",
  1734. .show_info = mptscsih_show_info,
  1735. .name = "MPT SAS Host",
  1736. .info = mptscsih_info,
  1737. .queuecommand = mptsas_qcmd,
  1738. .target_alloc = mptsas_target_alloc,
  1739. .slave_alloc = mptsas_slave_alloc,
  1740. .slave_configure = mptsas_slave_configure,
  1741. .target_destroy = mptsas_target_destroy,
  1742. .slave_destroy = mptscsih_slave_destroy,
  1743. .change_queue_depth = mptscsih_change_queue_depth,
  1744. .eh_timed_out = mptsas_eh_timed_out,
  1745. .eh_abort_handler = mptscsih_abort,
  1746. .eh_device_reset_handler = mptscsih_dev_reset,
  1747. .eh_host_reset_handler = mptscsih_host_reset,
  1748. .bios_param = mptscsih_bios_param,
  1749. .can_queue = MPT_SAS_CAN_QUEUE,
  1750. .this_id = -1,
  1751. .sg_tablesize = MPT_SCSI_SG_DEPTH,
  1752. .max_sectors = 8192,
  1753. .cmd_per_lun = 7,
  1754. .shost_attrs = mptscsih_host_attrs,
  1755. .no_write_same = 1,
  1756. };
  1757. static int mptsas_get_linkerrors(struct sas_phy *phy)
  1758. {
  1759. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  1760. ConfigExtendedPageHeader_t hdr;
  1761. CONFIGPARMS cfg;
  1762. SasPhyPage1_t *buffer;
  1763. dma_addr_t dma_handle;
  1764. int error;
  1765. /* FIXME: only have link errors on local phys */
  1766. if (!scsi_is_sas_phy_local(phy))
  1767. return -EINVAL;
  1768. hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
  1769. hdr.ExtPageLength = 0;
  1770. hdr.PageNumber = 1 /* page number 1*/;
  1771. hdr.Reserved1 = 0;
  1772. hdr.Reserved2 = 0;
  1773. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1774. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  1775. cfg.cfghdr.ehdr = &hdr;
  1776. cfg.physAddr = -1;
  1777. cfg.pageAddr = phy->identify.phy_identifier;
  1778. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1779. cfg.dir = 0; /* read */
  1780. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  1781. error = mpt_config(ioc, &cfg);
  1782. if (error)
  1783. return error;
  1784. if (!hdr.ExtPageLength)
  1785. return -ENXIO;
  1786. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1787. &dma_handle);
  1788. if (!buffer)
  1789. return -ENOMEM;
  1790. cfg.physAddr = dma_handle;
  1791. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1792. error = mpt_config(ioc, &cfg);
  1793. if (error)
  1794. goto out_free_consistent;
  1795. mptsas_print_phy_pg1(ioc, buffer);
  1796. phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
  1797. phy->running_disparity_error_count =
  1798. le32_to_cpu(buffer->RunningDisparityErrorCount);
  1799. phy->loss_of_dword_sync_count =
  1800. le32_to_cpu(buffer->LossDwordSynchCount);
  1801. phy->phy_reset_problem_count =
  1802. le32_to_cpu(buffer->PhyResetProblemCount);
  1803. out_free_consistent:
  1804. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1805. buffer, dma_handle);
  1806. return error;
  1807. }
  1808. static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
  1809. MPT_FRAME_HDR *reply)
  1810. {
  1811. ioc->sas_mgmt.status |= MPT_MGMT_STATUS_COMMAND_GOOD;
  1812. if (reply != NULL) {
  1813. ioc->sas_mgmt.status |= MPT_MGMT_STATUS_RF_VALID;
  1814. memcpy(ioc->sas_mgmt.reply, reply,
  1815. min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
  1816. }
  1817. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_PENDING) {
  1818. ioc->sas_mgmt.status &= ~MPT_MGMT_STATUS_PENDING;
  1819. complete(&ioc->sas_mgmt.done);
  1820. return 1;
  1821. }
  1822. return 0;
  1823. }
  1824. static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
  1825. {
  1826. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  1827. SasIoUnitControlRequest_t *req;
  1828. SasIoUnitControlReply_t *reply;
  1829. MPT_FRAME_HDR *mf;
  1830. MPIHeader_t *hdr;
  1831. unsigned long timeleft;
  1832. int error = -ERESTARTSYS;
  1833. /* FIXME: fusion doesn't allow non-local phy reset */
  1834. if (!scsi_is_sas_phy_local(phy))
  1835. return -EINVAL;
  1836. /* not implemented for expanders */
  1837. if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
  1838. return -ENXIO;
  1839. if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
  1840. goto out;
  1841. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  1842. if (!mf) {
  1843. error = -ENOMEM;
  1844. goto out_unlock;
  1845. }
  1846. hdr = (MPIHeader_t *) mf;
  1847. req = (SasIoUnitControlRequest_t *)mf;
  1848. memset(req, 0, sizeof(SasIoUnitControlRequest_t));
  1849. req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
  1850. req->MsgContext = hdr->MsgContext;
  1851. req->Operation = hard_reset ?
  1852. MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
  1853. req->PhyNum = phy->identify.phy_identifier;
  1854. INITIALIZE_MGMT_STATUS(ioc->sas_mgmt.status)
  1855. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  1856. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
  1857. 10 * HZ);
  1858. if (!(ioc->sas_mgmt.status & MPT_MGMT_STATUS_COMMAND_GOOD)) {
  1859. error = -ETIME;
  1860. mpt_free_msg_frame(ioc, mf);
  1861. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_DID_IOCRESET)
  1862. goto out_unlock;
  1863. if (!timeleft)
  1864. mpt_Soft_Hard_ResetHandler(ioc, CAN_SLEEP);
  1865. goto out_unlock;
  1866. }
  1867. /* a reply frame is expected */
  1868. if ((ioc->sas_mgmt.status &
  1869. MPT_MGMT_STATUS_RF_VALID) == 0) {
  1870. error = -ENXIO;
  1871. goto out_unlock;
  1872. }
  1873. /* process the completed Reply Message Frame */
  1874. reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
  1875. if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
  1876. printk(MYIOC_s_INFO_FMT "%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
  1877. ioc->name, __func__, reply->IOCStatus, reply->IOCLogInfo);
  1878. error = -ENXIO;
  1879. goto out_unlock;
  1880. }
  1881. error = 0;
  1882. out_unlock:
  1883. CLEAR_MGMT_STATUS(ioc->sas_mgmt.status)
  1884. mutex_unlock(&ioc->sas_mgmt.mutex);
  1885. out:
  1886. return error;
  1887. }
  1888. static int
  1889. mptsas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  1890. {
  1891. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  1892. int i, error;
  1893. struct mptsas_portinfo *p;
  1894. struct mptsas_enclosure enclosure_info;
  1895. u64 enclosure_handle;
  1896. mutex_lock(&ioc->sas_topology_mutex);
  1897. list_for_each_entry(p, &ioc->sas_topology, list) {
  1898. for (i = 0; i < p->num_phys; i++) {
  1899. if (p->phy_info[i].attached.sas_address ==
  1900. rphy->identify.sas_address) {
  1901. enclosure_handle = p->phy_info[i].
  1902. attached.handle_enclosure;
  1903. goto found_info;
  1904. }
  1905. }
  1906. }
  1907. mutex_unlock(&ioc->sas_topology_mutex);
  1908. return -ENXIO;
  1909. found_info:
  1910. mutex_unlock(&ioc->sas_topology_mutex);
  1911. memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
  1912. error = mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
  1913. (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
  1914. MPI_SAS_ENCLOS_PGAD_FORM_SHIFT), enclosure_handle);
  1915. if (!error)
  1916. *identifier = enclosure_info.enclosure_logical_id;
  1917. return error;
  1918. }
  1919. static int
  1920. mptsas_get_bay_identifier(struct sas_rphy *rphy)
  1921. {
  1922. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  1923. struct mptsas_portinfo *p;
  1924. int i, rc;
  1925. mutex_lock(&ioc->sas_topology_mutex);
  1926. list_for_each_entry(p, &ioc->sas_topology, list) {
  1927. for (i = 0; i < p->num_phys; i++) {
  1928. if (p->phy_info[i].attached.sas_address ==
  1929. rphy->identify.sas_address) {
  1930. rc = p->phy_info[i].attached.slot;
  1931. goto out;
  1932. }
  1933. }
  1934. }
  1935. rc = -ENXIO;
  1936. out:
  1937. mutex_unlock(&ioc->sas_topology_mutex);
  1938. return rc;
  1939. }
  1940. static void mptsas_smp_handler(struct bsg_job *job, struct Scsi_Host *shost,
  1941. struct sas_rphy *rphy)
  1942. {
  1943. MPT_ADAPTER *ioc = ((MPT_SCSI_HOST *) shost->hostdata)->ioc;
  1944. MPT_FRAME_HDR *mf;
  1945. SmpPassthroughRequest_t *smpreq;
  1946. int flagsLength;
  1947. unsigned long timeleft;
  1948. char *psge;
  1949. u64 sas_address = 0;
  1950. unsigned int reslen = 0;
  1951. int ret = -EINVAL;
  1952. /* do we need to support multiple segments? */
  1953. if (job->request_payload.sg_cnt > 1 ||
  1954. job->reply_payload.sg_cnt > 1) {
  1955. printk(MYIOC_s_ERR_FMT "%s: multiple segments req %u, rsp %u\n",
  1956. ioc->name, __func__, job->request_payload.payload_len,
  1957. job->reply_payload.payload_len);
  1958. goto out;
  1959. }
  1960. ret = mutex_lock_interruptible(&ioc->sas_mgmt.mutex);
  1961. if (ret)
  1962. goto out;
  1963. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  1964. if (!mf) {
  1965. ret = -ENOMEM;
  1966. goto out_unlock;
  1967. }
  1968. smpreq = (SmpPassthroughRequest_t *)mf;
  1969. memset(smpreq, 0, sizeof(*smpreq));
  1970. smpreq->RequestDataLength =
  1971. cpu_to_le16(job->request_payload.payload_len - 4);
  1972. smpreq->Function = MPI_FUNCTION_SMP_PASSTHROUGH;
  1973. if (rphy)
  1974. sas_address = rphy->identify.sas_address;
  1975. else {
  1976. struct mptsas_portinfo *port_info;
  1977. mutex_lock(&ioc->sas_topology_mutex);
  1978. port_info = ioc->hba_port_info;
  1979. if (port_info && port_info->phy_info)
  1980. sas_address =
  1981. port_info->phy_info[0].phy->identify.sas_address;
  1982. mutex_unlock(&ioc->sas_topology_mutex);
  1983. }
  1984. *((u64 *)&smpreq->SASAddress) = cpu_to_le64(sas_address);
  1985. psge = (char *)
  1986. (((int *) mf) + (offsetof(SmpPassthroughRequest_t, SGL) / 4));
  1987. /* request */
  1988. flagsLength = (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
  1989. MPI_SGE_FLAGS_END_OF_BUFFER |
  1990. MPI_SGE_FLAGS_DIRECTION)
  1991. << MPI_SGE_FLAGS_SHIFT;
  1992. if (!dma_map_sg(&ioc->pcidev->dev, job->request_payload.sg_list,
  1993. 1, PCI_DMA_BIDIRECTIONAL))
  1994. goto put_mf;
  1995. flagsLength |= (sg_dma_len(job->request_payload.sg_list) - 4);
  1996. ioc->add_sge(psge, flagsLength,
  1997. sg_dma_address(job->request_payload.sg_list));
  1998. psge += ioc->SGE_size;
  1999. /* response */
  2000. flagsLength = MPI_SGE_FLAGS_SIMPLE_ELEMENT |
  2001. MPI_SGE_FLAGS_SYSTEM_ADDRESS |
  2002. MPI_SGE_FLAGS_IOC_TO_HOST |
  2003. MPI_SGE_FLAGS_END_OF_BUFFER;
  2004. flagsLength = flagsLength << MPI_SGE_FLAGS_SHIFT;
  2005. if (!dma_map_sg(&ioc->pcidev->dev, job->reply_payload.sg_list,
  2006. 1, PCI_DMA_BIDIRECTIONAL))
  2007. goto unmap_out;
  2008. flagsLength |= sg_dma_len(job->reply_payload.sg_list) + 4;
  2009. ioc->add_sge(psge, flagsLength,
  2010. sg_dma_address(job->reply_payload.sg_list));
  2011. INITIALIZE_MGMT_STATUS(ioc->sas_mgmt.status)
  2012. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  2013. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done, 10 * HZ);
  2014. if (!(ioc->sas_mgmt.status & MPT_MGMT_STATUS_COMMAND_GOOD)) {
  2015. ret = -ETIME;
  2016. mpt_free_msg_frame(ioc, mf);
  2017. mf = NULL;
  2018. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_DID_IOCRESET)
  2019. goto unmap_in;
  2020. if (!timeleft)
  2021. mpt_Soft_Hard_ResetHandler(ioc, CAN_SLEEP);
  2022. goto unmap_in;
  2023. }
  2024. mf = NULL;
  2025. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_RF_VALID) {
  2026. SmpPassthroughReply_t *smprep;
  2027. smprep = (SmpPassthroughReply_t *)ioc->sas_mgmt.reply;
  2028. memcpy(job->reply, smprep, sizeof(*smprep));
  2029. job->reply_len = sizeof(*smprep);
  2030. reslen = smprep->ResponseDataLength;
  2031. } else {
  2032. printk(MYIOC_s_ERR_FMT
  2033. "%s: smp passthru reply failed to be returned\n",
  2034. ioc->name, __func__);
  2035. ret = -ENXIO;
  2036. }
  2037. unmap_in:
  2038. dma_unmap_sg(&ioc->pcidev->dev, job->reply_payload.sg_list, 1,
  2039. PCI_DMA_BIDIRECTIONAL);
  2040. unmap_out:
  2041. dma_unmap_sg(&ioc->pcidev->dev, job->request_payload.sg_list, 1,
  2042. PCI_DMA_BIDIRECTIONAL);
  2043. put_mf:
  2044. if (mf)
  2045. mpt_free_msg_frame(ioc, mf);
  2046. out_unlock:
  2047. CLEAR_MGMT_STATUS(ioc->sas_mgmt.status)
  2048. mutex_unlock(&ioc->sas_mgmt.mutex);
  2049. out:
  2050. bsg_job_done(job, ret, reslen);
  2051. }
  2052. static struct sas_function_template mptsas_transport_functions = {
  2053. .get_linkerrors = mptsas_get_linkerrors,
  2054. .get_enclosure_identifier = mptsas_get_enclosure_identifier,
  2055. .get_bay_identifier = mptsas_get_bay_identifier,
  2056. .phy_reset = mptsas_phy_reset,
  2057. .smp_handler = mptsas_smp_handler,
  2058. };
  2059. static struct scsi_transport_template *mptsas_transport_template;
  2060. static int
  2061. mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  2062. {
  2063. ConfigExtendedPageHeader_t hdr;
  2064. CONFIGPARMS cfg;
  2065. SasIOUnitPage0_t *buffer;
  2066. dma_addr_t dma_handle;
  2067. int error, i;
  2068. hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
  2069. hdr.ExtPageLength = 0;
  2070. hdr.PageNumber = 0;
  2071. hdr.Reserved1 = 0;
  2072. hdr.Reserved2 = 0;
  2073. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2074. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
  2075. cfg.cfghdr.ehdr = &hdr;
  2076. cfg.physAddr = -1;
  2077. cfg.pageAddr = 0;
  2078. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2079. cfg.dir = 0; /* read */
  2080. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2081. error = mpt_config(ioc, &cfg);
  2082. if (error)
  2083. goto out;
  2084. if (!hdr.ExtPageLength) {
  2085. error = -ENXIO;
  2086. goto out;
  2087. }
  2088. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2089. &dma_handle);
  2090. if (!buffer) {
  2091. error = -ENOMEM;
  2092. goto out;
  2093. }
  2094. cfg.physAddr = dma_handle;
  2095. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2096. error = mpt_config(ioc, &cfg);
  2097. if (error)
  2098. goto out_free_consistent;
  2099. port_info->num_phys = buffer->NumPhys;
  2100. port_info->phy_info = kcalloc(port_info->num_phys,
  2101. sizeof(struct mptsas_phyinfo), GFP_KERNEL);
  2102. if (!port_info->phy_info) {
  2103. error = -ENOMEM;
  2104. goto out_free_consistent;
  2105. }
  2106. ioc->nvdata_version_persistent =
  2107. le16_to_cpu(buffer->NvdataVersionPersistent);
  2108. ioc->nvdata_version_default =
  2109. le16_to_cpu(buffer->NvdataVersionDefault);
  2110. for (i = 0; i < port_info->num_phys; i++) {
  2111. mptsas_print_phy_data(ioc, &buffer->PhyData[i]);
  2112. port_info->phy_info[i].phy_id = i;
  2113. port_info->phy_info[i].port_id =
  2114. buffer->PhyData[i].Port;
  2115. port_info->phy_info[i].negotiated_link_rate =
  2116. buffer->PhyData[i].NegotiatedLinkRate;
  2117. port_info->phy_info[i].portinfo = port_info;
  2118. port_info->phy_info[i].handle =
  2119. le16_to_cpu(buffer->PhyData[i].ControllerDevHandle);
  2120. }
  2121. out_free_consistent:
  2122. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2123. buffer, dma_handle);
  2124. out:
  2125. return error;
  2126. }
  2127. static int
  2128. mptsas_sas_io_unit_pg1(MPT_ADAPTER *ioc)
  2129. {
  2130. ConfigExtendedPageHeader_t hdr;
  2131. CONFIGPARMS cfg;
  2132. SasIOUnitPage1_t *buffer;
  2133. dma_addr_t dma_handle;
  2134. int error;
  2135. u8 device_missing_delay;
  2136. memset(&hdr, 0, sizeof(ConfigExtendedPageHeader_t));
  2137. memset(&cfg, 0, sizeof(CONFIGPARMS));
  2138. cfg.cfghdr.ehdr = &hdr;
  2139. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2140. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2141. cfg.cfghdr.ehdr->PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2142. cfg.cfghdr.ehdr->ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
  2143. cfg.cfghdr.ehdr->PageVersion = MPI_SASIOUNITPAGE1_PAGEVERSION;
  2144. cfg.cfghdr.ehdr->PageNumber = 1;
  2145. error = mpt_config(ioc, &cfg);
  2146. if (error)
  2147. goto out;
  2148. if (!hdr.ExtPageLength) {
  2149. error = -ENXIO;
  2150. goto out;
  2151. }
  2152. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2153. &dma_handle);
  2154. if (!buffer) {
  2155. error = -ENOMEM;
  2156. goto out;
  2157. }
  2158. cfg.physAddr = dma_handle;
  2159. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2160. error = mpt_config(ioc, &cfg);
  2161. if (error)
  2162. goto out_free_consistent;
  2163. ioc->io_missing_delay =
  2164. le16_to_cpu(buffer->IODeviceMissingDelay);
  2165. device_missing_delay = buffer->ReportDeviceMissingDelay;
  2166. ioc->device_missing_delay = (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_UNIT_16) ?
  2167. (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16 :
  2168. device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2169. out_free_consistent:
  2170. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2171. buffer, dma_handle);
  2172. out:
  2173. return error;
  2174. }
  2175. static int
  2176. mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  2177. u32 form, u32 form_specific)
  2178. {
  2179. ConfigExtendedPageHeader_t hdr;
  2180. CONFIGPARMS cfg;
  2181. SasPhyPage0_t *buffer;
  2182. dma_addr_t dma_handle;
  2183. int error;
  2184. hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
  2185. hdr.ExtPageLength = 0;
  2186. hdr.PageNumber = 0;
  2187. hdr.Reserved1 = 0;
  2188. hdr.Reserved2 = 0;
  2189. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2190. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  2191. cfg.cfghdr.ehdr = &hdr;
  2192. cfg.dir = 0; /* read */
  2193. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2194. /* Get Phy Pg 0 for each Phy. */
  2195. cfg.physAddr = -1;
  2196. cfg.pageAddr = form + form_specific;
  2197. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2198. error = mpt_config(ioc, &cfg);
  2199. if (error)
  2200. goto out;
  2201. if (!hdr.ExtPageLength) {
  2202. error = -ENXIO;
  2203. goto out;
  2204. }
  2205. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2206. &dma_handle);
  2207. if (!buffer) {
  2208. error = -ENOMEM;
  2209. goto out;
  2210. }
  2211. cfg.physAddr = dma_handle;
  2212. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2213. error = mpt_config(ioc, &cfg);
  2214. if (error)
  2215. goto out_free_consistent;
  2216. mptsas_print_phy_pg0(ioc, buffer);
  2217. phy_info->hw_link_rate = buffer->HwLinkRate;
  2218. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  2219. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  2220. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  2221. out_free_consistent:
  2222. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2223. buffer, dma_handle);
  2224. out:
  2225. return error;
  2226. }
  2227. static int
  2228. mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
  2229. u32 form, u32 form_specific)
  2230. {
  2231. ConfigExtendedPageHeader_t hdr;
  2232. CONFIGPARMS cfg;
  2233. SasDevicePage0_t *buffer;
  2234. dma_addr_t dma_handle;
  2235. __le64 sas_address;
  2236. int error=0;
  2237. hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
  2238. hdr.ExtPageLength = 0;
  2239. hdr.PageNumber = 0;
  2240. hdr.Reserved1 = 0;
  2241. hdr.Reserved2 = 0;
  2242. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2243. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
  2244. cfg.cfghdr.ehdr = &hdr;
  2245. cfg.pageAddr = form + form_specific;
  2246. cfg.physAddr = -1;
  2247. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2248. cfg.dir = 0; /* read */
  2249. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2250. memset(device_info, 0, sizeof(struct mptsas_devinfo));
  2251. error = mpt_config(ioc, &cfg);
  2252. if (error)
  2253. goto out;
  2254. if (!hdr.ExtPageLength) {
  2255. error = -ENXIO;
  2256. goto out;
  2257. }
  2258. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2259. &dma_handle);
  2260. if (!buffer) {
  2261. error = -ENOMEM;
  2262. goto out;
  2263. }
  2264. cfg.physAddr = dma_handle;
  2265. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2266. error = mpt_config(ioc, &cfg);
  2267. if (error == MPI_IOCSTATUS_CONFIG_INVALID_PAGE) {
  2268. error = -ENODEV;
  2269. goto out_free_consistent;
  2270. }
  2271. if (error)
  2272. goto out_free_consistent;
  2273. mptsas_print_device_pg0(ioc, buffer);
  2274. memset(device_info, 0, sizeof(struct mptsas_devinfo));
  2275. device_info->handle = le16_to_cpu(buffer->DevHandle);
  2276. device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
  2277. device_info->handle_enclosure =
  2278. le16_to_cpu(buffer->EnclosureHandle);
  2279. device_info->slot = le16_to_cpu(buffer->Slot);
  2280. device_info->phy_id = buffer->PhyNum;
  2281. device_info->port_id = buffer->PhysicalPort;
  2282. device_info->id = buffer->TargetID;
  2283. device_info->phys_disk_num = ~0;
  2284. device_info->channel = buffer->Bus;
  2285. memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
  2286. device_info->sas_address = le64_to_cpu(sas_address);
  2287. device_info->device_info =
  2288. le32_to_cpu(buffer->DeviceInfo);
  2289. device_info->flags = le16_to_cpu(buffer->Flags);
  2290. out_free_consistent:
  2291. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2292. buffer, dma_handle);
  2293. out:
  2294. return error;
  2295. }
  2296. static int
  2297. mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
  2298. u32 form, u32 form_specific)
  2299. {
  2300. ConfigExtendedPageHeader_t hdr;
  2301. CONFIGPARMS cfg;
  2302. SasExpanderPage0_t *buffer;
  2303. dma_addr_t dma_handle;
  2304. int i, error;
  2305. __le64 sas_address;
  2306. memset(port_info, 0, sizeof(struct mptsas_portinfo));
  2307. hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
  2308. hdr.ExtPageLength = 0;
  2309. hdr.PageNumber = 0;
  2310. hdr.Reserved1 = 0;
  2311. hdr.Reserved2 = 0;
  2312. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2313. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  2314. cfg.cfghdr.ehdr = &hdr;
  2315. cfg.physAddr = -1;
  2316. cfg.pageAddr = form + form_specific;
  2317. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2318. cfg.dir = 0; /* read */
  2319. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2320. memset(port_info, 0, sizeof(struct mptsas_portinfo));
  2321. error = mpt_config(ioc, &cfg);
  2322. if (error)
  2323. goto out;
  2324. if (!hdr.ExtPageLength) {
  2325. error = -ENXIO;
  2326. goto out;
  2327. }
  2328. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2329. &dma_handle);
  2330. if (!buffer) {
  2331. error = -ENOMEM;
  2332. goto out;
  2333. }
  2334. cfg.physAddr = dma_handle;
  2335. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2336. error = mpt_config(ioc, &cfg);
  2337. if (error == MPI_IOCSTATUS_CONFIG_INVALID_PAGE) {
  2338. error = -ENODEV;
  2339. goto out_free_consistent;
  2340. }
  2341. if (error)
  2342. goto out_free_consistent;
  2343. /* save config data */
  2344. port_info->num_phys = (buffer->NumPhys) ? buffer->NumPhys : 1;
  2345. port_info->phy_info = kcalloc(port_info->num_phys,
  2346. sizeof(struct mptsas_phyinfo), GFP_KERNEL);
  2347. if (!port_info->phy_info) {
  2348. error = -ENOMEM;
  2349. goto out_free_consistent;
  2350. }
  2351. memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
  2352. for (i = 0; i < port_info->num_phys; i++) {
  2353. port_info->phy_info[i].portinfo = port_info;
  2354. port_info->phy_info[i].handle =
  2355. le16_to_cpu(buffer->DevHandle);
  2356. port_info->phy_info[i].identify.sas_address =
  2357. le64_to_cpu(sas_address);
  2358. port_info->phy_info[i].identify.handle_parent =
  2359. le16_to_cpu(buffer->ParentDevHandle);
  2360. }
  2361. out_free_consistent:
  2362. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2363. buffer, dma_handle);
  2364. out:
  2365. return error;
  2366. }
  2367. static int
  2368. mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  2369. u32 form, u32 form_specific)
  2370. {
  2371. ConfigExtendedPageHeader_t hdr;
  2372. CONFIGPARMS cfg;
  2373. SasExpanderPage1_t *buffer;
  2374. dma_addr_t dma_handle;
  2375. int error=0;
  2376. hdr.PageVersion = MPI_SASEXPANDER1_PAGEVERSION;
  2377. hdr.ExtPageLength = 0;
  2378. hdr.PageNumber = 1;
  2379. hdr.Reserved1 = 0;
  2380. hdr.Reserved2 = 0;
  2381. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2382. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  2383. cfg.cfghdr.ehdr = &hdr;
  2384. cfg.physAddr = -1;
  2385. cfg.pageAddr = form + form_specific;
  2386. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2387. cfg.dir = 0; /* read */
  2388. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2389. error = mpt_config(ioc, &cfg);
  2390. if (error)
  2391. goto out;
  2392. if (!hdr.ExtPageLength) {
  2393. error = -ENXIO;
  2394. goto out;
  2395. }
  2396. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2397. &dma_handle);
  2398. if (!buffer) {
  2399. error = -ENOMEM;
  2400. goto out;
  2401. }
  2402. cfg.physAddr = dma_handle;
  2403. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2404. error = mpt_config(ioc, &cfg);
  2405. if (error == MPI_IOCSTATUS_CONFIG_INVALID_PAGE) {
  2406. error = -ENODEV;
  2407. goto out_free_consistent;
  2408. }
  2409. if (error)
  2410. goto out_free_consistent;
  2411. mptsas_print_expander_pg1(ioc, buffer);
  2412. /* save config data */
  2413. phy_info->phy_id = buffer->PhyIdentifier;
  2414. phy_info->port_id = buffer->PhysicalPort;
  2415. phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
  2416. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  2417. phy_info->hw_link_rate = buffer->HwLinkRate;
  2418. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  2419. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  2420. out_free_consistent:
  2421. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2422. buffer, dma_handle);
  2423. out:
  2424. return error;
  2425. }
  2426. struct rep_manu_request{
  2427. u8 smp_frame_type;
  2428. u8 function;
  2429. u8 reserved;
  2430. u8 request_length;
  2431. };
  2432. struct rep_manu_reply{
  2433. u8 smp_frame_type; /* 0x41 */
  2434. u8 function; /* 0x01 */
  2435. u8 function_result;
  2436. u8 response_length;
  2437. u16 expander_change_count;
  2438. u8 reserved0[2];
  2439. u8 sas_format:1;
  2440. u8 reserved1:7;
  2441. u8 reserved2[3];
  2442. u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
  2443. u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
  2444. u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
  2445. u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
  2446. u16 component_id;
  2447. u8 component_revision_id;
  2448. u8 reserved3;
  2449. u8 vendor_specific[8];
  2450. };
  2451. /**
  2452. * mptsas_exp_repmanufacture_info -
  2453. * @ioc: per adapter object
  2454. * @sas_address: expander sas address
  2455. * @edev: the sas_expander_device object
  2456. *
  2457. * Fills in the sas_expander_device object when SMP port is created.
  2458. *
  2459. * Returns 0 for success, non-zero for failure.
  2460. */
  2461. static int
  2462. mptsas_exp_repmanufacture_info(MPT_ADAPTER *ioc,
  2463. u64 sas_address, struct sas_expander_device *edev)
  2464. {
  2465. MPT_FRAME_HDR *mf;
  2466. SmpPassthroughRequest_t *smpreq;
  2467. SmpPassthroughReply_t *smprep;
  2468. struct rep_manu_reply *manufacture_reply;
  2469. struct rep_manu_request *manufacture_request;
  2470. int ret;
  2471. int flagsLength;
  2472. unsigned long timeleft;
  2473. char *psge;
  2474. unsigned long flags;
  2475. void *data_out = NULL;
  2476. dma_addr_t data_out_dma = 0;
  2477. u32 sz;
  2478. spin_lock_irqsave(&ioc->taskmgmt_lock, flags);
  2479. if (ioc->ioc_reset_in_progress) {
  2480. spin_unlock_irqrestore(&ioc->taskmgmt_lock, flags);
  2481. printk(MYIOC_s_INFO_FMT "%s: host reset in progress!\n",
  2482. __func__, ioc->name);
  2483. return -EFAULT;
  2484. }
  2485. spin_unlock_irqrestore(&ioc->taskmgmt_lock, flags);
  2486. ret = mutex_lock_interruptible(&ioc->sas_mgmt.mutex);
  2487. if (ret)
  2488. goto out;
  2489. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  2490. if (!mf) {
  2491. ret = -ENOMEM;
  2492. goto out_unlock;
  2493. }
  2494. smpreq = (SmpPassthroughRequest_t *)mf;
  2495. memset(smpreq, 0, sizeof(*smpreq));
  2496. sz = sizeof(struct rep_manu_request) + sizeof(struct rep_manu_reply);
  2497. data_out = pci_alloc_consistent(ioc->pcidev, sz, &data_out_dma);
  2498. if (!data_out) {
  2499. printk(KERN_ERR "Memory allocation failure at %s:%d/%s()!\n",
  2500. __FILE__, __LINE__, __func__);
  2501. ret = -ENOMEM;
  2502. goto put_mf;
  2503. }
  2504. manufacture_request = data_out;
  2505. manufacture_request->smp_frame_type = 0x40;
  2506. manufacture_request->function = 1;
  2507. manufacture_request->reserved = 0;
  2508. manufacture_request->request_length = 0;
  2509. smpreq->Function = MPI_FUNCTION_SMP_PASSTHROUGH;
  2510. smpreq->PhysicalPort = 0xFF;
  2511. *((u64 *)&smpreq->SASAddress) = cpu_to_le64(sas_address);
  2512. smpreq->RequestDataLength = sizeof(struct rep_manu_request);
  2513. psge = (char *)
  2514. (((int *) mf) + (offsetof(SmpPassthroughRequest_t, SGL) / 4));
  2515. flagsLength = MPI_SGE_FLAGS_SIMPLE_ELEMENT |
  2516. MPI_SGE_FLAGS_SYSTEM_ADDRESS |
  2517. MPI_SGE_FLAGS_HOST_TO_IOC |
  2518. MPI_SGE_FLAGS_END_OF_BUFFER;
  2519. flagsLength = flagsLength << MPI_SGE_FLAGS_SHIFT;
  2520. flagsLength |= sizeof(struct rep_manu_request);
  2521. ioc->add_sge(psge, flagsLength, data_out_dma);
  2522. psge += ioc->SGE_size;
  2523. flagsLength = MPI_SGE_FLAGS_SIMPLE_ELEMENT |
  2524. MPI_SGE_FLAGS_SYSTEM_ADDRESS |
  2525. MPI_SGE_FLAGS_IOC_TO_HOST |
  2526. MPI_SGE_FLAGS_END_OF_BUFFER;
  2527. flagsLength = flagsLength << MPI_SGE_FLAGS_SHIFT;
  2528. flagsLength |= sizeof(struct rep_manu_reply);
  2529. ioc->add_sge(psge, flagsLength, data_out_dma +
  2530. sizeof(struct rep_manu_request));
  2531. INITIALIZE_MGMT_STATUS(ioc->sas_mgmt.status)
  2532. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  2533. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done, 10 * HZ);
  2534. if (!(ioc->sas_mgmt.status & MPT_MGMT_STATUS_COMMAND_GOOD)) {
  2535. ret = -ETIME;
  2536. mpt_free_msg_frame(ioc, mf);
  2537. mf = NULL;
  2538. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_DID_IOCRESET)
  2539. goto out_free;
  2540. if (!timeleft)
  2541. mpt_Soft_Hard_ResetHandler(ioc, CAN_SLEEP);
  2542. goto out_free;
  2543. }
  2544. mf = NULL;
  2545. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_RF_VALID) {
  2546. u8 *tmp;
  2547. smprep = (SmpPassthroughReply_t *)ioc->sas_mgmt.reply;
  2548. if (le16_to_cpu(smprep->ResponseDataLength) !=
  2549. sizeof(struct rep_manu_reply))
  2550. goto out_free;
  2551. manufacture_reply = data_out + sizeof(struct rep_manu_request);
  2552. strncpy(edev->vendor_id, manufacture_reply->vendor_id,
  2553. SAS_EXPANDER_VENDOR_ID_LEN);
  2554. strncpy(edev->product_id, manufacture_reply->product_id,
  2555. SAS_EXPANDER_PRODUCT_ID_LEN);
  2556. strncpy(edev->product_rev, manufacture_reply->product_rev,
  2557. SAS_EXPANDER_PRODUCT_REV_LEN);
  2558. edev->level = manufacture_reply->sas_format;
  2559. if (manufacture_reply->sas_format) {
  2560. strncpy(edev->component_vendor_id,
  2561. manufacture_reply->component_vendor_id,
  2562. SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
  2563. tmp = (u8 *)&manufacture_reply->component_id;
  2564. edev->component_id = tmp[0] << 8 | tmp[1];
  2565. edev->component_revision_id =
  2566. manufacture_reply->component_revision_id;
  2567. }
  2568. } else {
  2569. printk(MYIOC_s_ERR_FMT
  2570. "%s: smp passthru reply failed to be returned\n",
  2571. ioc->name, __func__);
  2572. ret = -ENXIO;
  2573. }
  2574. out_free:
  2575. if (data_out_dma)
  2576. pci_free_consistent(ioc->pcidev, sz, data_out, data_out_dma);
  2577. put_mf:
  2578. if (mf)
  2579. mpt_free_msg_frame(ioc, mf);
  2580. out_unlock:
  2581. CLEAR_MGMT_STATUS(ioc->sas_mgmt.status)
  2582. mutex_unlock(&ioc->sas_mgmt.mutex);
  2583. out:
  2584. return ret;
  2585. }
  2586. static void
  2587. mptsas_parse_device_info(struct sas_identify *identify,
  2588. struct mptsas_devinfo *device_info)
  2589. {
  2590. u16 protocols;
  2591. identify->sas_address = device_info->sas_address;
  2592. identify->phy_identifier = device_info->phy_id;
  2593. /*
  2594. * Fill in Phy Initiator Port Protocol.
  2595. * Bits 6:3, more than one bit can be set, fall through cases.
  2596. */
  2597. protocols = device_info->device_info & 0x78;
  2598. identify->initiator_port_protocols = 0;
  2599. if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
  2600. identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
  2601. if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
  2602. identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
  2603. if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
  2604. identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
  2605. if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
  2606. identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
  2607. /*
  2608. * Fill in Phy Target Port Protocol.
  2609. * Bits 10:7, more than one bit can be set, fall through cases.
  2610. */
  2611. protocols = device_info->device_info & 0x780;
  2612. identify->target_port_protocols = 0;
  2613. if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
  2614. identify->target_port_protocols |= SAS_PROTOCOL_SSP;
  2615. if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
  2616. identify->target_port_protocols |= SAS_PROTOCOL_STP;
  2617. if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
  2618. identify->target_port_protocols |= SAS_PROTOCOL_SMP;
  2619. if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  2620. identify->target_port_protocols |= SAS_PROTOCOL_SATA;
  2621. /*
  2622. * Fill in Attached device type.
  2623. */
  2624. switch (device_info->device_info &
  2625. MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
  2626. case MPI_SAS_DEVICE_INFO_NO_DEVICE:
  2627. identify->device_type = SAS_PHY_UNUSED;
  2628. break;
  2629. case MPI_SAS_DEVICE_INFO_END_DEVICE:
  2630. identify->device_type = SAS_END_DEVICE;
  2631. break;
  2632. case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
  2633. identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
  2634. break;
  2635. case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
  2636. identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
  2637. break;
  2638. }
  2639. }
  2640. static int mptsas_probe_one_phy(struct device *dev,
  2641. struct mptsas_phyinfo *phy_info, int index, int local)
  2642. {
  2643. MPT_ADAPTER *ioc;
  2644. struct sas_phy *phy;
  2645. struct sas_port *port;
  2646. int error = 0;
  2647. VirtTarget *vtarget;
  2648. if (!dev) {
  2649. error = -ENODEV;
  2650. goto out;
  2651. }
  2652. if (!phy_info->phy) {
  2653. phy = sas_phy_alloc(dev, index);
  2654. if (!phy) {
  2655. error = -ENOMEM;
  2656. goto out;
  2657. }
  2658. } else
  2659. phy = phy_info->phy;
  2660. mptsas_parse_device_info(&phy->identify, &phy_info->identify);
  2661. /*
  2662. * Set Negotiated link rate.
  2663. */
  2664. switch (phy_info->negotiated_link_rate) {
  2665. case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
  2666. phy->negotiated_linkrate = SAS_PHY_DISABLED;
  2667. break;
  2668. case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
  2669. phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
  2670. break;
  2671. case MPI_SAS_IOUNIT0_RATE_1_5:
  2672. phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
  2673. break;
  2674. case MPI_SAS_IOUNIT0_RATE_3_0:
  2675. phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
  2676. break;
  2677. case MPI_SAS_IOUNIT0_RATE_6_0:
  2678. phy->negotiated_linkrate = SAS_LINK_RATE_6_0_GBPS;
  2679. break;
  2680. case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
  2681. case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
  2682. default:
  2683. phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
  2684. break;
  2685. }
  2686. /*
  2687. * Set Max hardware link rate.
  2688. */
  2689. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  2690. case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
  2691. phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  2692. break;
  2693. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  2694. phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  2695. break;
  2696. default:
  2697. break;
  2698. }
  2699. /*
  2700. * Set Max programmed link rate.
  2701. */
  2702. switch (phy_info->programmed_link_rate &
  2703. MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  2704. case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
  2705. phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  2706. break;
  2707. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  2708. phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  2709. break;
  2710. default:
  2711. break;
  2712. }
  2713. /*
  2714. * Set Min hardware link rate.
  2715. */
  2716. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
  2717. case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
  2718. phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  2719. break;
  2720. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  2721. phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  2722. break;
  2723. default:
  2724. break;
  2725. }
  2726. /*
  2727. * Set Min programmed link rate.
  2728. */
  2729. switch (phy_info->programmed_link_rate &
  2730. MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
  2731. case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
  2732. phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  2733. break;
  2734. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  2735. phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  2736. break;
  2737. default:
  2738. break;
  2739. }
  2740. if (!phy_info->phy) {
  2741. error = sas_phy_add(phy);
  2742. if (error) {
  2743. sas_phy_free(phy);
  2744. goto out;
  2745. }
  2746. phy_info->phy = phy;
  2747. }
  2748. if (!phy_info->attached.handle ||
  2749. !phy_info->port_details)
  2750. goto out;
  2751. port = mptsas_get_port(phy_info);
  2752. ioc = phy_to_ioc(phy_info->phy);
  2753. if (phy_info->sas_port_add_phy) {
  2754. if (!port) {
  2755. port = sas_port_alloc_num(dev);
  2756. if (!port) {
  2757. error = -ENOMEM;
  2758. goto out;
  2759. }
  2760. error = sas_port_add(port);
  2761. if (error) {
  2762. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2763. "%s: exit at line=%d\n", ioc->name,
  2764. __func__, __LINE__));
  2765. goto out;
  2766. }
  2767. mptsas_set_port(ioc, phy_info, port);
  2768. devtprintk(ioc, dev_printk(KERN_DEBUG, &port->dev,
  2769. MYIOC_s_FMT "add port %d, sas_addr (0x%llx)\n",
  2770. ioc->name, port->port_identifier,
  2771. (unsigned long long)phy_info->
  2772. attached.sas_address));
  2773. }
  2774. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2775. "sas_port_add_phy: phy_id=%d\n",
  2776. ioc->name, phy_info->phy_id));
  2777. sas_port_add_phy(port, phy_info->phy);
  2778. phy_info->sas_port_add_phy = 0;
  2779. devtprintk(ioc, dev_printk(KERN_DEBUG, &phy_info->phy->dev,
  2780. MYIOC_s_FMT "add phy %d, phy-obj (0x%p)\n", ioc->name,
  2781. phy_info->phy_id, phy_info->phy));
  2782. }
  2783. if (!mptsas_get_rphy(phy_info) && port && !port->rphy) {
  2784. struct sas_rphy *rphy;
  2785. struct device *parent;
  2786. struct sas_identify identify;
  2787. parent = dev->parent->parent;
  2788. /*
  2789. * Let the hotplug_work thread handle processing
  2790. * the adding/removing of devices that occur
  2791. * after start of day.
  2792. */
  2793. if (mptsas_is_end_device(&phy_info->attached) &&
  2794. phy_info->attached.handle_parent) {
  2795. goto out;
  2796. }
  2797. mptsas_parse_device_info(&identify, &phy_info->attached);
  2798. if (scsi_is_host_device(parent)) {
  2799. struct mptsas_portinfo *port_info;
  2800. int i;
  2801. port_info = ioc->hba_port_info;
  2802. for (i = 0; i < port_info->num_phys; i++)
  2803. if (port_info->phy_info[i].identify.sas_address ==
  2804. identify.sas_address) {
  2805. sas_port_mark_backlink(port);
  2806. goto out;
  2807. }
  2808. } else if (scsi_is_sas_rphy(parent)) {
  2809. struct sas_rphy *parent_rphy = dev_to_rphy(parent);
  2810. if (identify.sas_address ==
  2811. parent_rphy->identify.sas_address) {
  2812. sas_port_mark_backlink(port);
  2813. goto out;
  2814. }
  2815. }
  2816. switch (identify.device_type) {
  2817. case SAS_END_DEVICE:
  2818. rphy = sas_end_device_alloc(port);
  2819. break;
  2820. case SAS_EDGE_EXPANDER_DEVICE:
  2821. case SAS_FANOUT_EXPANDER_DEVICE:
  2822. rphy = sas_expander_alloc(port, identify.device_type);
  2823. break;
  2824. default:
  2825. rphy = NULL;
  2826. break;
  2827. }
  2828. if (!rphy) {
  2829. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2830. "%s: exit at line=%d\n", ioc->name,
  2831. __func__, __LINE__));
  2832. goto out;
  2833. }
  2834. rphy->identify = identify;
  2835. error = sas_rphy_add(rphy);
  2836. if (error) {
  2837. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2838. "%s: exit at line=%d\n", ioc->name,
  2839. __func__, __LINE__));
  2840. sas_rphy_free(rphy);
  2841. goto out;
  2842. }
  2843. mptsas_set_rphy(ioc, phy_info, rphy);
  2844. if (identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
  2845. identify.device_type == SAS_FANOUT_EXPANDER_DEVICE)
  2846. mptsas_exp_repmanufacture_info(ioc,
  2847. identify.sas_address,
  2848. rphy_to_expander_device(rphy));
  2849. }
  2850. /* If the device exists,verify it wasn't previously flagged
  2851. as a missing device. If so, clear it */
  2852. vtarget = mptsas_find_vtarget(ioc,
  2853. phy_info->attached.channel,
  2854. phy_info->attached.id);
  2855. if (vtarget && vtarget->inDMD) {
  2856. printk(KERN_INFO "Device returned, unsetting inDMD\n");
  2857. vtarget->inDMD = 0;
  2858. }
  2859. out:
  2860. return error;
  2861. }
  2862. static int
  2863. mptsas_probe_hba_phys(MPT_ADAPTER *ioc)
  2864. {
  2865. struct mptsas_portinfo *port_info, *hba;
  2866. int error = -ENOMEM, i;
  2867. hba = kzalloc(sizeof(struct mptsas_portinfo), GFP_KERNEL);
  2868. if (! hba)
  2869. goto out;
  2870. error = mptsas_sas_io_unit_pg0(ioc, hba);
  2871. if (error)
  2872. goto out_free_port_info;
  2873. mptsas_sas_io_unit_pg1(ioc);
  2874. mutex_lock(&ioc->sas_topology_mutex);
  2875. port_info = ioc->hba_port_info;
  2876. if (!port_info) {
  2877. ioc->hba_port_info = port_info = hba;
  2878. ioc->hba_port_num_phy = port_info->num_phys;
  2879. list_add_tail(&port_info->list, &ioc->sas_topology);
  2880. } else {
  2881. for (i = 0; i < hba->num_phys; i++) {
  2882. port_info->phy_info[i].negotiated_link_rate =
  2883. hba->phy_info[i].negotiated_link_rate;
  2884. port_info->phy_info[i].handle =
  2885. hba->phy_info[i].handle;
  2886. port_info->phy_info[i].port_id =
  2887. hba->phy_info[i].port_id;
  2888. }
  2889. kfree(hba->phy_info);
  2890. kfree(hba);
  2891. hba = NULL;
  2892. }
  2893. mutex_unlock(&ioc->sas_topology_mutex);
  2894. #if defined(CPQ_CIM)
  2895. ioc->num_ports = port_info->num_phys;
  2896. #endif
  2897. for (i = 0; i < port_info->num_phys; i++) {
  2898. mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
  2899. (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
  2900. MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
  2901. port_info->phy_info[i].identify.handle =
  2902. port_info->phy_info[i].handle;
  2903. mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
  2904. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  2905. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2906. port_info->phy_info[i].identify.handle);
  2907. if (!ioc->hba_port_sas_addr)
  2908. ioc->hba_port_sas_addr =
  2909. port_info->phy_info[i].identify.sas_address;
  2910. port_info->phy_info[i].identify.phy_id =
  2911. port_info->phy_info[i].phy_id = i;
  2912. if (port_info->phy_info[i].attached.handle)
  2913. mptsas_sas_device_pg0(ioc,
  2914. &port_info->phy_info[i].attached,
  2915. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  2916. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2917. port_info->phy_info[i].attached.handle);
  2918. }
  2919. mptsas_setup_wide_ports(ioc, port_info);
  2920. for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
  2921. mptsas_probe_one_phy(&ioc->sh->shost_gendev,
  2922. &port_info->phy_info[i], ioc->sas_index, 1);
  2923. return 0;
  2924. out_free_port_info:
  2925. kfree(hba);
  2926. out:
  2927. return error;
  2928. }
  2929. static void
  2930. mptsas_expander_refresh(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  2931. {
  2932. struct mptsas_portinfo *parent;
  2933. struct device *parent_dev;
  2934. struct sas_rphy *rphy;
  2935. int i;
  2936. u64 sas_address; /* expander sas address */
  2937. u32 handle;
  2938. handle = port_info->phy_info[0].handle;
  2939. sas_address = port_info->phy_info[0].identify.sas_address;
  2940. for (i = 0; i < port_info->num_phys; i++) {
  2941. mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
  2942. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
  2943. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + handle);
  2944. mptsas_sas_device_pg0(ioc,
  2945. &port_info->phy_info[i].identify,
  2946. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  2947. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2948. port_info->phy_info[i].identify.handle);
  2949. port_info->phy_info[i].identify.phy_id =
  2950. port_info->phy_info[i].phy_id;
  2951. if (port_info->phy_info[i].attached.handle) {
  2952. mptsas_sas_device_pg0(ioc,
  2953. &port_info->phy_info[i].attached,
  2954. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  2955. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2956. port_info->phy_info[i].attached.handle);
  2957. port_info->phy_info[i].attached.phy_id =
  2958. port_info->phy_info[i].phy_id;
  2959. }
  2960. }
  2961. mutex_lock(&ioc->sas_topology_mutex);
  2962. parent = mptsas_find_portinfo_by_handle(ioc,
  2963. port_info->phy_info[0].identify.handle_parent);
  2964. if (!parent) {
  2965. mutex_unlock(&ioc->sas_topology_mutex);
  2966. return;
  2967. }
  2968. for (i = 0, parent_dev = NULL; i < parent->num_phys && !parent_dev;
  2969. i++) {
  2970. if (parent->phy_info[i].attached.sas_address == sas_address) {
  2971. rphy = mptsas_get_rphy(&parent->phy_info[i]);
  2972. parent_dev = &rphy->dev;
  2973. }
  2974. }
  2975. mutex_unlock(&ioc->sas_topology_mutex);
  2976. mptsas_setup_wide_ports(ioc, port_info);
  2977. for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
  2978. mptsas_probe_one_phy(parent_dev, &port_info->phy_info[i],
  2979. ioc->sas_index, 0);
  2980. }
  2981. static void
  2982. mptsas_expander_event_add(MPT_ADAPTER *ioc,
  2983. MpiEventDataSasExpanderStatusChange_t *expander_data)
  2984. {
  2985. struct mptsas_portinfo *port_info;
  2986. int i;
  2987. __le64 sas_address;
  2988. port_info = kzalloc(sizeof(struct mptsas_portinfo), GFP_KERNEL);
  2989. if (!port_info)
  2990. BUG();
  2991. port_info->num_phys = (expander_data->NumPhys) ?
  2992. expander_data->NumPhys : 1;
  2993. port_info->phy_info = kcalloc(port_info->num_phys,
  2994. sizeof(struct mptsas_phyinfo), GFP_KERNEL);
  2995. if (!port_info->phy_info)
  2996. BUG();
  2997. memcpy(&sas_address, &expander_data->SASAddress, sizeof(__le64));
  2998. for (i = 0; i < port_info->num_phys; i++) {
  2999. port_info->phy_info[i].portinfo = port_info;
  3000. port_info->phy_info[i].handle =
  3001. le16_to_cpu(expander_data->DevHandle);
  3002. port_info->phy_info[i].identify.sas_address =
  3003. le64_to_cpu(sas_address);
  3004. port_info->phy_info[i].identify.handle_parent =
  3005. le16_to_cpu(expander_data->ParentDevHandle);
  3006. }
  3007. mutex_lock(&ioc->sas_topology_mutex);
  3008. list_add_tail(&port_info->list, &ioc->sas_topology);
  3009. mutex_unlock(&ioc->sas_topology_mutex);
  3010. printk(MYIOC_s_INFO_FMT "add expander: num_phys %d, "
  3011. "sas_addr (0x%llx)\n", ioc->name, port_info->num_phys,
  3012. (unsigned long long)sas_address);
  3013. mptsas_expander_refresh(ioc, port_info);
  3014. }
  3015. /**
  3016. * mptsas_delete_expander_siblings - remove siblings attached to expander
  3017. * @ioc: Pointer to MPT_ADAPTER structure
  3018. * @parent: the parent port_info object
  3019. * @expander: the expander port_info object
  3020. **/
  3021. static void
  3022. mptsas_delete_expander_siblings(MPT_ADAPTER *ioc, struct mptsas_portinfo
  3023. *parent, struct mptsas_portinfo *expander)
  3024. {
  3025. struct mptsas_phyinfo *phy_info;
  3026. struct mptsas_portinfo *port_info;
  3027. struct sas_rphy *rphy;
  3028. int i;
  3029. phy_info = expander->phy_info;
  3030. for (i = 0; i < expander->num_phys; i++, phy_info++) {
  3031. rphy = mptsas_get_rphy(phy_info);
  3032. if (!rphy)
  3033. continue;
  3034. if (rphy->identify.device_type == SAS_END_DEVICE)
  3035. mptsas_del_end_device(ioc, phy_info);
  3036. }
  3037. phy_info = expander->phy_info;
  3038. for (i = 0; i < expander->num_phys; i++, phy_info++) {
  3039. rphy = mptsas_get_rphy(phy_info);
  3040. if (!rphy)
  3041. continue;
  3042. if (rphy->identify.device_type ==
  3043. MPI_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  3044. rphy->identify.device_type ==
  3045. MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
  3046. port_info = mptsas_find_portinfo_by_sas_address(ioc,
  3047. rphy->identify.sas_address);
  3048. if (!port_info)
  3049. continue;
  3050. if (port_info == parent) /* backlink rphy */
  3051. continue;
  3052. /*
  3053. Delete this expander even if the expdevpage is exists
  3054. because the parent expander is already deleted
  3055. */
  3056. mptsas_expander_delete(ioc, port_info, 1);
  3057. }
  3058. }
  3059. }
  3060. /**
  3061. * mptsas_expander_delete - remove this expander
  3062. * @ioc: Pointer to MPT_ADAPTER structure
  3063. * @port_info: expander port_info struct
  3064. * @force: Flag to forcefully delete the expander
  3065. *
  3066. **/
  3067. static void mptsas_expander_delete(MPT_ADAPTER *ioc,
  3068. struct mptsas_portinfo *port_info, u8 force)
  3069. {
  3070. struct mptsas_portinfo *parent;
  3071. int i;
  3072. u64 expander_sas_address;
  3073. struct mptsas_phyinfo *phy_info;
  3074. struct mptsas_portinfo buffer;
  3075. struct mptsas_portinfo_details *port_details;
  3076. struct sas_port *port;
  3077. if (!port_info)
  3078. return;
  3079. /* see if expander is still there before deleting */
  3080. mptsas_sas_expander_pg0(ioc, &buffer,
  3081. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
  3082. MPI_SAS_EXPAND_PGAD_FORM_SHIFT),
  3083. port_info->phy_info[0].identify.handle);
  3084. if (buffer.num_phys) {
  3085. kfree(buffer.phy_info);
  3086. if (!force)
  3087. return;
  3088. }
  3089. /*
  3090. * Obtain the port_info instance to the parent port
  3091. */
  3092. port_details = NULL;
  3093. expander_sas_address =
  3094. port_info->phy_info[0].identify.sas_address;
  3095. parent = mptsas_find_portinfo_by_handle(ioc,
  3096. port_info->phy_info[0].identify.handle_parent);
  3097. mptsas_delete_expander_siblings(ioc, parent, port_info);
  3098. if (!parent)
  3099. goto out;
  3100. /*
  3101. * Delete rphys in the parent that point
  3102. * to this expander.
  3103. */
  3104. phy_info = parent->phy_info;
  3105. port = NULL;
  3106. for (i = 0; i < parent->num_phys; i++, phy_info++) {
  3107. if (!phy_info->phy)
  3108. continue;
  3109. if (phy_info->attached.sas_address !=
  3110. expander_sas_address)
  3111. continue;
  3112. if (!port) {
  3113. port = mptsas_get_port(phy_info);
  3114. port_details = phy_info->port_details;
  3115. }
  3116. dev_printk(KERN_DEBUG, &phy_info->phy->dev,
  3117. MYIOC_s_FMT "delete phy %d, phy-obj (0x%p)\n", ioc->name,
  3118. phy_info->phy_id, phy_info->phy);
  3119. sas_port_delete_phy(port, phy_info->phy);
  3120. }
  3121. if (port) {
  3122. dev_printk(KERN_DEBUG, &port->dev,
  3123. MYIOC_s_FMT "delete port %d, sas_addr (0x%llx)\n",
  3124. ioc->name, port->port_identifier,
  3125. (unsigned long long)expander_sas_address);
  3126. sas_port_delete(port);
  3127. mptsas_port_delete(ioc, port_details);
  3128. }
  3129. out:
  3130. printk(MYIOC_s_INFO_FMT "delete expander: num_phys %d, "
  3131. "sas_addr (0x%llx)\n", ioc->name, port_info->num_phys,
  3132. (unsigned long long)expander_sas_address);
  3133. /*
  3134. * free link
  3135. */
  3136. list_del(&port_info->list);
  3137. kfree(port_info->phy_info);
  3138. kfree(port_info);
  3139. }
  3140. /**
  3141. * mptsas_send_expander_event - expanders events
  3142. * @ioc: Pointer to MPT_ADAPTER structure
  3143. * @expander_data: event data
  3144. *
  3145. *
  3146. * This function handles adding, removing, and refreshing
  3147. * device handles within the expander objects.
  3148. */
  3149. static void
  3150. mptsas_send_expander_event(struct fw_event_work *fw_event)
  3151. {
  3152. MPT_ADAPTER *ioc;
  3153. MpiEventDataSasExpanderStatusChange_t *expander_data;
  3154. struct mptsas_portinfo *port_info;
  3155. __le64 sas_address;
  3156. int i;
  3157. ioc = fw_event->ioc;
  3158. expander_data = (MpiEventDataSasExpanderStatusChange_t *)
  3159. fw_event->event_data;
  3160. memcpy(&sas_address, &expander_data->SASAddress, sizeof(__le64));
  3161. sas_address = le64_to_cpu(sas_address);
  3162. port_info = mptsas_find_portinfo_by_sas_address(ioc, sas_address);
  3163. if (expander_data->ReasonCode == MPI_EVENT_SAS_EXP_RC_ADDED) {
  3164. if (port_info) {
  3165. for (i = 0; i < port_info->num_phys; i++) {
  3166. port_info->phy_info[i].portinfo = port_info;
  3167. port_info->phy_info[i].handle =
  3168. le16_to_cpu(expander_data->DevHandle);
  3169. port_info->phy_info[i].identify.sas_address =
  3170. le64_to_cpu(sas_address);
  3171. port_info->phy_info[i].identify.handle_parent =
  3172. le16_to_cpu(expander_data->ParentDevHandle);
  3173. }
  3174. mptsas_expander_refresh(ioc, port_info);
  3175. } else if (!port_info && expander_data->NumPhys)
  3176. mptsas_expander_event_add(ioc, expander_data);
  3177. } else if (expander_data->ReasonCode ==
  3178. MPI_EVENT_SAS_EXP_RC_NOT_RESPONDING)
  3179. mptsas_expander_delete(ioc, port_info, 0);
  3180. mptsas_free_fw_event(ioc, fw_event);
  3181. }
  3182. /**
  3183. * mptsas_expander_add -
  3184. * @ioc: Pointer to MPT_ADAPTER structure
  3185. * @handle:
  3186. *
  3187. */
  3188. static struct mptsas_portinfo *
  3189. mptsas_expander_add(MPT_ADAPTER *ioc, u16 handle)
  3190. {
  3191. struct mptsas_portinfo buffer, *port_info;
  3192. int i;
  3193. if ((mptsas_sas_expander_pg0(ioc, &buffer,
  3194. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
  3195. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), handle)))
  3196. return NULL;
  3197. port_info = kzalloc(sizeof(struct mptsas_portinfo), GFP_ATOMIC);
  3198. if (!port_info) {
  3199. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3200. "%s: exit at line=%d\n", ioc->name,
  3201. __func__, __LINE__));
  3202. return NULL;
  3203. }
  3204. port_info->num_phys = buffer.num_phys;
  3205. port_info->phy_info = buffer.phy_info;
  3206. for (i = 0; i < port_info->num_phys; i++)
  3207. port_info->phy_info[i].portinfo = port_info;
  3208. mutex_lock(&ioc->sas_topology_mutex);
  3209. list_add_tail(&port_info->list, &ioc->sas_topology);
  3210. mutex_unlock(&ioc->sas_topology_mutex);
  3211. printk(MYIOC_s_INFO_FMT "add expander: num_phys %d, "
  3212. "sas_addr (0x%llx)\n", ioc->name, port_info->num_phys,
  3213. (unsigned long long)buffer.phy_info[0].identify.sas_address);
  3214. mptsas_expander_refresh(ioc, port_info);
  3215. return port_info;
  3216. }
  3217. static void
  3218. mptsas_send_link_status_event(struct fw_event_work *fw_event)
  3219. {
  3220. MPT_ADAPTER *ioc;
  3221. MpiEventDataSasPhyLinkStatus_t *link_data;
  3222. struct mptsas_portinfo *port_info;
  3223. struct mptsas_phyinfo *phy_info = NULL;
  3224. __le64 sas_address;
  3225. u8 phy_num;
  3226. u8 link_rate;
  3227. ioc = fw_event->ioc;
  3228. link_data = (MpiEventDataSasPhyLinkStatus_t *)fw_event->event_data;
  3229. memcpy(&sas_address, &link_data->SASAddress, sizeof(__le64));
  3230. sas_address = le64_to_cpu(sas_address);
  3231. link_rate = link_data->LinkRates >> 4;
  3232. phy_num = link_data->PhyNum;
  3233. port_info = mptsas_find_portinfo_by_sas_address(ioc, sas_address);
  3234. if (port_info) {
  3235. phy_info = &port_info->phy_info[phy_num];
  3236. if (phy_info)
  3237. phy_info->negotiated_link_rate = link_rate;
  3238. }
  3239. if (link_rate == MPI_SAS_IOUNIT0_RATE_1_5 ||
  3240. link_rate == MPI_SAS_IOUNIT0_RATE_3_0 ||
  3241. link_rate == MPI_SAS_IOUNIT0_RATE_6_0) {
  3242. if (!port_info) {
  3243. if (ioc->old_sas_discovery_protocal) {
  3244. port_info = mptsas_expander_add(ioc,
  3245. le16_to_cpu(link_data->DevHandle));
  3246. if (port_info)
  3247. goto out;
  3248. }
  3249. goto out;
  3250. }
  3251. if (port_info == ioc->hba_port_info)
  3252. mptsas_probe_hba_phys(ioc);
  3253. else
  3254. mptsas_expander_refresh(ioc, port_info);
  3255. } else if (phy_info && phy_info->phy) {
  3256. if (link_rate == MPI_SAS_IOUNIT0_RATE_PHY_DISABLED)
  3257. phy_info->phy->negotiated_linkrate =
  3258. SAS_PHY_DISABLED;
  3259. else if (link_rate ==
  3260. MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION)
  3261. phy_info->phy->negotiated_linkrate =
  3262. SAS_LINK_RATE_FAILED;
  3263. else {
  3264. phy_info->phy->negotiated_linkrate =
  3265. SAS_LINK_RATE_UNKNOWN;
  3266. if (ioc->device_missing_delay &&
  3267. mptsas_is_end_device(&phy_info->attached)) {
  3268. struct scsi_device *sdev;
  3269. VirtDevice *vdevice;
  3270. u8 channel, id;
  3271. id = phy_info->attached.id;
  3272. channel = phy_info->attached.channel;
  3273. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  3274. "Link down for fw_id %d:fw_channel %d\n",
  3275. ioc->name, phy_info->attached.id,
  3276. phy_info->attached.channel));
  3277. shost_for_each_device(sdev, ioc->sh) {
  3278. vdevice = sdev->hostdata;
  3279. if ((vdevice == NULL) ||
  3280. (vdevice->vtarget == NULL))
  3281. continue;
  3282. if ((vdevice->vtarget->tflags &
  3283. MPT_TARGET_FLAGS_RAID_COMPONENT ||
  3284. vdevice->vtarget->raidVolume))
  3285. continue;
  3286. if (vdevice->vtarget->id == id &&
  3287. vdevice->vtarget->channel ==
  3288. channel)
  3289. devtprintk(ioc,
  3290. printk(MYIOC_s_DEBUG_FMT
  3291. "SDEV OUTSTANDING CMDS"
  3292. "%d\n", ioc->name,
  3293. atomic_read(&sdev->device_busy)));
  3294. }
  3295. }
  3296. }
  3297. }
  3298. out:
  3299. mptsas_free_fw_event(ioc, fw_event);
  3300. }
  3301. static void
  3302. mptsas_not_responding_devices(MPT_ADAPTER *ioc)
  3303. {
  3304. struct mptsas_portinfo buffer, *port_info;
  3305. struct mptsas_device_info *sas_info;
  3306. struct mptsas_devinfo sas_device;
  3307. u32 handle;
  3308. VirtTarget *vtarget = NULL;
  3309. struct mptsas_phyinfo *phy_info;
  3310. u8 found_expander;
  3311. int retval, retry_count;
  3312. unsigned long flags;
  3313. mpt_findImVolumes(ioc);
  3314. spin_lock_irqsave(&ioc->taskmgmt_lock, flags);
  3315. if (ioc->ioc_reset_in_progress) {
  3316. dfailprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  3317. "%s: exiting due to a parallel reset \n", ioc->name,
  3318. __func__));
  3319. spin_unlock_irqrestore(&ioc->taskmgmt_lock, flags);
  3320. return;
  3321. }
  3322. spin_unlock_irqrestore(&ioc->taskmgmt_lock, flags);
  3323. /* devices, logical volumes */
  3324. mutex_lock(&ioc->sas_device_info_mutex);
  3325. redo_device_scan:
  3326. list_for_each_entry(sas_info, &ioc->sas_device_info_list, list) {
  3327. if (sas_info->is_cached)
  3328. continue;
  3329. if (!sas_info->is_logical_volume) {
  3330. sas_device.handle = 0;
  3331. retry_count = 0;
  3332. retry_page:
  3333. retval = mptsas_sas_device_pg0(ioc, &sas_device,
  3334. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID
  3335. << MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  3336. (sas_info->fw.channel << 8) +
  3337. sas_info->fw.id);
  3338. if (sas_device.handle)
  3339. continue;
  3340. if (retval == -EBUSY) {
  3341. spin_lock_irqsave(&ioc->taskmgmt_lock, flags);
  3342. if (ioc->ioc_reset_in_progress) {
  3343. dfailprintk(ioc,
  3344. printk(MYIOC_s_DEBUG_FMT
  3345. "%s: exiting due to reset\n",
  3346. ioc->name, __func__));
  3347. spin_unlock_irqrestore
  3348. (&ioc->taskmgmt_lock, flags);
  3349. mutex_unlock(&ioc->
  3350. sas_device_info_mutex);
  3351. return;
  3352. }
  3353. spin_unlock_irqrestore(&ioc->taskmgmt_lock,
  3354. flags);
  3355. }
  3356. if (retval && (retval != -ENODEV)) {
  3357. if (retry_count < 10) {
  3358. retry_count++;
  3359. goto retry_page;
  3360. } else {
  3361. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  3362. "%s: Config page retry exceeded retry "
  3363. "count deleting device 0x%llx\n",
  3364. ioc->name, __func__,
  3365. sas_info->sas_address));
  3366. }
  3367. }
  3368. /* delete device */
  3369. vtarget = mptsas_find_vtarget(ioc,
  3370. sas_info->fw.channel, sas_info->fw.id);
  3371. if (vtarget)
  3372. vtarget->deleted = 1;
  3373. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  3374. sas_info->sas_address);
  3375. mptsas_del_end_device(ioc, phy_info);
  3376. goto redo_device_scan;
  3377. } else
  3378. mptsas_volume_delete(ioc, sas_info->fw.id);
  3379. }
  3380. mutex_unlock(&ioc->sas_device_info_mutex);
  3381. /* expanders */
  3382. mutex_lock(&ioc->sas_topology_mutex);
  3383. redo_expander_scan:
  3384. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  3385. if (!(port_info->phy_info[0].identify.device_info &
  3386. MPI_SAS_DEVICE_INFO_SMP_TARGET))
  3387. continue;
  3388. found_expander = 0;
  3389. handle = 0xFFFF;
  3390. while (!mptsas_sas_expander_pg0(ioc, &buffer,
  3391. (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
  3392. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), handle) &&
  3393. !found_expander) {
  3394. handle = buffer.phy_info[0].handle;
  3395. if (buffer.phy_info[0].identify.sas_address ==
  3396. port_info->phy_info[0].identify.sas_address) {
  3397. found_expander = 1;
  3398. }
  3399. kfree(buffer.phy_info);
  3400. }
  3401. if (!found_expander) {
  3402. mptsas_expander_delete(ioc, port_info, 0);
  3403. goto redo_expander_scan;
  3404. }
  3405. }
  3406. mutex_unlock(&ioc->sas_topology_mutex);
  3407. }
  3408. /**
  3409. * mptsas_probe_expanders - adding expanders
  3410. * @ioc: Pointer to MPT_ADAPTER structure
  3411. *
  3412. **/
  3413. static void
  3414. mptsas_probe_expanders(MPT_ADAPTER *ioc)
  3415. {
  3416. struct mptsas_portinfo buffer, *port_info;
  3417. u32 handle;
  3418. int i;
  3419. handle = 0xFFFF;
  3420. while (!mptsas_sas_expander_pg0(ioc, &buffer,
  3421. (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
  3422. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), handle)) {
  3423. handle = buffer.phy_info[0].handle;
  3424. port_info = mptsas_find_portinfo_by_sas_address(ioc,
  3425. buffer.phy_info[0].identify.sas_address);
  3426. if (port_info) {
  3427. /* refreshing handles */
  3428. for (i = 0; i < buffer.num_phys; i++) {
  3429. port_info->phy_info[i].handle = handle;
  3430. port_info->phy_info[i].identify.handle_parent =
  3431. buffer.phy_info[0].identify.handle_parent;
  3432. }
  3433. mptsas_expander_refresh(ioc, port_info);
  3434. kfree(buffer.phy_info);
  3435. continue;
  3436. }
  3437. port_info = kzalloc(sizeof(struct mptsas_portinfo), GFP_KERNEL);
  3438. if (!port_info) {
  3439. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3440. "%s: exit at line=%d\n", ioc->name,
  3441. __func__, __LINE__));
  3442. return;
  3443. }
  3444. port_info->num_phys = buffer.num_phys;
  3445. port_info->phy_info = buffer.phy_info;
  3446. for (i = 0; i < port_info->num_phys; i++)
  3447. port_info->phy_info[i].portinfo = port_info;
  3448. mutex_lock(&ioc->sas_topology_mutex);
  3449. list_add_tail(&port_info->list, &ioc->sas_topology);
  3450. mutex_unlock(&ioc->sas_topology_mutex);
  3451. printk(MYIOC_s_INFO_FMT "add expander: num_phys %d, "
  3452. "sas_addr (0x%llx)\n", ioc->name, port_info->num_phys,
  3453. (unsigned long long)buffer.phy_info[0].identify.sas_address);
  3454. mptsas_expander_refresh(ioc, port_info);
  3455. }
  3456. }
  3457. static void
  3458. mptsas_probe_devices(MPT_ADAPTER *ioc)
  3459. {
  3460. u16 handle;
  3461. struct mptsas_devinfo sas_device;
  3462. struct mptsas_phyinfo *phy_info;
  3463. handle = 0xFFFF;
  3464. while (!(mptsas_sas_device_pg0(ioc, &sas_device,
  3465. MPI_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  3466. handle = sas_device.handle;
  3467. if ((sas_device.device_info &
  3468. (MPI_SAS_DEVICE_INFO_SSP_TARGET |
  3469. MPI_SAS_DEVICE_INFO_STP_TARGET |
  3470. MPI_SAS_DEVICE_INFO_SATA_DEVICE)) == 0)
  3471. continue;
  3472. /* If there is no FW B_T mapping for this device then continue
  3473. * */
  3474. if (!(sas_device.flags & MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)
  3475. || !(sas_device.flags &
  3476. MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED))
  3477. continue;
  3478. phy_info = mptsas_refreshing_device_handles(ioc, &sas_device);
  3479. if (!phy_info)
  3480. continue;
  3481. if (mptsas_get_rphy(phy_info))
  3482. continue;
  3483. mptsas_add_end_device(ioc, phy_info);
  3484. }
  3485. }
  3486. /**
  3487. * mptsas_scan_sas_topology -
  3488. * @ioc: Pointer to MPT_ADAPTER structure
  3489. * @sas_address:
  3490. *
  3491. **/
  3492. static void
  3493. mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
  3494. {
  3495. struct scsi_device *sdev;
  3496. int i;
  3497. mptsas_probe_hba_phys(ioc);
  3498. mptsas_probe_expanders(ioc);
  3499. mptsas_probe_devices(ioc);
  3500. /*
  3501. Reporting RAID volumes.
  3502. */
  3503. if (!ioc->ir_firmware || !ioc->raid_data.pIocPg2 ||
  3504. !ioc->raid_data.pIocPg2->NumActiveVolumes)
  3505. return;
  3506. for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  3507. sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
  3508. ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
  3509. if (sdev) {
  3510. scsi_device_put(sdev);
  3511. continue;
  3512. }
  3513. printk(MYIOC_s_INFO_FMT "attaching raid volume, channel %d, "
  3514. "id %d\n", ioc->name, MPTSAS_RAID_CHANNEL,
  3515. ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID);
  3516. scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL,
  3517. ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
  3518. }
  3519. }
  3520. static void
  3521. mptsas_handle_queue_full_event(struct fw_event_work *fw_event)
  3522. {
  3523. MPT_ADAPTER *ioc;
  3524. EventDataQueueFull_t *qfull_data;
  3525. struct mptsas_device_info *sas_info;
  3526. struct scsi_device *sdev;
  3527. int depth;
  3528. int id = -1;
  3529. int channel = -1;
  3530. int fw_id, fw_channel;
  3531. u16 current_depth;
  3532. ioc = fw_event->ioc;
  3533. qfull_data = (EventDataQueueFull_t *)fw_event->event_data;
  3534. fw_id = qfull_data->TargetID;
  3535. fw_channel = qfull_data->Bus;
  3536. current_depth = le16_to_cpu(qfull_data->CurrentDepth);
  3537. /* if hidden raid component, look for the volume id */
  3538. mutex_lock(&ioc->sas_device_info_mutex);
  3539. if (mptscsih_is_phys_disk(ioc, fw_channel, fw_id)) {
  3540. list_for_each_entry(sas_info, &ioc->sas_device_info_list,
  3541. list) {
  3542. if (sas_info->is_cached ||
  3543. sas_info->is_logical_volume)
  3544. continue;
  3545. if (sas_info->is_hidden_raid_component &&
  3546. (sas_info->fw.channel == fw_channel &&
  3547. sas_info->fw.id == fw_id)) {
  3548. id = sas_info->volume_id;
  3549. channel = MPTSAS_RAID_CHANNEL;
  3550. goto out;
  3551. }
  3552. }
  3553. } else {
  3554. list_for_each_entry(sas_info, &ioc->sas_device_info_list,
  3555. list) {
  3556. if (sas_info->is_cached ||
  3557. sas_info->is_hidden_raid_component ||
  3558. sas_info->is_logical_volume)
  3559. continue;
  3560. if (sas_info->fw.channel == fw_channel &&
  3561. sas_info->fw.id == fw_id) {
  3562. id = sas_info->os.id;
  3563. channel = sas_info->os.channel;
  3564. goto out;
  3565. }
  3566. }
  3567. }
  3568. out:
  3569. mutex_unlock(&ioc->sas_device_info_mutex);
  3570. if (id != -1) {
  3571. shost_for_each_device(sdev, ioc->sh) {
  3572. if (sdev->id == id && sdev->channel == channel) {
  3573. if (current_depth > sdev->queue_depth) {
  3574. sdev_printk(KERN_INFO, sdev,
  3575. "strange observation, the queue "
  3576. "depth is (%d) meanwhile fw queue "
  3577. "depth (%d)\n", sdev->queue_depth,
  3578. current_depth);
  3579. continue;
  3580. }
  3581. depth = scsi_track_queue_full(sdev,
  3582. sdev->queue_depth - 1);
  3583. if (depth > 0)
  3584. sdev_printk(KERN_INFO, sdev,
  3585. "Queue depth reduced to (%d)\n",
  3586. depth);
  3587. else if (depth < 0)
  3588. sdev_printk(KERN_INFO, sdev,
  3589. "Tagged Command Queueing is being "
  3590. "disabled\n");
  3591. else if (depth == 0)
  3592. sdev_printk(KERN_DEBUG, sdev,
  3593. "Queue depth not changed yet\n");
  3594. }
  3595. }
  3596. }
  3597. mptsas_free_fw_event(ioc, fw_event);
  3598. }
  3599. static struct mptsas_phyinfo *
  3600. mptsas_find_phyinfo_by_sas_address(MPT_ADAPTER *ioc, u64 sas_address)
  3601. {
  3602. struct mptsas_portinfo *port_info;
  3603. struct mptsas_phyinfo *phy_info = NULL;
  3604. int i;
  3605. mutex_lock(&ioc->sas_topology_mutex);
  3606. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  3607. for (i = 0; i < port_info->num_phys; i++) {
  3608. if (!mptsas_is_end_device(
  3609. &port_info->phy_info[i].attached))
  3610. continue;
  3611. if (port_info->phy_info[i].attached.sas_address
  3612. != sas_address)
  3613. continue;
  3614. phy_info = &port_info->phy_info[i];
  3615. break;
  3616. }
  3617. }
  3618. mutex_unlock(&ioc->sas_topology_mutex);
  3619. return phy_info;
  3620. }
  3621. /**
  3622. * mptsas_find_phyinfo_by_phys_disk_num -
  3623. * @ioc: Pointer to MPT_ADAPTER structure
  3624. * @phys_disk_num:
  3625. * @channel:
  3626. * @id:
  3627. *
  3628. **/
  3629. static struct mptsas_phyinfo *
  3630. mptsas_find_phyinfo_by_phys_disk_num(MPT_ADAPTER *ioc, u8 phys_disk_num,
  3631. u8 channel, u8 id)
  3632. {
  3633. struct mptsas_phyinfo *phy_info = NULL;
  3634. struct mptsas_portinfo *port_info;
  3635. RaidPhysDiskPage1_t *phys_disk = NULL;
  3636. int num_paths;
  3637. u64 sas_address = 0;
  3638. int i;
  3639. phy_info = NULL;
  3640. if (!ioc->raid_data.pIocPg3)
  3641. return NULL;
  3642. /* dual port support */
  3643. num_paths = mpt_raid_phys_disk_get_num_paths(ioc, phys_disk_num);
  3644. if (!num_paths)
  3645. goto out;
  3646. phys_disk = kzalloc(offsetof(RaidPhysDiskPage1_t, Path) +
  3647. (num_paths * sizeof(RAID_PHYS_DISK1_PATH)), GFP_KERNEL);
  3648. if (!phys_disk)
  3649. goto out;
  3650. mpt_raid_phys_disk_pg1(ioc, phys_disk_num, phys_disk);
  3651. for (i = 0; i < num_paths; i++) {
  3652. if ((phys_disk->Path[i].Flags & 1) != 0)
  3653. /* entry no longer valid */
  3654. continue;
  3655. if ((id == phys_disk->Path[i].PhysDiskID) &&
  3656. (channel == phys_disk->Path[i].PhysDiskBus)) {
  3657. memcpy(&sas_address, &phys_disk->Path[i].WWID,
  3658. sizeof(u64));
  3659. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  3660. sas_address);
  3661. goto out;
  3662. }
  3663. }
  3664. out:
  3665. kfree(phys_disk);
  3666. if (phy_info)
  3667. return phy_info;
  3668. /*
  3669. * Extra code to handle RAID0 case, where the sas_address is not updated
  3670. * in phys_disk_page_1 when hotswapped
  3671. */
  3672. mutex_lock(&ioc->sas_topology_mutex);
  3673. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  3674. for (i = 0; i < port_info->num_phys && !phy_info; i++) {
  3675. if (!mptsas_is_end_device(
  3676. &port_info->phy_info[i].attached))
  3677. continue;
  3678. if (port_info->phy_info[i].attached.phys_disk_num == ~0)
  3679. continue;
  3680. if ((port_info->phy_info[i].attached.phys_disk_num ==
  3681. phys_disk_num) &&
  3682. (port_info->phy_info[i].attached.id == id) &&
  3683. (port_info->phy_info[i].attached.channel ==
  3684. channel))
  3685. phy_info = &port_info->phy_info[i];
  3686. }
  3687. }
  3688. mutex_unlock(&ioc->sas_topology_mutex);
  3689. return phy_info;
  3690. }
  3691. static void
  3692. mptsas_reprobe_lun(struct scsi_device *sdev, void *data)
  3693. {
  3694. int rc;
  3695. sdev->no_uld_attach = data ? 1 : 0;
  3696. rc = scsi_device_reprobe(sdev);
  3697. }
  3698. static void
  3699. mptsas_reprobe_target(struct scsi_target *starget, int uld_attach)
  3700. {
  3701. starget_for_each_device(starget, uld_attach ? (void *)1 : NULL,
  3702. mptsas_reprobe_lun);
  3703. }
  3704. static void
  3705. mptsas_adding_inactive_raid_components(MPT_ADAPTER *ioc, u8 channel, u8 id)
  3706. {
  3707. CONFIGPARMS cfg;
  3708. ConfigPageHeader_t hdr;
  3709. dma_addr_t dma_handle;
  3710. pRaidVolumePage0_t buffer = NULL;
  3711. RaidPhysDiskPage0_t phys_disk;
  3712. int i;
  3713. struct mptsas_phyinfo *phy_info;
  3714. struct mptsas_devinfo sas_device;
  3715. memset(&cfg, 0 , sizeof(CONFIGPARMS));
  3716. memset(&hdr, 0 , sizeof(ConfigPageHeader_t));
  3717. hdr.PageType = MPI_CONFIG_PAGETYPE_RAID_VOLUME;
  3718. cfg.pageAddr = (channel << 8) + id;
  3719. cfg.cfghdr.hdr = &hdr;
  3720. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  3721. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  3722. if (mpt_config(ioc, &cfg) != 0)
  3723. goto out;
  3724. if (!hdr.PageLength)
  3725. goto out;
  3726. buffer = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4,
  3727. &dma_handle);
  3728. if (!buffer)
  3729. goto out;
  3730. cfg.physAddr = dma_handle;
  3731. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  3732. if (mpt_config(ioc, &cfg) != 0)
  3733. goto out;
  3734. if (!(buffer->VolumeStatus.Flags &
  3735. MPI_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE))
  3736. goto out;
  3737. if (!buffer->NumPhysDisks)
  3738. goto out;
  3739. for (i = 0; i < buffer->NumPhysDisks; i++) {
  3740. if (mpt_raid_phys_disk_pg0(ioc,
  3741. buffer->PhysDisk[i].PhysDiskNum, &phys_disk) != 0)
  3742. continue;
  3743. if (mptsas_sas_device_pg0(ioc, &sas_device,
  3744. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  3745. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  3746. (phys_disk.PhysDiskBus << 8) +
  3747. phys_disk.PhysDiskID))
  3748. continue;
  3749. /* If there is no FW B_T mapping for this device then continue
  3750. * */
  3751. if (!(sas_device.flags & MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)
  3752. || !(sas_device.flags &
  3753. MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED))
  3754. continue;
  3755. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  3756. sas_device.sas_address);
  3757. mptsas_add_end_device(ioc, phy_info);
  3758. }
  3759. out:
  3760. if (buffer)
  3761. pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, buffer,
  3762. dma_handle);
  3763. }
  3764. /*
  3765. * Work queue thread to handle SAS hotplug events
  3766. */
  3767. static void
  3768. mptsas_hotplug_work(MPT_ADAPTER *ioc, struct fw_event_work *fw_event,
  3769. struct mptsas_hotplug_event *hot_plug_info)
  3770. {
  3771. struct mptsas_phyinfo *phy_info;
  3772. struct scsi_target * starget;
  3773. struct mptsas_devinfo sas_device;
  3774. VirtTarget *vtarget;
  3775. int i;
  3776. struct mptsas_portinfo *port_info;
  3777. switch (hot_plug_info->event_type) {
  3778. case MPTSAS_ADD_PHYSDISK:
  3779. if (!ioc->raid_data.pIocPg2)
  3780. break;
  3781. for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  3782. if (ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID ==
  3783. hot_plug_info->id) {
  3784. printk(MYIOC_s_WARN_FMT "firmware bug: unable "
  3785. "to add hidden disk - target_id matches "
  3786. "volume_id\n", ioc->name);
  3787. mptsas_free_fw_event(ioc, fw_event);
  3788. return;
  3789. }
  3790. }
  3791. mpt_findImVolumes(ioc);
  3792. /* fall through */
  3793. case MPTSAS_ADD_DEVICE:
  3794. memset(&sas_device, 0, sizeof(struct mptsas_devinfo));
  3795. mptsas_sas_device_pg0(ioc, &sas_device,
  3796. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  3797. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  3798. (hot_plug_info->channel << 8) +
  3799. hot_plug_info->id);
  3800. /* If there is no FW B_T mapping for this device then break
  3801. * */
  3802. if (!(sas_device.flags & MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)
  3803. || !(sas_device.flags &
  3804. MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED))
  3805. break;
  3806. if (!sas_device.handle)
  3807. return;
  3808. phy_info = mptsas_refreshing_device_handles(ioc, &sas_device);
  3809. /* Device hot plug */
  3810. if (!phy_info) {
  3811. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  3812. "%s %d HOT PLUG: "
  3813. "parent handle of device %x\n", ioc->name,
  3814. __func__, __LINE__, sas_device.handle_parent));
  3815. port_info = mptsas_find_portinfo_by_handle(ioc,
  3816. sas_device.handle_parent);
  3817. if (port_info == ioc->hba_port_info)
  3818. mptsas_probe_hba_phys(ioc);
  3819. else if (port_info)
  3820. mptsas_expander_refresh(ioc, port_info);
  3821. else {
  3822. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3823. "%s %d port info is NULL\n",
  3824. ioc->name, __func__, __LINE__));
  3825. break;
  3826. }
  3827. phy_info = mptsas_refreshing_device_handles
  3828. (ioc, &sas_device);
  3829. }
  3830. if (!phy_info) {
  3831. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3832. "%s %d phy info is NULL\n",
  3833. ioc->name, __func__, __LINE__));
  3834. break;
  3835. }
  3836. if (mptsas_get_rphy(phy_info))
  3837. break;
  3838. mptsas_add_end_device(ioc, phy_info);
  3839. break;
  3840. case MPTSAS_DEL_DEVICE:
  3841. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  3842. hot_plug_info->sas_address);
  3843. mptsas_del_end_device(ioc, phy_info);
  3844. break;
  3845. case MPTSAS_DEL_PHYSDISK:
  3846. mpt_findImVolumes(ioc);
  3847. phy_info = mptsas_find_phyinfo_by_phys_disk_num(
  3848. ioc, hot_plug_info->phys_disk_num,
  3849. hot_plug_info->channel,
  3850. hot_plug_info->id);
  3851. mptsas_del_end_device(ioc, phy_info);
  3852. break;
  3853. case MPTSAS_ADD_PHYSDISK_REPROBE:
  3854. if (mptsas_sas_device_pg0(ioc, &sas_device,
  3855. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  3856. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  3857. (hot_plug_info->channel << 8) + hot_plug_info->id)) {
  3858. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3859. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3860. __func__, hot_plug_info->id, __LINE__));
  3861. break;
  3862. }
  3863. /* If there is no FW B_T mapping for this device then break
  3864. * */
  3865. if (!(sas_device.flags & MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)
  3866. || !(sas_device.flags &
  3867. MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED))
  3868. break;
  3869. phy_info = mptsas_find_phyinfo_by_sas_address(
  3870. ioc, sas_device.sas_address);
  3871. if (!phy_info) {
  3872. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3873. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3874. __func__, hot_plug_info->id, __LINE__));
  3875. break;
  3876. }
  3877. starget = mptsas_get_starget(phy_info);
  3878. if (!starget) {
  3879. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3880. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3881. __func__, hot_plug_info->id, __LINE__));
  3882. break;
  3883. }
  3884. vtarget = starget->hostdata;
  3885. if (!vtarget) {
  3886. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3887. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3888. __func__, hot_plug_info->id, __LINE__));
  3889. break;
  3890. }
  3891. mpt_findImVolumes(ioc);
  3892. starget_printk(KERN_INFO, starget, MYIOC_s_FMT "RAID Hidding: "
  3893. "fw_channel=%d, fw_id=%d, physdsk %d, sas_addr 0x%llx\n",
  3894. ioc->name, hot_plug_info->channel, hot_plug_info->id,
  3895. hot_plug_info->phys_disk_num, (unsigned long long)
  3896. sas_device.sas_address);
  3897. vtarget->id = hot_plug_info->phys_disk_num;
  3898. vtarget->tflags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
  3899. phy_info->attached.phys_disk_num = hot_plug_info->phys_disk_num;
  3900. mptsas_reprobe_target(starget, 1);
  3901. break;
  3902. case MPTSAS_DEL_PHYSDISK_REPROBE:
  3903. if (mptsas_sas_device_pg0(ioc, &sas_device,
  3904. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  3905. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  3906. (hot_plug_info->channel << 8) + hot_plug_info->id)) {
  3907. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3908. "%s: fw_id=%d exit at line=%d\n",
  3909. ioc->name, __func__,
  3910. hot_plug_info->id, __LINE__));
  3911. break;
  3912. }
  3913. /* If there is no FW B_T mapping for this device then break
  3914. * */
  3915. if (!(sas_device.flags & MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)
  3916. || !(sas_device.flags &
  3917. MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED))
  3918. break;
  3919. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  3920. sas_device.sas_address);
  3921. if (!phy_info) {
  3922. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3923. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3924. __func__, hot_plug_info->id, __LINE__));
  3925. break;
  3926. }
  3927. starget = mptsas_get_starget(phy_info);
  3928. if (!starget) {
  3929. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3930. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3931. __func__, hot_plug_info->id, __LINE__));
  3932. break;
  3933. }
  3934. vtarget = starget->hostdata;
  3935. if (!vtarget) {
  3936. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3937. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3938. __func__, hot_plug_info->id, __LINE__));
  3939. break;
  3940. }
  3941. if (!(vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT)) {
  3942. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3943. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3944. __func__, hot_plug_info->id, __LINE__));
  3945. break;
  3946. }
  3947. mpt_findImVolumes(ioc);
  3948. starget_printk(KERN_INFO, starget, MYIOC_s_FMT "RAID Exposing:"
  3949. " fw_channel=%d, fw_id=%d, physdsk %d, sas_addr 0x%llx\n",
  3950. ioc->name, hot_plug_info->channel, hot_plug_info->id,
  3951. hot_plug_info->phys_disk_num, (unsigned long long)
  3952. sas_device.sas_address);
  3953. vtarget->tflags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  3954. vtarget->id = hot_plug_info->id;
  3955. phy_info->attached.phys_disk_num = ~0;
  3956. mptsas_reprobe_target(starget, 0);
  3957. mptsas_add_device_component_by_fw(ioc,
  3958. hot_plug_info->channel, hot_plug_info->id);
  3959. break;
  3960. case MPTSAS_ADD_RAID:
  3961. mpt_findImVolumes(ioc);
  3962. printk(MYIOC_s_INFO_FMT "attaching raid volume, channel %d, "
  3963. "id %d\n", ioc->name, MPTSAS_RAID_CHANNEL,
  3964. hot_plug_info->id);
  3965. scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL,
  3966. hot_plug_info->id, 0);
  3967. break;
  3968. case MPTSAS_DEL_RAID:
  3969. mpt_findImVolumes(ioc);
  3970. printk(MYIOC_s_INFO_FMT "removing raid volume, channel %d, "
  3971. "id %d\n", ioc->name, MPTSAS_RAID_CHANNEL,
  3972. hot_plug_info->id);
  3973. scsi_remove_device(hot_plug_info->sdev);
  3974. scsi_device_put(hot_plug_info->sdev);
  3975. break;
  3976. case MPTSAS_ADD_INACTIVE_VOLUME:
  3977. mpt_findImVolumes(ioc);
  3978. mptsas_adding_inactive_raid_components(ioc,
  3979. hot_plug_info->channel, hot_plug_info->id);
  3980. break;
  3981. default:
  3982. break;
  3983. }
  3984. mptsas_free_fw_event(ioc, fw_event);
  3985. }
  3986. static void
  3987. mptsas_send_sas_event(struct fw_event_work *fw_event)
  3988. {
  3989. MPT_ADAPTER *ioc;
  3990. struct mptsas_hotplug_event hot_plug_info;
  3991. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data;
  3992. u32 device_info;
  3993. u64 sas_address;
  3994. ioc = fw_event->ioc;
  3995. sas_event_data = (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)
  3996. fw_event->event_data;
  3997. device_info = le32_to_cpu(sas_event_data->DeviceInfo);
  3998. if ((device_info &
  3999. (MPI_SAS_DEVICE_INFO_SSP_TARGET |
  4000. MPI_SAS_DEVICE_INFO_STP_TARGET |
  4001. MPI_SAS_DEVICE_INFO_SATA_DEVICE)) == 0) {
  4002. mptsas_free_fw_event(ioc, fw_event);
  4003. return;
  4004. }
  4005. if (sas_event_data->ReasonCode ==
  4006. MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED) {
  4007. mptbase_sas_persist_operation(ioc,
  4008. MPI_SAS_OP_CLEAR_NOT_PRESENT);
  4009. mptsas_free_fw_event(ioc, fw_event);
  4010. return;
  4011. }
  4012. switch (sas_event_data->ReasonCode) {
  4013. case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
  4014. case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
  4015. memset(&hot_plug_info, 0, sizeof(struct mptsas_hotplug_event));
  4016. hot_plug_info.handle = le16_to_cpu(sas_event_data->DevHandle);
  4017. hot_plug_info.channel = sas_event_data->Bus;
  4018. hot_plug_info.id = sas_event_data->TargetID;
  4019. hot_plug_info.phy_id = sas_event_data->PhyNum;
  4020. memcpy(&sas_address, &sas_event_data->SASAddress,
  4021. sizeof(u64));
  4022. hot_plug_info.sas_address = le64_to_cpu(sas_address);
  4023. hot_plug_info.device_info = device_info;
  4024. if (sas_event_data->ReasonCode &
  4025. MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
  4026. hot_plug_info.event_type = MPTSAS_ADD_DEVICE;
  4027. else
  4028. hot_plug_info.event_type = MPTSAS_DEL_DEVICE;
  4029. mptsas_hotplug_work(ioc, fw_event, &hot_plug_info);
  4030. break;
  4031. case MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED:
  4032. mptbase_sas_persist_operation(ioc,
  4033. MPI_SAS_OP_CLEAR_NOT_PRESENT);
  4034. mptsas_free_fw_event(ioc, fw_event);
  4035. break;
  4036. case MPI_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  4037. /* TODO */
  4038. case MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  4039. /* TODO */
  4040. default:
  4041. mptsas_free_fw_event(ioc, fw_event);
  4042. break;
  4043. }
  4044. }
  4045. static void
  4046. mptsas_send_raid_event(struct fw_event_work *fw_event)
  4047. {
  4048. MPT_ADAPTER *ioc;
  4049. EVENT_DATA_RAID *raid_event_data;
  4050. struct mptsas_hotplug_event hot_plug_info;
  4051. int status;
  4052. int state;
  4053. struct scsi_device *sdev = NULL;
  4054. VirtDevice *vdevice = NULL;
  4055. RaidPhysDiskPage0_t phys_disk;
  4056. ioc = fw_event->ioc;
  4057. raid_event_data = (EVENT_DATA_RAID *)fw_event->event_data;
  4058. status = le32_to_cpu(raid_event_data->SettingsStatus);
  4059. state = (status >> 8) & 0xff;
  4060. memset(&hot_plug_info, 0, sizeof(struct mptsas_hotplug_event));
  4061. hot_plug_info.id = raid_event_data->VolumeID;
  4062. hot_plug_info.channel = raid_event_data->VolumeBus;
  4063. hot_plug_info.phys_disk_num = raid_event_data->PhysDiskNum;
  4064. if (raid_event_data->ReasonCode == MPI_EVENT_RAID_RC_VOLUME_DELETED ||
  4065. raid_event_data->ReasonCode == MPI_EVENT_RAID_RC_VOLUME_CREATED ||
  4066. raid_event_data->ReasonCode ==
  4067. MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED) {
  4068. sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
  4069. hot_plug_info.id, 0);
  4070. hot_plug_info.sdev = sdev;
  4071. if (sdev)
  4072. vdevice = sdev->hostdata;
  4073. }
  4074. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Entering %s: "
  4075. "ReasonCode=%02x\n", ioc->name, __func__,
  4076. raid_event_data->ReasonCode));
  4077. switch (raid_event_data->ReasonCode) {
  4078. case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
  4079. hot_plug_info.event_type = MPTSAS_DEL_PHYSDISK_REPROBE;
  4080. break;
  4081. case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
  4082. hot_plug_info.event_type = MPTSAS_ADD_PHYSDISK_REPROBE;
  4083. break;
  4084. case MPI_EVENT_RAID_RC_PHYSDISK_STATUS_CHANGED:
  4085. switch (state) {
  4086. case MPI_PD_STATE_ONLINE:
  4087. case MPI_PD_STATE_NOT_COMPATIBLE:
  4088. mpt_raid_phys_disk_pg0(ioc,
  4089. raid_event_data->PhysDiskNum, &phys_disk);
  4090. hot_plug_info.id = phys_disk.PhysDiskID;
  4091. hot_plug_info.channel = phys_disk.PhysDiskBus;
  4092. hot_plug_info.event_type = MPTSAS_ADD_PHYSDISK;
  4093. break;
  4094. case MPI_PD_STATE_FAILED:
  4095. case MPI_PD_STATE_MISSING:
  4096. case MPI_PD_STATE_OFFLINE_AT_HOST_REQUEST:
  4097. case MPI_PD_STATE_FAILED_AT_HOST_REQUEST:
  4098. case MPI_PD_STATE_OFFLINE_FOR_ANOTHER_REASON:
  4099. hot_plug_info.event_type = MPTSAS_DEL_PHYSDISK;
  4100. break;
  4101. default:
  4102. break;
  4103. }
  4104. break;
  4105. case MPI_EVENT_RAID_RC_VOLUME_DELETED:
  4106. if (!sdev)
  4107. break;
  4108. vdevice->vtarget->deleted = 1; /* block IO */
  4109. hot_plug_info.event_type = MPTSAS_DEL_RAID;
  4110. break;
  4111. case MPI_EVENT_RAID_RC_VOLUME_CREATED:
  4112. if (sdev) {
  4113. scsi_device_put(sdev);
  4114. break;
  4115. }
  4116. hot_plug_info.event_type = MPTSAS_ADD_RAID;
  4117. break;
  4118. case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
  4119. if (!(status & MPI_RAIDVOL0_STATUS_FLAG_ENABLED)) {
  4120. if (!sdev)
  4121. break;
  4122. vdevice->vtarget->deleted = 1; /* block IO */
  4123. hot_plug_info.event_type = MPTSAS_DEL_RAID;
  4124. break;
  4125. }
  4126. switch (state) {
  4127. case MPI_RAIDVOL0_STATUS_STATE_FAILED:
  4128. case MPI_RAIDVOL0_STATUS_STATE_MISSING:
  4129. if (!sdev)
  4130. break;
  4131. vdevice->vtarget->deleted = 1; /* block IO */
  4132. hot_plug_info.event_type = MPTSAS_DEL_RAID;
  4133. break;
  4134. case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL:
  4135. case MPI_RAIDVOL0_STATUS_STATE_DEGRADED:
  4136. if (sdev) {
  4137. scsi_device_put(sdev);
  4138. break;
  4139. }
  4140. hot_plug_info.event_type = MPTSAS_ADD_RAID;
  4141. break;
  4142. default:
  4143. break;
  4144. }
  4145. break;
  4146. default:
  4147. break;
  4148. }
  4149. if (hot_plug_info.event_type != MPTSAS_IGNORE_EVENT)
  4150. mptsas_hotplug_work(ioc, fw_event, &hot_plug_info);
  4151. else
  4152. mptsas_free_fw_event(ioc, fw_event);
  4153. }
  4154. /**
  4155. * mptsas_issue_tm - send mptsas internal tm request
  4156. * @ioc: Pointer to MPT_ADAPTER structure
  4157. * @type: Task Management type
  4158. * @channel: channel number for task management
  4159. * @id: Logical Target ID for reset (if appropriate)
  4160. * @lun: Logical unit for reset (if appropriate)
  4161. * @task_context: Context for the task to be aborted
  4162. * @timeout: timeout for task management control
  4163. *
  4164. * return 0 on success and -1 on failure:
  4165. *
  4166. */
  4167. static int
  4168. mptsas_issue_tm(MPT_ADAPTER *ioc, u8 type, u8 channel, u8 id, u64 lun,
  4169. int task_context, ulong timeout, u8 *issue_reset)
  4170. {
  4171. MPT_FRAME_HDR *mf;
  4172. SCSITaskMgmt_t *pScsiTm;
  4173. int retval;
  4174. unsigned long timeleft;
  4175. *issue_reset = 0;
  4176. mf = mpt_get_msg_frame(mptsasDeviceResetCtx, ioc);
  4177. if (mf == NULL) {
  4178. retval = -1; /* return failure */
  4179. dtmprintk(ioc, printk(MYIOC_s_WARN_FMT "TaskMgmt request: no "
  4180. "msg frames!!\n", ioc->name));
  4181. goto out;
  4182. }
  4183. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TaskMgmt request: mr = %p, "
  4184. "task_type = 0x%02X,\n\t timeout = %ld, fw_channel = %d, "
  4185. "fw_id = %d, lun = %lld,\n\t task_context = 0x%x\n", ioc->name, mf,
  4186. type, timeout, channel, id, (unsigned long long)lun,
  4187. task_context));
  4188. pScsiTm = (SCSITaskMgmt_t *) mf;
  4189. memset(pScsiTm, 0, sizeof(SCSITaskMgmt_t));
  4190. pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
  4191. pScsiTm->TaskType = type;
  4192. pScsiTm->MsgFlags = 0;
  4193. pScsiTm->TargetID = id;
  4194. pScsiTm->Bus = channel;
  4195. pScsiTm->ChainOffset = 0;
  4196. pScsiTm->Reserved = 0;
  4197. pScsiTm->Reserved1 = 0;
  4198. pScsiTm->TaskMsgContext = task_context;
  4199. int_to_scsilun(lun, (struct scsi_lun *)pScsiTm->LUN);
  4200. INITIALIZE_MGMT_STATUS(ioc->taskmgmt_cmds.status)
  4201. CLEAR_MGMT_STATUS(ioc->internal_cmds.status)
  4202. retval = 0;
  4203. mpt_put_msg_frame_hi_pri(mptsasDeviceResetCtx, ioc, mf);
  4204. /* Now wait for the command to complete */
  4205. timeleft = wait_for_completion_timeout(&ioc->taskmgmt_cmds.done,
  4206. timeout*HZ);
  4207. if (!(ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_COMMAND_GOOD)) {
  4208. retval = -1; /* return failure */
  4209. dtmprintk(ioc, printk(MYIOC_s_ERR_FMT
  4210. "TaskMgmt request: TIMED OUT!(mr=%p)\n", ioc->name, mf));
  4211. mpt_free_msg_frame(ioc, mf);
  4212. if (ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_DID_IOCRESET)
  4213. goto out;
  4214. *issue_reset = 1;
  4215. goto out;
  4216. }
  4217. if (!(ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_RF_VALID)) {
  4218. retval = -1; /* return failure */
  4219. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4220. "TaskMgmt request: failed with no reply\n", ioc->name));
  4221. goto out;
  4222. }
  4223. out:
  4224. CLEAR_MGMT_STATUS(ioc->taskmgmt_cmds.status)
  4225. return retval;
  4226. }
  4227. /**
  4228. * mptsas_broadcast_primitive_work - Handle broadcast primitives
  4229. * @work: work queue payload containing info describing the event
  4230. *
  4231. * this will be handled in workqueue context.
  4232. */
  4233. static void
  4234. mptsas_broadcast_primitive_work(struct fw_event_work *fw_event)
  4235. {
  4236. MPT_ADAPTER *ioc = fw_event->ioc;
  4237. MPT_FRAME_HDR *mf;
  4238. VirtDevice *vdevice;
  4239. int ii;
  4240. struct scsi_cmnd *sc;
  4241. SCSITaskMgmtReply_t *pScsiTmReply;
  4242. u8 issue_reset;
  4243. int task_context;
  4244. u8 channel, id;
  4245. int lun;
  4246. u32 termination_count;
  4247. u32 query_count;
  4248. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4249. "%s - enter\n", ioc->name, __func__));
  4250. mutex_lock(&ioc->taskmgmt_cmds.mutex);
  4251. if (mpt_set_taskmgmt_in_progress_flag(ioc) != 0) {
  4252. mutex_unlock(&ioc->taskmgmt_cmds.mutex);
  4253. mptsas_requeue_fw_event(ioc, fw_event, 1000);
  4254. return;
  4255. }
  4256. issue_reset = 0;
  4257. termination_count = 0;
  4258. query_count = 0;
  4259. mpt_findImVolumes(ioc);
  4260. pScsiTmReply = (SCSITaskMgmtReply_t *) ioc->taskmgmt_cmds.reply;
  4261. for (ii = 0; ii < ioc->req_depth; ii++) {
  4262. if (ioc->fw_events_off)
  4263. goto out;
  4264. sc = mptscsih_get_scsi_lookup(ioc, ii);
  4265. if (!sc)
  4266. continue;
  4267. mf = MPT_INDEX_2_MFPTR(ioc, ii);
  4268. if (!mf)
  4269. continue;
  4270. task_context = mf->u.frame.hwhdr.msgctxu.MsgContext;
  4271. vdevice = sc->device->hostdata;
  4272. if (!vdevice || !vdevice->vtarget)
  4273. continue;
  4274. if (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT)
  4275. continue; /* skip hidden raid components */
  4276. if (vdevice->vtarget->raidVolume)
  4277. continue; /* skip hidden raid components */
  4278. channel = vdevice->vtarget->channel;
  4279. id = vdevice->vtarget->id;
  4280. lun = vdevice->lun;
  4281. if (mptsas_issue_tm(ioc, MPI_SCSITASKMGMT_TASKTYPE_QUERY_TASK,
  4282. channel, id, (u64)lun, task_context, 30, &issue_reset))
  4283. goto out;
  4284. query_count++;
  4285. termination_count +=
  4286. le32_to_cpu(pScsiTmReply->TerminationCount);
  4287. if ((pScsiTmReply->IOCStatus == MPI_IOCSTATUS_SUCCESS) &&
  4288. (pScsiTmReply->ResponseCode ==
  4289. MPI_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
  4290. pScsiTmReply->ResponseCode ==
  4291. MPI_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
  4292. continue;
  4293. if (mptsas_issue_tm(ioc,
  4294. MPI_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET,
  4295. channel, id, (u64)lun, 0, 30, &issue_reset))
  4296. goto out;
  4297. termination_count +=
  4298. le32_to_cpu(pScsiTmReply->TerminationCount);
  4299. }
  4300. out:
  4301. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4302. "%s - exit, query_count = %d termination_count = %d\n",
  4303. ioc->name, __func__, query_count, termination_count));
  4304. ioc->broadcast_aen_busy = 0;
  4305. mpt_clear_taskmgmt_in_progress_flag(ioc);
  4306. mutex_unlock(&ioc->taskmgmt_cmds.mutex);
  4307. if (issue_reset) {
  4308. printk(MYIOC_s_WARN_FMT
  4309. "Issuing Reset from %s!! doorbell=0x%08x\n",
  4310. ioc->name, __func__, mpt_GetIocState(ioc, 0));
  4311. mpt_Soft_Hard_ResetHandler(ioc, CAN_SLEEP);
  4312. }
  4313. mptsas_free_fw_event(ioc, fw_event);
  4314. }
  4315. /*
  4316. * mptsas_send_ir2_event - handle exposing hidden disk when
  4317. * an inactive raid volume is added
  4318. *
  4319. * @ioc: Pointer to MPT_ADAPTER structure
  4320. * @ir2_data
  4321. *
  4322. */
  4323. static void
  4324. mptsas_send_ir2_event(struct fw_event_work *fw_event)
  4325. {
  4326. MPT_ADAPTER *ioc;
  4327. struct mptsas_hotplug_event hot_plug_info;
  4328. MPI_EVENT_DATA_IR2 *ir2_data;
  4329. u8 reasonCode;
  4330. RaidPhysDiskPage0_t phys_disk;
  4331. ioc = fw_event->ioc;
  4332. ir2_data = (MPI_EVENT_DATA_IR2 *)fw_event->event_data;
  4333. reasonCode = ir2_data->ReasonCode;
  4334. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Entering %s: "
  4335. "ReasonCode=%02x\n", ioc->name, __func__, reasonCode));
  4336. memset(&hot_plug_info, 0, sizeof(struct mptsas_hotplug_event));
  4337. hot_plug_info.id = ir2_data->TargetID;
  4338. hot_plug_info.channel = ir2_data->Bus;
  4339. switch (reasonCode) {
  4340. case MPI_EVENT_IR2_RC_FOREIGN_CFG_DETECTED:
  4341. hot_plug_info.event_type = MPTSAS_ADD_INACTIVE_VOLUME;
  4342. break;
  4343. case MPI_EVENT_IR2_RC_DUAL_PORT_REMOVED:
  4344. hot_plug_info.phys_disk_num = ir2_data->PhysDiskNum;
  4345. hot_plug_info.event_type = MPTSAS_DEL_PHYSDISK;
  4346. break;
  4347. case MPI_EVENT_IR2_RC_DUAL_PORT_ADDED:
  4348. hot_plug_info.phys_disk_num = ir2_data->PhysDiskNum;
  4349. mpt_raid_phys_disk_pg0(ioc,
  4350. ir2_data->PhysDiskNum, &phys_disk);
  4351. hot_plug_info.id = phys_disk.PhysDiskID;
  4352. hot_plug_info.event_type = MPTSAS_ADD_PHYSDISK;
  4353. break;
  4354. default:
  4355. mptsas_free_fw_event(ioc, fw_event);
  4356. return;
  4357. }
  4358. mptsas_hotplug_work(ioc, fw_event, &hot_plug_info);
  4359. }
  4360. static int
  4361. mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
  4362. {
  4363. u32 event = le32_to_cpu(reply->Event);
  4364. int event_data_sz;
  4365. struct fw_event_work *fw_event;
  4366. unsigned long delay;
  4367. if (ioc->bus_type != SAS)
  4368. return 0;
  4369. /* events turned off due to host reset or driver unloading */
  4370. if (ioc->fw_events_off)
  4371. return 0;
  4372. delay = msecs_to_jiffies(1);
  4373. switch (event) {
  4374. case MPI_EVENT_SAS_BROADCAST_PRIMITIVE:
  4375. {
  4376. EVENT_DATA_SAS_BROADCAST_PRIMITIVE *broadcast_event_data =
  4377. (EVENT_DATA_SAS_BROADCAST_PRIMITIVE *)reply->Data;
  4378. if (broadcast_event_data->Primitive !=
  4379. MPI_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
  4380. return 0;
  4381. if (ioc->broadcast_aen_busy)
  4382. return 0;
  4383. ioc->broadcast_aen_busy = 1;
  4384. break;
  4385. }
  4386. case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
  4387. {
  4388. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data =
  4389. (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data;
  4390. u16 ioc_stat;
  4391. ioc_stat = le16_to_cpu(reply->IOCStatus);
  4392. if (sas_event_data->ReasonCode ==
  4393. MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING) {
  4394. mptsas_target_reset_queue(ioc, sas_event_data);
  4395. return 0;
  4396. }
  4397. if (sas_event_data->ReasonCode ==
  4398. MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
  4399. ioc->device_missing_delay &&
  4400. (ioc_stat & MPI_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)) {
  4401. VirtTarget *vtarget = NULL;
  4402. u8 id, channel;
  4403. id = sas_event_data->TargetID;
  4404. channel = sas_event_data->Bus;
  4405. vtarget = mptsas_find_vtarget(ioc, channel, id);
  4406. if (vtarget) {
  4407. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4408. "LogInfo (0x%x) available for "
  4409. "INTERNAL_DEVICE_RESET"
  4410. "fw_id %d fw_channel %d\n", ioc->name,
  4411. le32_to_cpu(reply->IOCLogInfo),
  4412. id, channel));
  4413. if (vtarget->raidVolume) {
  4414. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4415. "Skipping Raid Volume for inDMD\n",
  4416. ioc->name));
  4417. } else {
  4418. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4419. "Setting device flag inDMD\n",
  4420. ioc->name));
  4421. vtarget->inDMD = 1;
  4422. }
  4423. }
  4424. }
  4425. break;
  4426. }
  4427. case MPI_EVENT_SAS_EXPANDER_STATUS_CHANGE:
  4428. {
  4429. MpiEventDataSasExpanderStatusChange_t *expander_data =
  4430. (MpiEventDataSasExpanderStatusChange_t *)reply->Data;
  4431. if (ioc->old_sas_discovery_protocal)
  4432. return 0;
  4433. if (expander_data->ReasonCode ==
  4434. MPI_EVENT_SAS_EXP_RC_NOT_RESPONDING &&
  4435. ioc->device_missing_delay)
  4436. delay = HZ * ioc->device_missing_delay;
  4437. break;
  4438. }
  4439. case MPI_EVENT_SAS_DISCOVERY:
  4440. {
  4441. u32 discovery_status;
  4442. EventDataSasDiscovery_t *discovery_data =
  4443. (EventDataSasDiscovery_t *)reply->Data;
  4444. discovery_status = le32_to_cpu(discovery_data->DiscoveryStatus);
  4445. ioc->sas_discovery_quiesce_io = discovery_status ? 1 : 0;
  4446. if (ioc->old_sas_discovery_protocal && !discovery_status)
  4447. mptsas_queue_rescan(ioc);
  4448. return 0;
  4449. }
  4450. case MPI_EVENT_INTEGRATED_RAID:
  4451. case MPI_EVENT_PERSISTENT_TABLE_FULL:
  4452. case MPI_EVENT_IR2:
  4453. case MPI_EVENT_SAS_PHY_LINK_STATUS:
  4454. case MPI_EVENT_QUEUE_FULL:
  4455. break;
  4456. default:
  4457. return 0;
  4458. }
  4459. event_data_sz = ((reply->MsgLength * 4) -
  4460. offsetof(EventNotificationReply_t, Data));
  4461. fw_event = kzalloc(sizeof(*fw_event) + event_data_sz, GFP_ATOMIC);
  4462. if (!fw_event) {
  4463. printk(MYIOC_s_WARN_FMT "%s: failed at (line=%d)\n", ioc->name,
  4464. __func__, __LINE__);
  4465. return 0;
  4466. }
  4467. memcpy(fw_event->event_data, reply->Data, event_data_sz);
  4468. fw_event->event = event;
  4469. fw_event->ioc = ioc;
  4470. mptsas_add_fw_event(ioc, fw_event, delay);
  4471. return 0;
  4472. }
  4473. /* Delete a volume when no longer listed in ioc pg2
  4474. */
  4475. static void mptsas_volume_delete(MPT_ADAPTER *ioc, u8 id)
  4476. {
  4477. struct scsi_device *sdev;
  4478. int i;
  4479. sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL, id, 0);
  4480. if (!sdev)
  4481. return;
  4482. if (!ioc->raid_data.pIocPg2)
  4483. goto out;
  4484. if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
  4485. goto out;
  4486. for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++)
  4487. if (ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID == id)
  4488. goto release_sdev;
  4489. out:
  4490. printk(MYIOC_s_INFO_FMT "removing raid volume, channel %d, "
  4491. "id %d\n", ioc->name, MPTSAS_RAID_CHANNEL, id);
  4492. scsi_remove_device(sdev);
  4493. release_sdev:
  4494. scsi_device_put(sdev);
  4495. }
  4496. static int
  4497. mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  4498. {
  4499. struct Scsi_Host *sh;
  4500. MPT_SCSI_HOST *hd;
  4501. MPT_ADAPTER *ioc;
  4502. unsigned long flags;
  4503. int ii;
  4504. int numSGE = 0;
  4505. int scale;
  4506. int ioc_cap;
  4507. int error=0;
  4508. int r;
  4509. r = mpt_attach(pdev,id);
  4510. if (r)
  4511. return r;
  4512. ioc = pci_get_drvdata(pdev);
  4513. mptsas_fw_event_off(ioc);
  4514. ioc->DoneCtx = mptsasDoneCtx;
  4515. ioc->TaskCtx = mptsasTaskCtx;
  4516. ioc->InternalCtx = mptsasInternalCtx;
  4517. ioc->schedule_target_reset = &mptsas_schedule_target_reset;
  4518. ioc->schedule_dead_ioc_flush_running_cmds =
  4519. &mptscsih_flush_running_cmds;
  4520. /* Added sanity check on readiness of the MPT adapter.
  4521. */
  4522. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  4523. printk(MYIOC_s_WARN_FMT
  4524. "Skipping because it's not operational!\n",
  4525. ioc->name);
  4526. error = -ENODEV;
  4527. goto out_mptsas_probe;
  4528. }
  4529. if (!ioc->active) {
  4530. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  4531. ioc->name);
  4532. error = -ENODEV;
  4533. goto out_mptsas_probe;
  4534. }
  4535. /* Sanity check - ensure at least 1 port is INITIATOR capable
  4536. */
  4537. ioc_cap = 0;
  4538. for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
  4539. if (ioc->pfacts[ii].ProtocolFlags &
  4540. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  4541. ioc_cap++;
  4542. }
  4543. if (!ioc_cap) {
  4544. printk(MYIOC_s_WARN_FMT
  4545. "Skipping ioc=%p because SCSI Initiator mode "
  4546. "is NOT enabled!\n", ioc->name, ioc);
  4547. return 0;
  4548. }
  4549. sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
  4550. if (!sh) {
  4551. printk(MYIOC_s_WARN_FMT
  4552. "Unable to register controller with SCSI subsystem\n",
  4553. ioc->name);
  4554. error = -1;
  4555. goto out_mptsas_probe;
  4556. }
  4557. spin_lock_irqsave(&ioc->FreeQlock, flags);
  4558. /* Attach the SCSI Host to the IOC structure
  4559. */
  4560. ioc->sh = sh;
  4561. sh->io_port = 0;
  4562. sh->n_io_port = 0;
  4563. sh->irq = 0;
  4564. /* set 16 byte cdb's */
  4565. sh->max_cmd_len = 16;
  4566. sh->can_queue = min_t(int, ioc->req_depth - 10, sh->can_queue);
  4567. sh->max_id = -1;
  4568. sh->max_lun = max_lun;
  4569. sh->transportt = mptsas_transport_template;
  4570. /* Required entry.
  4571. */
  4572. sh->unique_id = ioc->id;
  4573. INIT_LIST_HEAD(&ioc->sas_topology);
  4574. mutex_init(&ioc->sas_topology_mutex);
  4575. mutex_init(&ioc->sas_discovery_mutex);
  4576. mutex_init(&ioc->sas_mgmt.mutex);
  4577. init_completion(&ioc->sas_mgmt.done);
  4578. /* Verify that we won't exceed the maximum
  4579. * number of chain buffers
  4580. * We can optimize: ZZ = req_sz/sizeof(SGE)
  4581. * For 32bit SGE's:
  4582. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  4583. * + (req_sz - 64)/sizeof(SGE)
  4584. * A slightly different algorithm is required for
  4585. * 64bit SGEs.
  4586. */
  4587. scale = ioc->req_sz/ioc->SGE_size;
  4588. if (ioc->sg_addr_size == sizeof(u64)) {
  4589. numSGE = (scale - 1) *
  4590. (ioc->facts.MaxChainDepth-1) + scale +
  4591. (ioc->req_sz - 60) / ioc->SGE_size;
  4592. } else {
  4593. numSGE = 1 + (scale - 1) *
  4594. (ioc->facts.MaxChainDepth-1) + scale +
  4595. (ioc->req_sz - 64) / ioc->SGE_size;
  4596. }
  4597. if (numSGE < sh->sg_tablesize) {
  4598. /* Reset this value */
  4599. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4600. "Resetting sg_tablesize to %d from %d\n",
  4601. ioc->name, numSGE, sh->sg_tablesize));
  4602. sh->sg_tablesize = numSGE;
  4603. }
  4604. if (mpt_loadtime_max_sectors) {
  4605. if (mpt_loadtime_max_sectors < 64 ||
  4606. mpt_loadtime_max_sectors > 8192) {
  4607. printk(MYIOC_s_INFO_FMT "Invalid value passed for"
  4608. "mpt_loadtime_max_sectors %d."
  4609. "Range from 64 to 8192\n", ioc->name,
  4610. mpt_loadtime_max_sectors);
  4611. }
  4612. mpt_loadtime_max_sectors &= 0xFFFFFFFE;
  4613. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4614. "Resetting max sector to %d from %d\n",
  4615. ioc->name, mpt_loadtime_max_sectors, sh->max_sectors));
  4616. sh->max_sectors = mpt_loadtime_max_sectors;
  4617. }
  4618. hd = shost_priv(sh);
  4619. hd->ioc = ioc;
  4620. /* SCSI needs scsi_cmnd lookup table!
  4621. * (with size equal to req_depth*PtrSz!)
  4622. */
  4623. ioc->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  4624. if (!ioc->ScsiLookup) {
  4625. error = -ENOMEM;
  4626. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  4627. goto out_mptsas_probe;
  4628. }
  4629. spin_lock_init(&ioc->scsi_lookup_lock);
  4630. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "ScsiLookup @ %p\n",
  4631. ioc->name, ioc->ScsiLookup));
  4632. ioc->sas_data.ptClear = mpt_pt_clear;
  4633. hd->last_queue_full = 0;
  4634. INIT_LIST_HEAD(&hd->target_reset_list);
  4635. INIT_LIST_HEAD(&ioc->sas_device_info_list);
  4636. mutex_init(&ioc->sas_device_info_mutex);
  4637. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  4638. if (ioc->sas_data.ptClear==1) {
  4639. mptbase_sas_persist_operation(
  4640. ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
  4641. }
  4642. error = scsi_add_host(sh, &ioc->pcidev->dev);
  4643. if (error) {
  4644. dprintk(ioc, printk(MYIOC_s_ERR_FMT
  4645. "scsi_add_host failed\n", ioc->name));
  4646. goto out_mptsas_probe;
  4647. }
  4648. /* older firmware doesn't support expander events */
  4649. if ((ioc->facts.HeaderVersion >> 8) < 0xE)
  4650. ioc->old_sas_discovery_protocal = 1;
  4651. mptsas_scan_sas_topology(ioc);
  4652. mptsas_fw_event_on(ioc);
  4653. return 0;
  4654. out_mptsas_probe:
  4655. mptscsih_remove(pdev);
  4656. return error;
  4657. }
  4658. static void
  4659. mptsas_shutdown(struct pci_dev *pdev)
  4660. {
  4661. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  4662. mptsas_fw_event_off(ioc);
  4663. mptsas_cleanup_fw_event_q(ioc);
  4664. }
  4665. static void mptsas_remove(struct pci_dev *pdev)
  4666. {
  4667. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  4668. struct mptsas_portinfo *p, *n;
  4669. int i;
  4670. if (!ioc->sh) {
  4671. printk(MYIOC_s_INFO_FMT "IOC is in Target mode\n", ioc->name);
  4672. mpt_detach(pdev);
  4673. return;
  4674. }
  4675. mptsas_shutdown(pdev);
  4676. mptsas_del_device_components(ioc);
  4677. ioc->sas_discovery_ignore_events = 1;
  4678. sas_remove_host(ioc->sh);
  4679. mutex_lock(&ioc->sas_topology_mutex);
  4680. list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
  4681. list_del(&p->list);
  4682. for (i = 0 ; i < p->num_phys ; i++)
  4683. mptsas_port_delete(ioc, p->phy_info[i].port_details);
  4684. kfree(p->phy_info);
  4685. kfree(p);
  4686. }
  4687. mutex_unlock(&ioc->sas_topology_mutex);
  4688. ioc->hba_port_info = NULL;
  4689. mptscsih_remove(pdev);
  4690. }
  4691. static struct pci_device_id mptsas_pci_table[] = {
  4692. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064,
  4693. PCI_ANY_ID, PCI_ANY_ID },
  4694. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068,
  4695. PCI_ANY_ID, PCI_ANY_ID },
  4696. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064E,
  4697. PCI_ANY_ID, PCI_ANY_ID },
  4698. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068E,
  4699. PCI_ANY_ID, PCI_ANY_ID },
  4700. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1078,
  4701. PCI_ANY_ID, PCI_ANY_ID },
  4702. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068_820XELP,
  4703. PCI_ANY_ID, PCI_ANY_ID },
  4704. {0} /* Terminating entry */
  4705. };
  4706. MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
  4707. static struct pci_driver mptsas_driver = {
  4708. .name = "mptsas",
  4709. .id_table = mptsas_pci_table,
  4710. .probe = mptsas_probe,
  4711. .remove = mptsas_remove,
  4712. .shutdown = mptsas_shutdown,
  4713. #ifdef CONFIG_PM
  4714. .suspend = mptscsih_suspend,
  4715. .resume = mptscsih_resume,
  4716. #endif
  4717. };
  4718. static int __init
  4719. mptsas_init(void)
  4720. {
  4721. int error;
  4722. show_mptmod_ver(my_NAME, my_VERSION);
  4723. mptsas_transport_template =
  4724. sas_attach_transport(&mptsas_transport_functions);
  4725. if (!mptsas_transport_template)
  4726. return -ENODEV;
  4727. mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER,
  4728. "mptscsih_io_done");
  4729. mptsasTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSAS_DRIVER,
  4730. "mptscsih_taskmgmt_complete");
  4731. mptsasInternalCtx =
  4732. mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER,
  4733. "mptscsih_scandv_complete");
  4734. mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER,
  4735. "mptsas_mgmt_done");
  4736. mptsasDeviceResetCtx =
  4737. mpt_register(mptsas_taskmgmt_complete, MPTSAS_DRIVER,
  4738. "mptsas_taskmgmt_complete");
  4739. mpt_event_register(mptsasDoneCtx, mptsas_event_process);
  4740. mpt_reset_register(mptsasDoneCtx, mptsas_ioc_reset);
  4741. error = pci_register_driver(&mptsas_driver);
  4742. if (error)
  4743. sas_release_transport(mptsas_transport_template);
  4744. return error;
  4745. }
  4746. static void __exit
  4747. mptsas_exit(void)
  4748. {
  4749. pci_unregister_driver(&mptsas_driver);
  4750. sas_release_transport(mptsas_transport_template);
  4751. mpt_reset_deregister(mptsasDoneCtx);
  4752. mpt_event_deregister(mptsasDoneCtx);
  4753. mpt_deregister(mptsasMgmtCtx);
  4754. mpt_deregister(mptsasInternalCtx);
  4755. mpt_deregister(mptsasTaskCtx);
  4756. mpt_deregister(mptsasDoneCtx);
  4757. mpt_deregister(mptsasDeviceResetCtx);
  4758. }
  4759. module_init(mptsas_init);
  4760. module_exit(mptsas_exit);