mptsas.c 147 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444
  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_primative_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. head = &hd->target_reset_list;
  1034. if (list_empty(head))
  1035. return;
  1036. target_reset_list = list_entry(head->next,
  1037. struct mptsas_target_reset_event, list);
  1038. id = target_reset_list->sas_event_data.TargetID;
  1039. channel = target_reset_list->sas_event_data.Bus;
  1040. target_reset_list->time_count = jiffies;
  1041. if (mptsas_target_reset(ioc, channel, id))
  1042. target_reset_list->target_reset_issued = 1;
  1043. return;
  1044. }
  1045. /**
  1046. * mptsas_taskmgmt_complete - complete SAS task management function
  1047. * @ioc: Pointer to MPT_ADAPTER structure
  1048. *
  1049. * Completion for TARGET_RESET after NOT_RESPONDING_EVENT, enable work
  1050. * queue to finish off removing device from upper layers. then send next
  1051. * TARGET_RESET in the queue.
  1052. **/
  1053. static int
  1054. mptsas_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
  1055. {
  1056. MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
  1057. struct list_head *head = &hd->target_reset_list;
  1058. u8 id, channel;
  1059. struct mptsas_target_reset_event *target_reset_list;
  1060. SCSITaskMgmtReply_t *pScsiTmReply;
  1061. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TaskMgmt completed: "
  1062. "(mf = %p, mr = %p)\n", ioc->name, mf, mr));
  1063. pScsiTmReply = (SCSITaskMgmtReply_t *)mr;
  1064. if (!pScsiTmReply)
  1065. return 0;
  1066. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1067. "\tTaskMgmt completed: fw_channel = %d, fw_id = %d,\n"
  1068. "\ttask_type = 0x%02X, iocstatus = 0x%04X "
  1069. "loginfo = 0x%08X,\n\tresponse_code = 0x%02X, "
  1070. "term_cmnds = %d\n", ioc->name,
  1071. pScsiTmReply->Bus, pScsiTmReply->TargetID,
  1072. pScsiTmReply->TaskType,
  1073. le16_to_cpu(pScsiTmReply->IOCStatus),
  1074. le32_to_cpu(pScsiTmReply->IOCLogInfo),
  1075. pScsiTmReply->ResponseCode,
  1076. le32_to_cpu(pScsiTmReply->TerminationCount)));
  1077. if (pScsiTmReply->ResponseCode)
  1078. mptscsih_taskmgmt_response_code(ioc,
  1079. pScsiTmReply->ResponseCode);
  1080. if (pScsiTmReply->TaskType ==
  1081. MPI_SCSITASKMGMT_TASKTYPE_QUERY_TASK || pScsiTmReply->TaskType ==
  1082. MPI_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET) {
  1083. ioc->taskmgmt_cmds.status |= MPT_MGMT_STATUS_COMMAND_GOOD;
  1084. ioc->taskmgmt_cmds.status |= MPT_MGMT_STATUS_RF_VALID;
  1085. memcpy(ioc->taskmgmt_cmds.reply, mr,
  1086. min(MPT_DEFAULT_FRAME_SIZE, 4 * mr->u.reply.MsgLength));
  1087. if (ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_PENDING) {
  1088. ioc->taskmgmt_cmds.status &= ~MPT_MGMT_STATUS_PENDING;
  1089. complete(&ioc->taskmgmt_cmds.done);
  1090. return 1;
  1091. }
  1092. return 0;
  1093. }
  1094. mpt_clear_taskmgmt_in_progress_flag(ioc);
  1095. if (list_empty(head))
  1096. return 1;
  1097. target_reset_list = list_entry(head->next,
  1098. struct mptsas_target_reset_event, list);
  1099. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1100. "TaskMgmt: completed (%d seconds)\n",
  1101. ioc->name, jiffies_to_msecs(jiffies -
  1102. target_reset_list->time_count)/1000));
  1103. id = pScsiTmReply->TargetID;
  1104. channel = pScsiTmReply->Bus;
  1105. target_reset_list->time_count = jiffies;
  1106. /*
  1107. * retry target reset
  1108. */
  1109. if (!target_reset_list->target_reset_issued) {
  1110. if (mptsas_target_reset(ioc, channel, id))
  1111. target_reset_list->target_reset_issued = 1;
  1112. return 1;
  1113. }
  1114. /*
  1115. * enable work queue to remove device from upper layers
  1116. */
  1117. list_del(&target_reset_list->list);
  1118. if (!ioc->fw_events_off)
  1119. mptsas_queue_device_delete(ioc,
  1120. &target_reset_list->sas_event_data);
  1121. ioc->schedule_target_reset(ioc);
  1122. return 1;
  1123. }
  1124. /**
  1125. * mptscsih_ioc_reset
  1126. *
  1127. * @ioc
  1128. * @reset_phase
  1129. *
  1130. **/
  1131. static int
  1132. mptsas_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
  1133. {
  1134. MPT_SCSI_HOST *hd;
  1135. int rc;
  1136. rc = mptscsih_ioc_reset(ioc, reset_phase);
  1137. if ((ioc->bus_type != SAS) || (!rc))
  1138. return rc;
  1139. hd = shost_priv(ioc->sh);
  1140. if (!hd->ioc)
  1141. goto out;
  1142. switch (reset_phase) {
  1143. case MPT_IOC_SETUP_RESET:
  1144. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1145. "%s: MPT_IOC_SETUP_RESET\n", ioc->name, __func__));
  1146. mptsas_fw_event_off(ioc);
  1147. break;
  1148. case MPT_IOC_PRE_RESET:
  1149. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1150. "%s: MPT_IOC_PRE_RESET\n", ioc->name, __func__));
  1151. break;
  1152. case MPT_IOC_POST_RESET:
  1153. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1154. "%s: MPT_IOC_POST_RESET\n", ioc->name, __func__));
  1155. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_PENDING) {
  1156. ioc->sas_mgmt.status |= MPT_MGMT_STATUS_DID_IOCRESET;
  1157. complete(&ioc->sas_mgmt.done);
  1158. }
  1159. mptsas_cleanup_fw_event_q(ioc);
  1160. mptsas_queue_rescan(ioc);
  1161. break;
  1162. default:
  1163. break;
  1164. }
  1165. out:
  1166. return rc;
  1167. }
  1168. /**
  1169. * enum device_state -
  1170. * @DEVICE_RETRY: need to retry the TUR
  1171. * @DEVICE_ERROR: TUR return error, don't add device
  1172. * @DEVICE_READY: device can be added
  1173. *
  1174. */
  1175. enum device_state{
  1176. DEVICE_RETRY,
  1177. DEVICE_ERROR,
  1178. DEVICE_READY,
  1179. };
  1180. static int
  1181. mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc, struct mptsas_enclosure *enclosure,
  1182. u32 form, u32 form_specific)
  1183. {
  1184. ConfigExtendedPageHeader_t hdr;
  1185. CONFIGPARMS cfg;
  1186. SasEnclosurePage0_t *buffer;
  1187. dma_addr_t dma_handle;
  1188. int error;
  1189. __le64 le_identifier;
  1190. memset(&hdr, 0, sizeof(hdr));
  1191. hdr.PageVersion = MPI_SASENCLOSURE0_PAGEVERSION;
  1192. hdr.PageNumber = 0;
  1193. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1194. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_ENCLOSURE;
  1195. cfg.cfghdr.ehdr = &hdr;
  1196. cfg.physAddr = -1;
  1197. cfg.pageAddr = form + form_specific;
  1198. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1199. cfg.dir = 0; /* read */
  1200. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  1201. error = mpt_config(ioc, &cfg);
  1202. if (error)
  1203. goto out;
  1204. if (!hdr.ExtPageLength) {
  1205. error = -ENXIO;
  1206. goto out;
  1207. }
  1208. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1209. &dma_handle);
  1210. if (!buffer) {
  1211. error = -ENOMEM;
  1212. goto out;
  1213. }
  1214. cfg.physAddr = dma_handle;
  1215. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1216. error = mpt_config(ioc, &cfg);
  1217. if (error)
  1218. goto out_free_consistent;
  1219. /* save config data */
  1220. memcpy(&le_identifier, &buffer->EnclosureLogicalID, sizeof(__le64));
  1221. enclosure->enclosure_logical_id = le64_to_cpu(le_identifier);
  1222. enclosure->enclosure_handle = le16_to_cpu(buffer->EnclosureHandle);
  1223. enclosure->flags = le16_to_cpu(buffer->Flags);
  1224. enclosure->num_slot = le16_to_cpu(buffer->NumSlots);
  1225. enclosure->start_slot = le16_to_cpu(buffer->StartSlot);
  1226. enclosure->start_id = buffer->StartTargetID;
  1227. enclosure->start_channel = buffer->StartBus;
  1228. enclosure->sep_id = buffer->SEPTargetID;
  1229. enclosure->sep_channel = buffer->SEPBus;
  1230. out_free_consistent:
  1231. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1232. buffer, dma_handle);
  1233. out:
  1234. return error;
  1235. }
  1236. /**
  1237. * mptsas_add_end_device - report a new end device to sas transport layer
  1238. * @ioc: Pointer to MPT_ADAPTER structure
  1239. * @phy_info: describes attached device
  1240. *
  1241. * return (0) success (1) failure
  1242. *
  1243. **/
  1244. static int
  1245. mptsas_add_end_device(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info)
  1246. {
  1247. struct sas_rphy *rphy;
  1248. struct sas_port *port;
  1249. struct sas_identify identify;
  1250. char *ds = NULL;
  1251. u8 fw_id;
  1252. if (!phy_info) {
  1253. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1254. "%s: exit at line=%d\n", ioc->name,
  1255. __func__, __LINE__));
  1256. return 1;
  1257. }
  1258. fw_id = phy_info->attached.id;
  1259. if (mptsas_get_rphy(phy_info)) {
  1260. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1261. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1262. __func__, fw_id, __LINE__));
  1263. return 2;
  1264. }
  1265. port = mptsas_get_port(phy_info);
  1266. if (!port) {
  1267. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1268. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1269. __func__, fw_id, __LINE__));
  1270. return 3;
  1271. }
  1272. if (phy_info->attached.device_info &
  1273. MPI_SAS_DEVICE_INFO_SSP_TARGET)
  1274. ds = "ssp";
  1275. if (phy_info->attached.device_info &
  1276. MPI_SAS_DEVICE_INFO_STP_TARGET)
  1277. ds = "stp";
  1278. if (phy_info->attached.device_info &
  1279. MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  1280. ds = "sata";
  1281. printk(MYIOC_s_INFO_FMT "attaching %s device: fw_channel %d, fw_id %d,"
  1282. " phy %d, sas_addr 0x%llx\n", ioc->name, ds,
  1283. phy_info->attached.channel, phy_info->attached.id,
  1284. phy_info->attached.phy_id, (unsigned long long)
  1285. phy_info->attached.sas_address);
  1286. mptsas_parse_device_info(&identify, &phy_info->attached);
  1287. rphy = sas_end_device_alloc(port);
  1288. if (!rphy) {
  1289. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1290. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1291. __func__, fw_id, __LINE__));
  1292. return 5; /* non-fatal: an rphy can be added later */
  1293. }
  1294. rphy->identify = identify;
  1295. if (sas_rphy_add(rphy)) {
  1296. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1297. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1298. __func__, fw_id, __LINE__));
  1299. sas_rphy_free(rphy);
  1300. return 6;
  1301. }
  1302. mptsas_set_rphy(ioc, phy_info, rphy);
  1303. return 0;
  1304. }
  1305. /**
  1306. * mptsas_del_end_device - report a deleted end device to sas transport layer
  1307. * @ioc: Pointer to MPT_ADAPTER structure
  1308. * @phy_info: describes attached device
  1309. *
  1310. **/
  1311. static void
  1312. mptsas_del_end_device(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info)
  1313. {
  1314. struct sas_rphy *rphy;
  1315. struct sas_port *port;
  1316. struct mptsas_portinfo *port_info;
  1317. struct mptsas_phyinfo *phy_info_parent;
  1318. int i;
  1319. char *ds = NULL;
  1320. u8 fw_id;
  1321. u64 sas_address;
  1322. if (!phy_info)
  1323. return;
  1324. fw_id = phy_info->attached.id;
  1325. sas_address = phy_info->attached.sas_address;
  1326. if (!phy_info->port_details) {
  1327. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1328. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1329. __func__, fw_id, __LINE__));
  1330. return;
  1331. }
  1332. rphy = mptsas_get_rphy(phy_info);
  1333. if (!rphy) {
  1334. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1335. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1336. __func__, fw_id, __LINE__));
  1337. return;
  1338. }
  1339. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_INITIATOR
  1340. || phy_info->attached.device_info
  1341. & MPI_SAS_DEVICE_INFO_SMP_INITIATOR
  1342. || phy_info->attached.device_info
  1343. & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
  1344. ds = "initiator";
  1345. if (phy_info->attached.device_info &
  1346. MPI_SAS_DEVICE_INFO_SSP_TARGET)
  1347. ds = "ssp";
  1348. if (phy_info->attached.device_info &
  1349. MPI_SAS_DEVICE_INFO_STP_TARGET)
  1350. ds = "stp";
  1351. if (phy_info->attached.device_info &
  1352. MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  1353. ds = "sata";
  1354. dev_printk(KERN_DEBUG, &rphy->dev, MYIOC_s_FMT
  1355. "removing %s device: fw_channel %d, fw_id %d, phy %d,"
  1356. "sas_addr 0x%llx\n", ioc->name, ds, phy_info->attached.channel,
  1357. phy_info->attached.id, phy_info->attached.phy_id,
  1358. (unsigned long long) sas_address);
  1359. port = mptsas_get_port(phy_info);
  1360. if (!port) {
  1361. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1362. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1363. __func__, fw_id, __LINE__));
  1364. return;
  1365. }
  1366. port_info = phy_info->portinfo;
  1367. phy_info_parent = port_info->phy_info;
  1368. for (i = 0; i < port_info->num_phys; i++, phy_info_parent++) {
  1369. if (!phy_info_parent->phy)
  1370. continue;
  1371. if (phy_info_parent->attached.sas_address !=
  1372. sas_address)
  1373. continue;
  1374. dev_printk(KERN_DEBUG, &phy_info_parent->phy->dev,
  1375. MYIOC_s_FMT "delete phy %d, phy-obj (0x%p)\n",
  1376. ioc->name, phy_info_parent->phy_id,
  1377. phy_info_parent->phy);
  1378. sas_port_delete_phy(port, phy_info_parent->phy);
  1379. }
  1380. dev_printk(KERN_DEBUG, &port->dev, MYIOC_s_FMT
  1381. "delete port %d, sas_addr (0x%llx)\n", ioc->name,
  1382. port->port_identifier, (unsigned long long)sas_address);
  1383. sas_port_delete(port);
  1384. mptsas_set_port(ioc, phy_info, NULL);
  1385. mptsas_port_delete(ioc, phy_info->port_details);
  1386. }
  1387. static struct mptsas_phyinfo *
  1388. mptsas_refreshing_device_handles(MPT_ADAPTER *ioc,
  1389. struct mptsas_devinfo *sas_device)
  1390. {
  1391. struct mptsas_phyinfo *phy_info;
  1392. struct mptsas_portinfo *port_info;
  1393. int i;
  1394. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  1395. sas_device->sas_address);
  1396. if (!phy_info)
  1397. goto out;
  1398. port_info = phy_info->portinfo;
  1399. if (!port_info)
  1400. goto out;
  1401. mutex_lock(&ioc->sas_topology_mutex);
  1402. for (i = 0; i < port_info->num_phys; i++) {
  1403. if (port_info->phy_info[i].attached.sas_address !=
  1404. sas_device->sas_address)
  1405. continue;
  1406. port_info->phy_info[i].attached.channel = sas_device->channel;
  1407. port_info->phy_info[i].attached.id = sas_device->id;
  1408. port_info->phy_info[i].attached.sas_address =
  1409. sas_device->sas_address;
  1410. port_info->phy_info[i].attached.handle = sas_device->handle;
  1411. port_info->phy_info[i].attached.handle_parent =
  1412. sas_device->handle_parent;
  1413. port_info->phy_info[i].attached.handle_enclosure =
  1414. sas_device->handle_enclosure;
  1415. }
  1416. mutex_unlock(&ioc->sas_topology_mutex);
  1417. out:
  1418. return phy_info;
  1419. }
  1420. /**
  1421. * mptsas_firmware_event_work - work thread for processing fw events
  1422. * @work: work queue payload containing info describing the event
  1423. * Context: user
  1424. *
  1425. */
  1426. static void
  1427. mptsas_firmware_event_work(struct work_struct *work)
  1428. {
  1429. struct fw_event_work *fw_event =
  1430. container_of(work, struct fw_event_work, work.work);
  1431. MPT_ADAPTER *ioc = fw_event->ioc;
  1432. /* special rescan topology handling */
  1433. if (fw_event->event == -1) {
  1434. if (ioc->in_rescan) {
  1435. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1436. "%s: rescan ignored as it is in progress\n",
  1437. ioc->name, __func__));
  1438. return;
  1439. }
  1440. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: rescan after "
  1441. "reset\n", ioc->name, __func__));
  1442. ioc->in_rescan = 1;
  1443. mptsas_not_responding_devices(ioc);
  1444. mptsas_scan_sas_topology(ioc);
  1445. ioc->in_rescan = 0;
  1446. mptsas_free_fw_event(ioc, fw_event);
  1447. mptsas_fw_event_on(ioc);
  1448. return;
  1449. }
  1450. /* events handling turned off during host reset */
  1451. if (ioc->fw_events_off) {
  1452. mptsas_free_fw_event(ioc, fw_event);
  1453. return;
  1454. }
  1455. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: fw_event=(0x%p), "
  1456. "event = (0x%02x)\n", ioc->name, __func__, fw_event,
  1457. (fw_event->event & 0xFF)));
  1458. switch (fw_event->event) {
  1459. case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
  1460. mptsas_send_sas_event(fw_event);
  1461. break;
  1462. case MPI_EVENT_INTEGRATED_RAID:
  1463. mptsas_send_raid_event(fw_event);
  1464. break;
  1465. case MPI_EVENT_IR2:
  1466. mptsas_send_ir2_event(fw_event);
  1467. break;
  1468. case MPI_EVENT_PERSISTENT_TABLE_FULL:
  1469. mptbase_sas_persist_operation(ioc,
  1470. MPI_SAS_OP_CLEAR_NOT_PRESENT);
  1471. mptsas_free_fw_event(ioc, fw_event);
  1472. break;
  1473. case MPI_EVENT_SAS_BROADCAST_PRIMITIVE:
  1474. mptsas_broadcast_primative_work(fw_event);
  1475. break;
  1476. case MPI_EVENT_SAS_EXPANDER_STATUS_CHANGE:
  1477. mptsas_send_expander_event(fw_event);
  1478. break;
  1479. case MPI_EVENT_SAS_PHY_LINK_STATUS:
  1480. mptsas_send_link_status_event(fw_event);
  1481. break;
  1482. case MPI_EVENT_QUEUE_FULL:
  1483. mptsas_handle_queue_full_event(fw_event);
  1484. break;
  1485. }
  1486. }
  1487. static int
  1488. mptsas_slave_configure(struct scsi_device *sdev)
  1489. {
  1490. struct Scsi_Host *host = sdev->host;
  1491. MPT_SCSI_HOST *hd = shost_priv(host);
  1492. MPT_ADAPTER *ioc = hd->ioc;
  1493. VirtDevice *vdevice = sdev->hostdata;
  1494. if (vdevice->vtarget->deleted) {
  1495. sdev_printk(KERN_INFO, sdev, "clearing deleted flag\n");
  1496. vdevice->vtarget->deleted = 0;
  1497. }
  1498. /*
  1499. * RAID volumes placed beyond the last expected port.
  1500. * Ignore sending sas mode pages in that case..
  1501. */
  1502. if (sdev->channel == MPTSAS_RAID_CHANNEL) {
  1503. mptsas_add_device_component_starget_ir(ioc, scsi_target(sdev));
  1504. goto out;
  1505. }
  1506. sas_read_port_mode_page(sdev);
  1507. mptsas_add_device_component_starget(ioc, scsi_target(sdev));
  1508. out:
  1509. return mptscsih_slave_configure(sdev);
  1510. }
  1511. static int
  1512. mptsas_target_alloc(struct scsi_target *starget)
  1513. {
  1514. struct Scsi_Host *host = dev_to_shost(&starget->dev);
  1515. MPT_SCSI_HOST *hd = shost_priv(host);
  1516. VirtTarget *vtarget;
  1517. u8 id, channel;
  1518. struct sas_rphy *rphy;
  1519. struct mptsas_portinfo *p;
  1520. int i;
  1521. MPT_ADAPTER *ioc = hd->ioc;
  1522. vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
  1523. if (!vtarget)
  1524. return -ENOMEM;
  1525. vtarget->starget = starget;
  1526. vtarget->ioc_id = ioc->id;
  1527. vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
  1528. id = starget->id;
  1529. channel = 0;
  1530. /*
  1531. * RAID volumes placed beyond the last expected port.
  1532. */
  1533. if (starget->channel == MPTSAS_RAID_CHANNEL) {
  1534. if (!ioc->raid_data.pIocPg2) {
  1535. kfree(vtarget);
  1536. return -ENXIO;
  1537. }
  1538. for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  1539. if (id == ioc->raid_data.pIocPg2->
  1540. RaidVolume[i].VolumeID) {
  1541. channel = ioc->raid_data.pIocPg2->
  1542. RaidVolume[i].VolumeBus;
  1543. }
  1544. }
  1545. vtarget->raidVolume = 1;
  1546. goto out;
  1547. }
  1548. rphy = dev_to_rphy(starget->dev.parent);
  1549. mutex_lock(&ioc->sas_topology_mutex);
  1550. list_for_each_entry(p, &ioc->sas_topology, list) {
  1551. for (i = 0; i < p->num_phys; i++) {
  1552. if (p->phy_info[i].attached.sas_address !=
  1553. rphy->identify.sas_address)
  1554. continue;
  1555. id = p->phy_info[i].attached.id;
  1556. channel = p->phy_info[i].attached.channel;
  1557. mptsas_set_starget(&p->phy_info[i], starget);
  1558. /*
  1559. * Exposing hidden raid components
  1560. */
  1561. if (mptscsih_is_phys_disk(ioc, channel, id)) {
  1562. id = mptscsih_raid_id_to_num(ioc,
  1563. channel, id);
  1564. vtarget->tflags |=
  1565. MPT_TARGET_FLAGS_RAID_COMPONENT;
  1566. p->phy_info[i].attached.phys_disk_num = id;
  1567. }
  1568. mutex_unlock(&ioc->sas_topology_mutex);
  1569. goto out;
  1570. }
  1571. }
  1572. mutex_unlock(&ioc->sas_topology_mutex);
  1573. kfree(vtarget);
  1574. return -ENXIO;
  1575. out:
  1576. vtarget->id = id;
  1577. vtarget->channel = channel;
  1578. starget->hostdata = vtarget;
  1579. return 0;
  1580. }
  1581. static void
  1582. mptsas_target_destroy(struct scsi_target *starget)
  1583. {
  1584. struct Scsi_Host *host = dev_to_shost(&starget->dev);
  1585. MPT_SCSI_HOST *hd = shost_priv(host);
  1586. struct sas_rphy *rphy;
  1587. struct mptsas_portinfo *p;
  1588. int i;
  1589. MPT_ADAPTER *ioc = hd->ioc;
  1590. VirtTarget *vtarget;
  1591. if (!starget->hostdata)
  1592. return;
  1593. vtarget = starget->hostdata;
  1594. mptsas_del_device_component_by_os(ioc, starget->channel,
  1595. starget->id);
  1596. if (starget->channel == MPTSAS_RAID_CHANNEL)
  1597. goto out;
  1598. rphy = dev_to_rphy(starget->dev.parent);
  1599. list_for_each_entry(p, &ioc->sas_topology, list) {
  1600. for (i = 0; i < p->num_phys; i++) {
  1601. if (p->phy_info[i].attached.sas_address !=
  1602. rphy->identify.sas_address)
  1603. continue;
  1604. starget_printk(KERN_INFO, starget, MYIOC_s_FMT
  1605. "delete device: fw_channel %d, fw_id %d, phy %d, "
  1606. "sas_addr 0x%llx\n", ioc->name,
  1607. p->phy_info[i].attached.channel,
  1608. p->phy_info[i].attached.id,
  1609. p->phy_info[i].attached.phy_id, (unsigned long long)
  1610. p->phy_info[i].attached.sas_address);
  1611. mptsas_set_starget(&p->phy_info[i], NULL);
  1612. }
  1613. }
  1614. out:
  1615. vtarget->starget = NULL;
  1616. kfree(starget->hostdata);
  1617. starget->hostdata = NULL;
  1618. }
  1619. static int
  1620. mptsas_slave_alloc(struct scsi_device *sdev)
  1621. {
  1622. struct Scsi_Host *host = sdev->host;
  1623. MPT_SCSI_HOST *hd = shost_priv(host);
  1624. struct sas_rphy *rphy;
  1625. struct mptsas_portinfo *p;
  1626. VirtDevice *vdevice;
  1627. struct scsi_target *starget;
  1628. int i;
  1629. MPT_ADAPTER *ioc = hd->ioc;
  1630. vdevice = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
  1631. if (!vdevice) {
  1632. printk(MYIOC_s_ERR_FMT "slave_alloc kzalloc(%zd) FAILED!\n",
  1633. ioc->name, sizeof(VirtDevice));
  1634. return -ENOMEM;
  1635. }
  1636. starget = scsi_target(sdev);
  1637. vdevice->vtarget = starget->hostdata;
  1638. if (sdev->channel == MPTSAS_RAID_CHANNEL)
  1639. goto out;
  1640. rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
  1641. mutex_lock(&ioc->sas_topology_mutex);
  1642. list_for_each_entry(p, &ioc->sas_topology, list) {
  1643. for (i = 0; i < p->num_phys; i++) {
  1644. if (p->phy_info[i].attached.sas_address !=
  1645. rphy->identify.sas_address)
  1646. continue;
  1647. vdevice->lun = sdev->lun;
  1648. /*
  1649. * Exposing hidden raid components
  1650. */
  1651. if (mptscsih_is_phys_disk(ioc,
  1652. p->phy_info[i].attached.channel,
  1653. p->phy_info[i].attached.id))
  1654. sdev->no_uld_attach = 1;
  1655. mutex_unlock(&ioc->sas_topology_mutex);
  1656. goto out;
  1657. }
  1658. }
  1659. mutex_unlock(&ioc->sas_topology_mutex);
  1660. kfree(vdevice);
  1661. return -ENXIO;
  1662. out:
  1663. vdevice->vtarget->num_luns++;
  1664. sdev->hostdata = vdevice;
  1665. return 0;
  1666. }
  1667. static int
  1668. mptsas_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *SCpnt)
  1669. {
  1670. MPT_SCSI_HOST *hd;
  1671. MPT_ADAPTER *ioc;
  1672. VirtDevice *vdevice = SCpnt->device->hostdata;
  1673. if (!vdevice || !vdevice->vtarget || vdevice->vtarget->deleted) {
  1674. SCpnt->result = DID_NO_CONNECT << 16;
  1675. SCpnt->scsi_done(SCpnt);
  1676. return 0;
  1677. }
  1678. hd = shost_priv(shost);
  1679. ioc = hd->ioc;
  1680. if (ioc->sas_discovery_quiesce_io)
  1681. return SCSI_MLQUEUE_HOST_BUSY;
  1682. if (ioc->debug_level & MPT_DEBUG_SCSI)
  1683. scsi_print_command(SCpnt);
  1684. return mptscsih_qcmd(SCpnt);
  1685. }
  1686. /**
  1687. * mptsas_mptsas_eh_timed_out - resets the scsi_cmnd timeout
  1688. * if the device under question is currently in the
  1689. * device removal delay.
  1690. * @sc: scsi command that the midlayer is about to time out
  1691. *
  1692. **/
  1693. static enum blk_eh_timer_return mptsas_eh_timed_out(struct scsi_cmnd *sc)
  1694. {
  1695. MPT_SCSI_HOST *hd;
  1696. MPT_ADAPTER *ioc;
  1697. VirtDevice *vdevice;
  1698. enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
  1699. hd = shost_priv(sc->device->host);
  1700. if (hd == NULL) {
  1701. printk(KERN_ERR MYNAM ": %s: Can't locate host! (sc=%p)\n",
  1702. __func__, sc);
  1703. goto done;
  1704. }
  1705. ioc = hd->ioc;
  1706. if (ioc->bus_type != SAS) {
  1707. printk(KERN_ERR MYNAM ": %s: Wrong bus type (sc=%p)\n",
  1708. __func__, sc);
  1709. goto done;
  1710. }
  1711. /* In case if IOC is in reset from internal context.
  1712. * Do not execute EEH for the same IOC. SML should to reset timer.
  1713. */
  1714. if (ioc->ioc_reset_in_progress) {
  1715. dtmprintk(ioc, printk(MYIOC_s_WARN_FMT ": %s: ioc is in reset,"
  1716. "SML need to reset the timer (sc=%p)\n",
  1717. ioc->name, __func__, sc));
  1718. rc = BLK_EH_RESET_TIMER;
  1719. }
  1720. vdevice = sc->device->hostdata;
  1721. if (vdevice && vdevice->vtarget && (vdevice->vtarget->inDMD
  1722. || vdevice->vtarget->deleted)) {
  1723. dtmprintk(ioc, printk(MYIOC_s_WARN_FMT ": %s: target removed "
  1724. "or in device removal delay (sc=%p)\n",
  1725. ioc->name, __func__, sc));
  1726. rc = BLK_EH_RESET_TIMER;
  1727. goto done;
  1728. }
  1729. done:
  1730. return rc;
  1731. }
  1732. static struct scsi_host_template mptsas_driver_template = {
  1733. .module = THIS_MODULE,
  1734. .proc_name = "mptsas",
  1735. .show_info = mptscsih_show_info,
  1736. .name = "MPT SAS Host",
  1737. .info = mptscsih_info,
  1738. .queuecommand = mptsas_qcmd,
  1739. .target_alloc = mptsas_target_alloc,
  1740. .slave_alloc = mptsas_slave_alloc,
  1741. .slave_configure = mptsas_slave_configure,
  1742. .target_destroy = mptsas_target_destroy,
  1743. .slave_destroy = mptscsih_slave_destroy,
  1744. .change_queue_depth = mptscsih_change_queue_depth,
  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. .use_clustering = ENABLE_CLUSTERING,
  1755. .shost_attrs = mptscsih_host_attrs,
  1756. .no_write_same = 1,
  1757. };
  1758. static int mptsas_get_linkerrors(struct sas_phy *phy)
  1759. {
  1760. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  1761. ConfigExtendedPageHeader_t hdr;
  1762. CONFIGPARMS cfg;
  1763. SasPhyPage1_t *buffer;
  1764. dma_addr_t dma_handle;
  1765. int error;
  1766. /* FIXME: only have link errors on local phys */
  1767. if (!scsi_is_sas_phy_local(phy))
  1768. return -EINVAL;
  1769. hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
  1770. hdr.ExtPageLength = 0;
  1771. hdr.PageNumber = 1 /* page number 1*/;
  1772. hdr.Reserved1 = 0;
  1773. hdr.Reserved2 = 0;
  1774. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1775. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  1776. cfg.cfghdr.ehdr = &hdr;
  1777. cfg.physAddr = -1;
  1778. cfg.pageAddr = phy->identify.phy_identifier;
  1779. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1780. cfg.dir = 0; /* read */
  1781. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  1782. error = mpt_config(ioc, &cfg);
  1783. if (error)
  1784. return error;
  1785. if (!hdr.ExtPageLength)
  1786. return -ENXIO;
  1787. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1788. &dma_handle);
  1789. if (!buffer)
  1790. return -ENOMEM;
  1791. cfg.physAddr = dma_handle;
  1792. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1793. error = mpt_config(ioc, &cfg);
  1794. if (error)
  1795. goto out_free_consistent;
  1796. mptsas_print_phy_pg1(ioc, buffer);
  1797. phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
  1798. phy->running_disparity_error_count =
  1799. le32_to_cpu(buffer->RunningDisparityErrorCount);
  1800. phy->loss_of_dword_sync_count =
  1801. le32_to_cpu(buffer->LossDwordSynchCount);
  1802. phy->phy_reset_problem_count =
  1803. le32_to_cpu(buffer->PhyResetProblemCount);
  1804. out_free_consistent:
  1805. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1806. buffer, dma_handle);
  1807. return error;
  1808. }
  1809. static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
  1810. MPT_FRAME_HDR *reply)
  1811. {
  1812. ioc->sas_mgmt.status |= MPT_MGMT_STATUS_COMMAND_GOOD;
  1813. if (reply != NULL) {
  1814. ioc->sas_mgmt.status |= MPT_MGMT_STATUS_RF_VALID;
  1815. memcpy(ioc->sas_mgmt.reply, reply,
  1816. min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
  1817. }
  1818. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_PENDING) {
  1819. ioc->sas_mgmt.status &= ~MPT_MGMT_STATUS_PENDING;
  1820. complete(&ioc->sas_mgmt.done);
  1821. return 1;
  1822. }
  1823. return 0;
  1824. }
  1825. static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
  1826. {
  1827. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  1828. SasIoUnitControlRequest_t *req;
  1829. SasIoUnitControlReply_t *reply;
  1830. MPT_FRAME_HDR *mf;
  1831. MPIHeader_t *hdr;
  1832. unsigned long timeleft;
  1833. int error = -ERESTARTSYS;
  1834. /* FIXME: fusion doesn't allow non-local phy reset */
  1835. if (!scsi_is_sas_phy_local(phy))
  1836. return -EINVAL;
  1837. /* not implemented for expanders */
  1838. if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
  1839. return -ENXIO;
  1840. if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
  1841. goto out;
  1842. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  1843. if (!mf) {
  1844. error = -ENOMEM;
  1845. goto out_unlock;
  1846. }
  1847. hdr = (MPIHeader_t *) mf;
  1848. req = (SasIoUnitControlRequest_t *)mf;
  1849. memset(req, 0, sizeof(SasIoUnitControlRequest_t));
  1850. req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
  1851. req->MsgContext = hdr->MsgContext;
  1852. req->Operation = hard_reset ?
  1853. MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
  1854. req->PhyNum = phy->identify.phy_identifier;
  1855. INITIALIZE_MGMT_STATUS(ioc->sas_mgmt.status)
  1856. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  1857. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
  1858. 10 * HZ);
  1859. if (!(ioc->sas_mgmt.status & MPT_MGMT_STATUS_COMMAND_GOOD)) {
  1860. error = -ETIME;
  1861. mpt_free_msg_frame(ioc, mf);
  1862. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_DID_IOCRESET)
  1863. goto out_unlock;
  1864. if (!timeleft)
  1865. mpt_Soft_Hard_ResetHandler(ioc, CAN_SLEEP);
  1866. goto out_unlock;
  1867. }
  1868. /* a reply frame is expected */
  1869. if ((ioc->sas_mgmt.status &
  1870. MPT_MGMT_STATUS_RF_VALID) == 0) {
  1871. error = -ENXIO;
  1872. goto out_unlock;
  1873. }
  1874. /* process the completed Reply Message Frame */
  1875. reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
  1876. if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
  1877. printk(MYIOC_s_INFO_FMT "%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
  1878. ioc->name, __func__, reply->IOCStatus, reply->IOCLogInfo);
  1879. error = -ENXIO;
  1880. goto out_unlock;
  1881. }
  1882. error = 0;
  1883. out_unlock:
  1884. CLEAR_MGMT_STATUS(ioc->sas_mgmt.status)
  1885. mutex_unlock(&ioc->sas_mgmt.mutex);
  1886. out:
  1887. return error;
  1888. }
  1889. static int
  1890. mptsas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  1891. {
  1892. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  1893. int i, error;
  1894. struct mptsas_portinfo *p;
  1895. struct mptsas_enclosure enclosure_info;
  1896. u64 enclosure_handle;
  1897. mutex_lock(&ioc->sas_topology_mutex);
  1898. list_for_each_entry(p, &ioc->sas_topology, list) {
  1899. for (i = 0; i < p->num_phys; i++) {
  1900. if (p->phy_info[i].attached.sas_address ==
  1901. rphy->identify.sas_address) {
  1902. enclosure_handle = p->phy_info[i].
  1903. attached.handle_enclosure;
  1904. goto found_info;
  1905. }
  1906. }
  1907. }
  1908. mutex_unlock(&ioc->sas_topology_mutex);
  1909. return -ENXIO;
  1910. found_info:
  1911. mutex_unlock(&ioc->sas_topology_mutex);
  1912. memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
  1913. error = mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
  1914. (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
  1915. MPI_SAS_ENCLOS_PGAD_FORM_SHIFT), enclosure_handle);
  1916. if (!error)
  1917. *identifier = enclosure_info.enclosure_logical_id;
  1918. return error;
  1919. }
  1920. static int
  1921. mptsas_get_bay_identifier(struct sas_rphy *rphy)
  1922. {
  1923. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  1924. struct mptsas_portinfo *p;
  1925. int i, rc;
  1926. mutex_lock(&ioc->sas_topology_mutex);
  1927. list_for_each_entry(p, &ioc->sas_topology, list) {
  1928. for (i = 0; i < p->num_phys; i++) {
  1929. if (p->phy_info[i].attached.sas_address ==
  1930. rphy->identify.sas_address) {
  1931. rc = p->phy_info[i].attached.slot;
  1932. goto out;
  1933. }
  1934. }
  1935. }
  1936. rc = -ENXIO;
  1937. out:
  1938. mutex_unlock(&ioc->sas_topology_mutex);
  1939. return rc;
  1940. }
  1941. static int mptsas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1942. struct request *req)
  1943. {
  1944. MPT_ADAPTER *ioc = ((MPT_SCSI_HOST *) shost->hostdata)->ioc;
  1945. MPT_FRAME_HDR *mf;
  1946. SmpPassthroughRequest_t *smpreq;
  1947. struct request *rsp = req->next_rq;
  1948. int ret;
  1949. int flagsLength;
  1950. unsigned long timeleft;
  1951. char *psge;
  1952. dma_addr_t dma_addr_in = 0;
  1953. dma_addr_t dma_addr_out = 0;
  1954. u64 sas_address = 0;
  1955. if (!rsp) {
  1956. printk(MYIOC_s_ERR_FMT "%s: the smp response space is missing\n",
  1957. ioc->name, __func__);
  1958. return -EINVAL;
  1959. }
  1960. /* do we need to support multiple segments? */
  1961. if (bio_multiple_segments(req->bio) ||
  1962. bio_multiple_segments(rsp->bio)) {
  1963. printk(MYIOC_s_ERR_FMT "%s: multiple segments req %u, rsp %u\n",
  1964. ioc->name, __func__, blk_rq_bytes(req), blk_rq_bytes(rsp));
  1965. return -EINVAL;
  1966. }
  1967. ret = mutex_lock_interruptible(&ioc->sas_mgmt.mutex);
  1968. if (ret)
  1969. goto out;
  1970. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  1971. if (!mf) {
  1972. ret = -ENOMEM;
  1973. goto out_unlock;
  1974. }
  1975. smpreq = (SmpPassthroughRequest_t *)mf;
  1976. memset(smpreq, 0, sizeof(*smpreq));
  1977. smpreq->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
  1978. smpreq->Function = MPI_FUNCTION_SMP_PASSTHROUGH;
  1979. if (rphy)
  1980. sas_address = rphy->identify.sas_address;
  1981. else {
  1982. struct mptsas_portinfo *port_info;
  1983. mutex_lock(&ioc->sas_topology_mutex);
  1984. port_info = ioc->hba_port_info;
  1985. if (port_info && port_info->phy_info)
  1986. sas_address =
  1987. port_info->phy_info[0].phy->identify.sas_address;
  1988. mutex_unlock(&ioc->sas_topology_mutex);
  1989. }
  1990. *((u64 *)&smpreq->SASAddress) = cpu_to_le64(sas_address);
  1991. psge = (char *)
  1992. (((int *) mf) + (offsetof(SmpPassthroughRequest_t, SGL) / 4));
  1993. /* request */
  1994. flagsLength = (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
  1995. MPI_SGE_FLAGS_END_OF_BUFFER |
  1996. MPI_SGE_FLAGS_DIRECTION)
  1997. << MPI_SGE_FLAGS_SHIFT;
  1998. flagsLength |= (blk_rq_bytes(req) - 4);
  1999. dma_addr_out = pci_map_single(ioc->pcidev, bio_data(req->bio),
  2000. blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
  2001. if (!dma_addr_out)
  2002. goto put_mf;
  2003. ioc->add_sge(psge, flagsLength, dma_addr_out);
  2004. psge += ioc->SGE_size;
  2005. /* response */
  2006. flagsLength = MPI_SGE_FLAGS_SIMPLE_ELEMENT |
  2007. MPI_SGE_FLAGS_SYSTEM_ADDRESS |
  2008. MPI_SGE_FLAGS_IOC_TO_HOST |
  2009. MPI_SGE_FLAGS_END_OF_BUFFER;
  2010. flagsLength = flagsLength << MPI_SGE_FLAGS_SHIFT;
  2011. flagsLength |= blk_rq_bytes(rsp) + 4;
  2012. dma_addr_in = pci_map_single(ioc->pcidev, bio_data(rsp->bio),
  2013. blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
  2014. if (!dma_addr_in)
  2015. goto unmap;
  2016. ioc->add_sge(psge, flagsLength, dma_addr_in);
  2017. INITIALIZE_MGMT_STATUS(ioc->sas_mgmt.status)
  2018. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  2019. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done, 10 * HZ);
  2020. if (!(ioc->sas_mgmt.status & MPT_MGMT_STATUS_COMMAND_GOOD)) {
  2021. ret = -ETIME;
  2022. mpt_free_msg_frame(ioc, mf);
  2023. mf = NULL;
  2024. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_DID_IOCRESET)
  2025. goto unmap;
  2026. if (!timeleft)
  2027. mpt_Soft_Hard_ResetHandler(ioc, CAN_SLEEP);
  2028. goto unmap;
  2029. }
  2030. mf = NULL;
  2031. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_RF_VALID) {
  2032. SmpPassthroughReply_t *smprep;
  2033. smprep = (SmpPassthroughReply_t *)ioc->sas_mgmt.reply;
  2034. memcpy(req->sense, smprep, sizeof(*smprep));
  2035. req->sense_len = sizeof(*smprep);
  2036. req->resid_len = 0;
  2037. rsp->resid_len -= smprep->ResponseDataLength;
  2038. } else {
  2039. printk(MYIOC_s_ERR_FMT
  2040. "%s: smp passthru reply failed to be returned\n",
  2041. ioc->name, __func__);
  2042. ret = -ENXIO;
  2043. }
  2044. unmap:
  2045. if (dma_addr_out)
  2046. pci_unmap_single(ioc->pcidev, dma_addr_out, blk_rq_bytes(req),
  2047. PCI_DMA_BIDIRECTIONAL);
  2048. if (dma_addr_in)
  2049. pci_unmap_single(ioc->pcidev, dma_addr_in, blk_rq_bytes(rsp),
  2050. PCI_DMA_BIDIRECTIONAL);
  2051. put_mf:
  2052. if (mf)
  2053. mpt_free_msg_frame(ioc, mf);
  2054. out_unlock:
  2055. CLEAR_MGMT_STATUS(ioc->sas_mgmt.status)
  2056. mutex_unlock(&ioc->sas_mgmt.mutex);
  2057. out:
  2058. return ret;
  2059. }
  2060. static struct sas_function_template mptsas_transport_functions = {
  2061. .get_linkerrors = mptsas_get_linkerrors,
  2062. .get_enclosure_identifier = mptsas_get_enclosure_identifier,
  2063. .get_bay_identifier = mptsas_get_bay_identifier,
  2064. .phy_reset = mptsas_phy_reset,
  2065. .smp_handler = mptsas_smp_handler,
  2066. };
  2067. static struct scsi_transport_template *mptsas_transport_template;
  2068. static int
  2069. mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  2070. {
  2071. ConfigExtendedPageHeader_t hdr;
  2072. CONFIGPARMS cfg;
  2073. SasIOUnitPage0_t *buffer;
  2074. dma_addr_t dma_handle;
  2075. int error, i;
  2076. hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
  2077. hdr.ExtPageLength = 0;
  2078. hdr.PageNumber = 0;
  2079. hdr.Reserved1 = 0;
  2080. hdr.Reserved2 = 0;
  2081. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2082. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
  2083. cfg.cfghdr.ehdr = &hdr;
  2084. cfg.physAddr = -1;
  2085. cfg.pageAddr = 0;
  2086. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2087. cfg.dir = 0; /* read */
  2088. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2089. error = mpt_config(ioc, &cfg);
  2090. if (error)
  2091. goto out;
  2092. if (!hdr.ExtPageLength) {
  2093. error = -ENXIO;
  2094. goto out;
  2095. }
  2096. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2097. &dma_handle);
  2098. if (!buffer) {
  2099. error = -ENOMEM;
  2100. goto out;
  2101. }
  2102. cfg.physAddr = dma_handle;
  2103. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2104. error = mpt_config(ioc, &cfg);
  2105. if (error)
  2106. goto out_free_consistent;
  2107. port_info->num_phys = buffer->NumPhys;
  2108. port_info->phy_info = kcalloc(port_info->num_phys,
  2109. sizeof(struct mptsas_phyinfo), GFP_KERNEL);
  2110. if (!port_info->phy_info) {
  2111. error = -ENOMEM;
  2112. goto out_free_consistent;
  2113. }
  2114. ioc->nvdata_version_persistent =
  2115. le16_to_cpu(buffer->NvdataVersionPersistent);
  2116. ioc->nvdata_version_default =
  2117. le16_to_cpu(buffer->NvdataVersionDefault);
  2118. for (i = 0; i < port_info->num_phys; i++) {
  2119. mptsas_print_phy_data(ioc, &buffer->PhyData[i]);
  2120. port_info->phy_info[i].phy_id = i;
  2121. port_info->phy_info[i].port_id =
  2122. buffer->PhyData[i].Port;
  2123. port_info->phy_info[i].negotiated_link_rate =
  2124. buffer->PhyData[i].NegotiatedLinkRate;
  2125. port_info->phy_info[i].portinfo = port_info;
  2126. port_info->phy_info[i].handle =
  2127. le16_to_cpu(buffer->PhyData[i].ControllerDevHandle);
  2128. }
  2129. out_free_consistent:
  2130. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2131. buffer, dma_handle);
  2132. out:
  2133. return error;
  2134. }
  2135. static int
  2136. mptsas_sas_io_unit_pg1(MPT_ADAPTER *ioc)
  2137. {
  2138. ConfigExtendedPageHeader_t hdr;
  2139. CONFIGPARMS cfg;
  2140. SasIOUnitPage1_t *buffer;
  2141. dma_addr_t dma_handle;
  2142. int error;
  2143. u8 device_missing_delay;
  2144. memset(&hdr, 0, sizeof(ConfigExtendedPageHeader_t));
  2145. memset(&cfg, 0, sizeof(CONFIGPARMS));
  2146. cfg.cfghdr.ehdr = &hdr;
  2147. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2148. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2149. cfg.cfghdr.ehdr->PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2150. cfg.cfghdr.ehdr->ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
  2151. cfg.cfghdr.ehdr->PageVersion = MPI_SASIOUNITPAGE1_PAGEVERSION;
  2152. cfg.cfghdr.ehdr->PageNumber = 1;
  2153. error = mpt_config(ioc, &cfg);
  2154. if (error)
  2155. goto out;
  2156. if (!hdr.ExtPageLength) {
  2157. error = -ENXIO;
  2158. goto out;
  2159. }
  2160. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2161. &dma_handle);
  2162. if (!buffer) {
  2163. error = -ENOMEM;
  2164. goto out;
  2165. }
  2166. cfg.physAddr = dma_handle;
  2167. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2168. error = mpt_config(ioc, &cfg);
  2169. if (error)
  2170. goto out_free_consistent;
  2171. ioc->io_missing_delay =
  2172. le16_to_cpu(buffer->IODeviceMissingDelay);
  2173. device_missing_delay = buffer->ReportDeviceMissingDelay;
  2174. ioc->device_missing_delay = (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_UNIT_16) ?
  2175. (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16 :
  2176. device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2177. out_free_consistent:
  2178. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2179. buffer, dma_handle);
  2180. out:
  2181. return error;
  2182. }
  2183. static int
  2184. mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  2185. u32 form, u32 form_specific)
  2186. {
  2187. ConfigExtendedPageHeader_t hdr;
  2188. CONFIGPARMS cfg;
  2189. SasPhyPage0_t *buffer;
  2190. dma_addr_t dma_handle;
  2191. int error;
  2192. hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
  2193. hdr.ExtPageLength = 0;
  2194. hdr.PageNumber = 0;
  2195. hdr.Reserved1 = 0;
  2196. hdr.Reserved2 = 0;
  2197. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2198. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  2199. cfg.cfghdr.ehdr = &hdr;
  2200. cfg.dir = 0; /* read */
  2201. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2202. /* Get Phy Pg 0 for each Phy. */
  2203. cfg.physAddr = -1;
  2204. cfg.pageAddr = form + form_specific;
  2205. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2206. error = mpt_config(ioc, &cfg);
  2207. if (error)
  2208. goto out;
  2209. if (!hdr.ExtPageLength) {
  2210. error = -ENXIO;
  2211. goto out;
  2212. }
  2213. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2214. &dma_handle);
  2215. if (!buffer) {
  2216. error = -ENOMEM;
  2217. goto out;
  2218. }
  2219. cfg.physAddr = dma_handle;
  2220. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2221. error = mpt_config(ioc, &cfg);
  2222. if (error)
  2223. goto out_free_consistent;
  2224. mptsas_print_phy_pg0(ioc, buffer);
  2225. phy_info->hw_link_rate = buffer->HwLinkRate;
  2226. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  2227. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  2228. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  2229. out_free_consistent:
  2230. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2231. buffer, dma_handle);
  2232. out:
  2233. return error;
  2234. }
  2235. static int
  2236. mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
  2237. u32 form, u32 form_specific)
  2238. {
  2239. ConfigExtendedPageHeader_t hdr;
  2240. CONFIGPARMS cfg;
  2241. SasDevicePage0_t *buffer;
  2242. dma_addr_t dma_handle;
  2243. __le64 sas_address;
  2244. int error=0;
  2245. hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
  2246. hdr.ExtPageLength = 0;
  2247. hdr.PageNumber = 0;
  2248. hdr.Reserved1 = 0;
  2249. hdr.Reserved2 = 0;
  2250. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2251. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
  2252. cfg.cfghdr.ehdr = &hdr;
  2253. cfg.pageAddr = form + form_specific;
  2254. cfg.physAddr = -1;
  2255. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2256. cfg.dir = 0; /* read */
  2257. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2258. memset(device_info, 0, sizeof(struct mptsas_devinfo));
  2259. error = mpt_config(ioc, &cfg);
  2260. if (error)
  2261. goto out;
  2262. if (!hdr.ExtPageLength) {
  2263. error = -ENXIO;
  2264. goto out;
  2265. }
  2266. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2267. &dma_handle);
  2268. if (!buffer) {
  2269. error = -ENOMEM;
  2270. goto out;
  2271. }
  2272. cfg.physAddr = dma_handle;
  2273. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2274. error = mpt_config(ioc, &cfg);
  2275. if (error == MPI_IOCSTATUS_CONFIG_INVALID_PAGE) {
  2276. error = -ENODEV;
  2277. goto out_free_consistent;
  2278. }
  2279. if (error)
  2280. goto out_free_consistent;
  2281. mptsas_print_device_pg0(ioc, buffer);
  2282. memset(device_info, 0, sizeof(struct mptsas_devinfo));
  2283. device_info->handle = le16_to_cpu(buffer->DevHandle);
  2284. device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
  2285. device_info->handle_enclosure =
  2286. le16_to_cpu(buffer->EnclosureHandle);
  2287. device_info->slot = le16_to_cpu(buffer->Slot);
  2288. device_info->phy_id = buffer->PhyNum;
  2289. device_info->port_id = buffer->PhysicalPort;
  2290. device_info->id = buffer->TargetID;
  2291. device_info->phys_disk_num = ~0;
  2292. device_info->channel = buffer->Bus;
  2293. memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
  2294. device_info->sas_address = le64_to_cpu(sas_address);
  2295. device_info->device_info =
  2296. le32_to_cpu(buffer->DeviceInfo);
  2297. device_info->flags = le16_to_cpu(buffer->Flags);
  2298. out_free_consistent:
  2299. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2300. buffer, dma_handle);
  2301. out:
  2302. return error;
  2303. }
  2304. static int
  2305. mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
  2306. u32 form, u32 form_specific)
  2307. {
  2308. ConfigExtendedPageHeader_t hdr;
  2309. CONFIGPARMS cfg;
  2310. SasExpanderPage0_t *buffer;
  2311. dma_addr_t dma_handle;
  2312. int i, error;
  2313. __le64 sas_address;
  2314. memset(port_info, 0, sizeof(struct mptsas_portinfo));
  2315. hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
  2316. hdr.ExtPageLength = 0;
  2317. hdr.PageNumber = 0;
  2318. hdr.Reserved1 = 0;
  2319. hdr.Reserved2 = 0;
  2320. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2321. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  2322. cfg.cfghdr.ehdr = &hdr;
  2323. cfg.physAddr = -1;
  2324. cfg.pageAddr = form + form_specific;
  2325. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2326. cfg.dir = 0; /* read */
  2327. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2328. memset(port_info, 0, sizeof(struct mptsas_portinfo));
  2329. error = mpt_config(ioc, &cfg);
  2330. if (error)
  2331. goto out;
  2332. if (!hdr.ExtPageLength) {
  2333. error = -ENXIO;
  2334. goto out;
  2335. }
  2336. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2337. &dma_handle);
  2338. if (!buffer) {
  2339. error = -ENOMEM;
  2340. goto out;
  2341. }
  2342. cfg.physAddr = dma_handle;
  2343. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2344. error = mpt_config(ioc, &cfg);
  2345. if (error == MPI_IOCSTATUS_CONFIG_INVALID_PAGE) {
  2346. error = -ENODEV;
  2347. goto out_free_consistent;
  2348. }
  2349. if (error)
  2350. goto out_free_consistent;
  2351. /* save config data */
  2352. port_info->num_phys = (buffer->NumPhys) ? buffer->NumPhys : 1;
  2353. port_info->phy_info = kcalloc(port_info->num_phys,
  2354. sizeof(struct mptsas_phyinfo), GFP_KERNEL);
  2355. if (!port_info->phy_info) {
  2356. error = -ENOMEM;
  2357. goto out_free_consistent;
  2358. }
  2359. memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
  2360. for (i = 0; i < port_info->num_phys; i++) {
  2361. port_info->phy_info[i].portinfo = port_info;
  2362. port_info->phy_info[i].handle =
  2363. le16_to_cpu(buffer->DevHandle);
  2364. port_info->phy_info[i].identify.sas_address =
  2365. le64_to_cpu(sas_address);
  2366. port_info->phy_info[i].identify.handle_parent =
  2367. le16_to_cpu(buffer->ParentDevHandle);
  2368. }
  2369. out_free_consistent:
  2370. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2371. buffer, dma_handle);
  2372. out:
  2373. return error;
  2374. }
  2375. static int
  2376. mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  2377. u32 form, u32 form_specific)
  2378. {
  2379. ConfigExtendedPageHeader_t hdr;
  2380. CONFIGPARMS cfg;
  2381. SasExpanderPage1_t *buffer;
  2382. dma_addr_t dma_handle;
  2383. int error=0;
  2384. hdr.PageVersion = MPI_SASEXPANDER1_PAGEVERSION;
  2385. hdr.ExtPageLength = 0;
  2386. hdr.PageNumber = 1;
  2387. hdr.Reserved1 = 0;
  2388. hdr.Reserved2 = 0;
  2389. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2390. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  2391. cfg.cfghdr.ehdr = &hdr;
  2392. cfg.physAddr = -1;
  2393. cfg.pageAddr = form + form_specific;
  2394. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2395. cfg.dir = 0; /* read */
  2396. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2397. error = mpt_config(ioc, &cfg);
  2398. if (error)
  2399. goto out;
  2400. if (!hdr.ExtPageLength) {
  2401. error = -ENXIO;
  2402. goto out;
  2403. }
  2404. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2405. &dma_handle);
  2406. if (!buffer) {
  2407. error = -ENOMEM;
  2408. goto out;
  2409. }
  2410. cfg.physAddr = dma_handle;
  2411. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2412. error = mpt_config(ioc, &cfg);
  2413. if (error == MPI_IOCSTATUS_CONFIG_INVALID_PAGE) {
  2414. error = -ENODEV;
  2415. goto out_free_consistent;
  2416. }
  2417. if (error)
  2418. goto out_free_consistent;
  2419. mptsas_print_expander_pg1(ioc, buffer);
  2420. /* save config data */
  2421. phy_info->phy_id = buffer->PhyIdentifier;
  2422. phy_info->port_id = buffer->PhysicalPort;
  2423. phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
  2424. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  2425. phy_info->hw_link_rate = buffer->HwLinkRate;
  2426. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  2427. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  2428. out_free_consistent:
  2429. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2430. buffer, dma_handle);
  2431. out:
  2432. return error;
  2433. }
  2434. struct rep_manu_request{
  2435. u8 smp_frame_type;
  2436. u8 function;
  2437. u8 reserved;
  2438. u8 request_length;
  2439. };
  2440. struct rep_manu_reply{
  2441. u8 smp_frame_type; /* 0x41 */
  2442. u8 function; /* 0x01 */
  2443. u8 function_result;
  2444. u8 response_length;
  2445. u16 expander_change_count;
  2446. u8 reserved0[2];
  2447. u8 sas_format:1;
  2448. u8 reserved1:7;
  2449. u8 reserved2[3];
  2450. u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
  2451. u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
  2452. u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
  2453. u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
  2454. u16 component_id;
  2455. u8 component_revision_id;
  2456. u8 reserved3;
  2457. u8 vendor_specific[8];
  2458. };
  2459. /**
  2460. * mptsas_exp_repmanufacture_info -
  2461. * @ioc: per adapter object
  2462. * @sas_address: expander sas address
  2463. * @edev: the sas_expander_device object
  2464. *
  2465. * Fills in the sas_expander_device object when SMP port is created.
  2466. *
  2467. * Returns 0 for success, non-zero for failure.
  2468. */
  2469. static int
  2470. mptsas_exp_repmanufacture_info(MPT_ADAPTER *ioc,
  2471. u64 sas_address, struct sas_expander_device *edev)
  2472. {
  2473. MPT_FRAME_HDR *mf;
  2474. SmpPassthroughRequest_t *smpreq;
  2475. SmpPassthroughReply_t *smprep;
  2476. struct rep_manu_reply *manufacture_reply;
  2477. struct rep_manu_request *manufacture_request;
  2478. int ret;
  2479. int flagsLength;
  2480. unsigned long timeleft;
  2481. char *psge;
  2482. unsigned long flags;
  2483. void *data_out = NULL;
  2484. dma_addr_t data_out_dma = 0;
  2485. u32 sz;
  2486. spin_lock_irqsave(&ioc->taskmgmt_lock, flags);
  2487. if (ioc->ioc_reset_in_progress) {
  2488. spin_unlock_irqrestore(&ioc->taskmgmt_lock, flags);
  2489. printk(MYIOC_s_INFO_FMT "%s: host reset in progress!\n",
  2490. __func__, ioc->name);
  2491. return -EFAULT;
  2492. }
  2493. spin_unlock_irqrestore(&ioc->taskmgmt_lock, flags);
  2494. ret = mutex_lock_interruptible(&ioc->sas_mgmt.mutex);
  2495. if (ret)
  2496. goto out;
  2497. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  2498. if (!mf) {
  2499. ret = -ENOMEM;
  2500. goto out_unlock;
  2501. }
  2502. smpreq = (SmpPassthroughRequest_t *)mf;
  2503. memset(smpreq, 0, sizeof(*smpreq));
  2504. sz = sizeof(struct rep_manu_request) + sizeof(struct rep_manu_reply);
  2505. data_out = pci_alloc_consistent(ioc->pcidev, sz, &data_out_dma);
  2506. if (!data_out) {
  2507. printk(KERN_ERR "Memory allocation failure at %s:%d/%s()!\n",
  2508. __FILE__, __LINE__, __func__);
  2509. ret = -ENOMEM;
  2510. goto put_mf;
  2511. }
  2512. manufacture_request = data_out;
  2513. manufacture_request->smp_frame_type = 0x40;
  2514. manufacture_request->function = 1;
  2515. manufacture_request->reserved = 0;
  2516. manufacture_request->request_length = 0;
  2517. smpreq->Function = MPI_FUNCTION_SMP_PASSTHROUGH;
  2518. smpreq->PhysicalPort = 0xFF;
  2519. *((u64 *)&smpreq->SASAddress) = cpu_to_le64(sas_address);
  2520. smpreq->RequestDataLength = sizeof(struct rep_manu_request);
  2521. psge = (char *)
  2522. (((int *) mf) + (offsetof(SmpPassthroughRequest_t, SGL) / 4));
  2523. flagsLength = MPI_SGE_FLAGS_SIMPLE_ELEMENT |
  2524. MPI_SGE_FLAGS_SYSTEM_ADDRESS |
  2525. MPI_SGE_FLAGS_HOST_TO_IOC |
  2526. MPI_SGE_FLAGS_END_OF_BUFFER;
  2527. flagsLength = flagsLength << MPI_SGE_FLAGS_SHIFT;
  2528. flagsLength |= sizeof(struct rep_manu_request);
  2529. ioc->add_sge(psge, flagsLength, data_out_dma);
  2530. psge += ioc->SGE_size;
  2531. flagsLength = MPI_SGE_FLAGS_SIMPLE_ELEMENT |
  2532. MPI_SGE_FLAGS_SYSTEM_ADDRESS |
  2533. MPI_SGE_FLAGS_IOC_TO_HOST |
  2534. MPI_SGE_FLAGS_END_OF_BUFFER;
  2535. flagsLength = flagsLength << MPI_SGE_FLAGS_SHIFT;
  2536. flagsLength |= sizeof(struct rep_manu_reply);
  2537. ioc->add_sge(psge, flagsLength, data_out_dma +
  2538. sizeof(struct rep_manu_request));
  2539. INITIALIZE_MGMT_STATUS(ioc->sas_mgmt.status)
  2540. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  2541. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done, 10 * HZ);
  2542. if (!(ioc->sas_mgmt.status & MPT_MGMT_STATUS_COMMAND_GOOD)) {
  2543. ret = -ETIME;
  2544. mpt_free_msg_frame(ioc, mf);
  2545. mf = NULL;
  2546. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_DID_IOCRESET)
  2547. goto out_free;
  2548. if (!timeleft)
  2549. mpt_Soft_Hard_ResetHandler(ioc, CAN_SLEEP);
  2550. goto out_free;
  2551. }
  2552. mf = NULL;
  2553. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_RF_VALID) {
  2554. u8 *tmp;
  2555. smprep = (SmpPassthroughReply_t *)ioc->sas_mgmt.reply;
  2556. if (le16_to_cpu(smprep->ResponseDataLength) !=
  2557. sizeof(struct rep_manu_reply))
  2558. goto out_free;
  2559. manufacture_reply = data_out + sizeof(struct rep_manu_request);
  2560. strncpy(edev->vendor_id, manufacture_reply->vendor_id,
  2561. SAS_EXPANDER_VENDOR_ID_LEN);
  2562. strncpy(edev->product_id, manufacture_reply->product_id,
  2563. SAS_EXPANDER_PRODUCT_ID_LEN);
  2564. strncpy(edev->product_rev, manufacture_reply->product_rev,
  2565. SAS_EXPANDER_PRODUCT_REV_LEN);
  2566. edev->level = manufacture_reply->sas_format;
  2567. if (manufacture_reply->sas_format) {
  2568. strncpy(edev->component_vendor_id,
  2569. manufacture_reply->component_vendor_id,
  2570. SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
  2571. tmp = (u8 *)&manufacture_reply->component_id;
  2572. edev->component_id = tmp[0] << 8 | tmp[1];
  2573. edev->component_revision_id =
  2574. manufacture_reply->component_revision_id;
  2575. }
  2576. } else {
  2577. printk(MYIOC_s_ERR_FMT
  2578. "%s: smp passthru reply failed to be returned\n",
  2579. ioc->name, __func__);
  2580. ret = -ENXIO;
  2581. }
  2582. out_free:
  2583. if (data_out_dma)
  2584. pci_free_consistent(ioc->pcidev, sz, data_out, data_out_dma);
  2585. put_mf:
  2586. if (mf)
  2587. mpt_free_msg_frame(ioc, mf);
  2588. out_unlock:
  2589. CLEAR_MGMT_STATUS(ioc->sas_mgmt.status)
  2590. mutex_unlock(&ioc->sas_mgmt.mutex);
  2591. out:
  2592. return ret;
  2593. }
  2594. static void
  2595. mptsas_parse_device_info(struct sas_identify *identify,
  2596. struct mptsas_devinfo *device_info)
  2597. {
  2598. u16 protocols;
  2599. identify->sas_address = device_info->sas_address;
  2600. identify->phy_identifier = device_info->phy_id;
  2601. /*
  2602. * Fill in Phy Initiator Port Protocol.
  2603. * Bits 6:3, more than one bit can be set, fall through cases.
  2604. */
  2605. protocols = device_info->device_info & 0x78;
  2606. identify->initiator_port_protocols = 0;
  2607. if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
  2608. identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
  2609. if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
  2610. identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
  2611. if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
  2612. identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
  2613. if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
  2614. identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
  2615. /*
  2616. * Fill in Phy Target Port Protocol.
  2617. * Bits 10:7, more than one bit can be set, fall through cases.
  2618. */
  2619. protocols = device_info->device_info & 0x780;
  2620. identify->target_port_protocols = 0;
  2621. if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
  2622. identify->target_port_protocols |= SAS_PROTOCOL_SSP;
  2623. if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
  2624. identify->target_port_protocols |= SAS_PROTOCOL_STP;
  2625. if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
  2626. identify->target_port_protocols |= SAS_PROTOCOL_SMP;
  2627. if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  2628. identify->target_port_protocols |= SAS_PROTOCOL_SATA;
  2629. /*
  2630. * Fill in Attached device type.
  2631. */
  2632. switch (device_info->device_info &
  2633. MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
  2634. case MPI_SAS_DEVICE_INFO_NO_DEVICE:
  2635. identify->device_type = SAS_PHY_UNUSED;
  2636. break;
  2637. case MPI_SAS_DEVICE_INFO_END_DEVICE:
  2638. identify->device_type = SAS_END_DEVICE;
  2639. break;
  2640. case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
  2641. identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
  2642. break;
  2643. case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
  2644. identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
  2645. break;
  2646. }
  2647. }
  2648. static int mptsas_probe_one_phy(struct device *dev,
  2649. struct mptsas_phyinfo *phy_info, int index, int local)
  2650. {
  2651. MPT_ADAPTER *ioc;
  2652. struct sas_phy *phy;
  2653. struct sas_port *port;
  2654. int error = 0;
  2655. VirtTarget *vtarget;
  2656. if (!dev) {
  2657. error = -ENODEV;
  2658. goto out;
  2659. }
  2660. if (!phy_info->phy) {
  2661. phy = sas_phy_alloc(dev, index);
  2662. if (!phy) {
  2663. error = -ENOMEM;
  2664. goto out;
  2665. }
  2666. } else
  2667. phy = phy_info->phy;
  2668. mptsas_parse_device_info(&phy->identify, &phy_info->identify);
  2669. /*
  2670. * Set Negotiated link rate.
  2671. */
  2672. switch (phy_info->negotiated_link_rate) {
  2673. case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
  2674. phy->negotiated_linkrate = SAS_PHY_DISABLED;
  2675. break;
  2676. case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
  2677. phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
  2678. break;
  2679. case MPI_SAS_IOUNIT0_RATE_1_5:
  2680. phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
  2681. break;
  2682. case MPI_SAS_IOUNIT0_RATE_3_0:
  2683. phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
  2684. break;
  2685. case MPI_SAS_IOUNIT0_RATE_6_0:
  2686. phy->negotiated_linkrate = SAS_LINK_RATE_6_0_GBPS;
  2687. break;
  2688. case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
  2689. case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
  2690. default:
  2691. phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
  2692. break;
  2693. }
  2694. /*
  2695. * Set Max hardware link rate.
  2696. */
  2697. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  2698. case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
  2699. phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  2700. break;
  2701. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  2702. phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  2703. break;
  2704. default:
  2705. break;
  2706. }
  2707. /*
  2708. * Set Max programmed link rate.
  2709. */
  2710. switch (phy_info->programmed_link_rate &
  2711. MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  2712. case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
  2713. phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  2714. break;
  2715. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  2716. phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  2717. break;
  2718. default:
  2719. break;
  2720. }
  2721. /*
  2722. * Set Min hardware link rate.
  2723. */
  2724. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
  2725. case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
  2726. phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  2727. break;
  2728. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  2729. phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  2730. break;
  2731. default:
  2732. break;
  2733. }
  2734. /*
  2735. * Set Min programmed link rate.
  2736. */
  2737. switch (phy_info->programmed_link_rate &
  2738. MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
  2739. case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
  2740. phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  2741. break;
  2742. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  2743. phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  2744. break;
  2745. default:
  2746. break;
  2747. }
  2748. if (!phy_info->phy) {
  2749. error = sas_phy_add(phy);
  2750. if (error) {
  2751. sas_phy_free(phy);
  2752. goto out;
  2753. }
  2754. phy_info->phy = phy;
  2755. }
  2756. if (!phy_info->attached.handle ||
  2757. !phy_info->port_details)
  2758. goto out;
  2759. port = mptsas_get_port(phy_info);
  2760. ioc = phy_to_ioc(phy_info->phy);
  2761. if (phy_info->sas_port_add_phy) {
  2762. if (!port) {
  2763. port = sas_port_alloc_num(dev);
  2764. if (!port) {
  2765. error = -ENOMEM;
  2766. goto out;
  2767. }
  2768. error = sas_port_add(port);
  2769. if (error) {
  2770. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2771. "%s: exit at line=%d\n", ioc->name,
  2772. __func__, __LINE__));
  2773. goto out;
  2774. }
  2775. mptsas_set_port(ioc, phy_info, port);
  2776. devtprintk(ioc, dev_printk(KERN_DEBUG, &port->dev,
  2777. MYIOC_s_FMT "add port %d, sas_addr (0x%llx)\n",
  2778. ioc->name, port->port_identifier,
  2779. (unsigned long long)phy_info->
  2780. attached.sas_address));
  2781. }
  2782. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2783. "sas_port_add_phy: phy_id=%d\n",
  2784. ioc->name, phy_info->phy_id));
  2785. sas_port_add_phy(port, phy_info->phy);
  2786. phy_info->sas_port_add_phy = 0;
  2787. devtprintk(ioc, dev_printk(KERN_DEBUG, &phy_info->phy->dev,
  2788. MYIOC_s_FMT "add phy %d, phy-obj (0x%p)\n", ioc->name,
  2789. phy_info->phy_id, phy_info->phy));
  2790. }
  2791. if (!mptsas_get_rphy(phy_info) && port && !port->rphy) {
  2792. struct sas_rphy *rphy;
  2793. struct device *parent;
  2794. struct sas_identify identify;
  2795. parent = dev->parent->parent;
  2796. /*
  2797. * Let the hotplug_work thread handle processing
  2798. * the adding/removing of devices that occur
  2799. * after start of day.
  2800. */
  2801. if (mptsas_is_end_device(&phy_info->attached) &&
  2802. phy_info->attached.handle_parent) {
  2803. goto out;
  2804. }
  2805. mptsas_parse_device_info(&identify, &phy_info->attached);
  2806. if (scsi_is_host_device(parent)) {
  2807. struct mptsas_portinfo *port_info;
  2808. int i;
  2809. port_info = ioc->hba_port_info;
  2810. for (i = 0; i < port_info->num_phys; i++)
  2811. if (port_info->phy_info[i].identify.sas_address ==
  2812. identify.sas_address) {
  2813. sas_port_mark_backlink(port);
  2814. goto out;
  2815. }
  2816. } else if (scsi_is_sas_rphy(parent)) {
  2817. struct sas_rphy *parent_rphy = dev_to_rphy(parent);
  2818. if (identify.sas_address ==
  2819. parent_rphy->identify.sas_address) {
  2820. sas_port_mark_backlink(port);
  2821. goto out;
  2822. }
  2823. }
  2824. switch (identify.device_type) {
  2825. case SAS_END_DEVICE:
  2826. rphy = sas_end_device_alloc(port);
  2827. break;
  2828. case SAS_EDGE_EXPANDER_DEVICE:
  2829. case SAS_FANOUT_EXPANDER_DEVICE:
  2830. rphy = sas_expander_alloc(port, identify.device_type);
  2831. break;
  2832. default:
  2833. rphy = NULL;
  2834. break;
  2835. }
  2836. if (!rphy) {
  2837. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2838. "%s: exit at line=%d\n", ioc->name,
  2839. __func__, __LINE__));
  2840. goto out;
  2841. }
  2842. rphy->identify = identify;
  2843. error = sas_rphy_add(rphy);
  2844. if (error) {
  2845. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2846. "%s: exit at line=%d\n", ioc->name,
  2847. __func__, __LINE__));
  2848. sas_rphy_free(rphy);
  2849. goto out;
  2850. }
  2851. mptsas_set_rphy(ioc, phy_info, rphy);
  2852. if (identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
  2853. identify.device_type == SAS_FANOUT_EXPANDER_DEVICE)
  2854. mptsas_exp_repmanufacture_info(ioc,
  2855. identify.sas_address,
  2856. rphy_to_expander_device(rphy));
  2857. }
  2858. /* If the device exists,verify it wasn't previously flagged
  2859. as a missing device. If so, clear it */
  2860. vtarget = mptsas_find_vtarget(ioc,
  2861. phy_info->attached.channel,
  2862. phy_info->attached.id);
  2863. if (vtarget && vtarget->inDMD) {
  2864. printk(KERN_INFO "Device returned, unsetting inDMD\n");
  2865. vtarget->inDMD = 0;
  2866. }
  2867. out:
  2868. return error;
  2869. }
  2870. static int
  2871. mptsas_probe_hba_phys(MPT_ADAPTER *ioc)
  2872. {
  2873. struct mptsas_portinfo *port_info, *hba;
  2874. int error = -ENOMEM, i;
  2875. hba = kzalloc(sizeof(struct mptsas_portinfo), GFP_KERNEL);
  2876. if (! hba)
  2877. goto out;
  2878. error = mptsas_sas_io_unit_pg0(ioc, hba);
  2879. if (error)
  2880. goto out_free_port_info;
  2881. mptsas_sas_io_unit_pg1(ioc);
  2882. mutex_lock(&ioc->sas_topology_mutex);
  2883. port_info = ioc->hba_port_info;
  2884. if (!port_info) {
  2885. ioc->hba_port_info = port_info = hba;
  2886. ioc->hba_port_num_phy = port_info->num_phys;
  2887. list_add_tail(&port_info->list, &ioc->sas_topology);
  2888. } else {
  2889. for (i = 0; i < hba->num_phys; i++) {
  2890. port_info->phy_info[i].negotiated_link_rate =
  2891. hba->phy_info[i].negotiated_link_rate;
  2892. port_info->phy_info[i].handle =
  2893. hba->phy_info[i].handle;
  2894. port_info->phy_info[i].port_id =
  2895. hba->phy_info[i].port_id;
  2896. }
  2897. kfree(hba->phy_info);
  2898. kfree(hba);
  2899. hba = NULL;
  2900. }
  2901. mutex_unlock(&ioc->sas_topology_mutex);
  2902. #if defined(CPQ_CIM)
  2903. ioc->num_ports = port_info->num_phys;
  2904. #endif
  2905. for (i = 0; i < port_info->num_phys; i++) {
  2906. mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
  2907. (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
  2908. MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
  2909. port_info->phy_info[i].identify.handle =
  2910. port_info->phy_info[i].handle;
  2911. mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
  2912. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  2913. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2914. port_info->phy_info[i].identify.handle);
  2915. if (!ioc->hba_port_sas_addr)
  2916. ioc->hba_port_sas_addr =
  2917. port_info->phy_info[i].identify.sas_address;
  2918. port_info->phy_info[i].identify.phy_id =
  2919. port_info->phy_info[i].phy_id = i;
  2920. if (port_info->phy_info[i].attached.handle)
  2921. mptsas_sas_device_pg0(ioc,
  2922. &port_info->phy_info[i].attached,
  2923. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  2924. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2925. port_info->phy_info[i].attached.handle);
  2926. }
  2927. mptsas_setup_wide_ports(ioc, port_info);
  2928. for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
  2929. mptsas_probe_one_phy(&ioc->sh->shost_gendev,
  2930. &port_info->phy_info[i], ioc->sas_index, 1);
  2931. return 0;
  2932. out_free_port_info:
  2933. kfree(hba);
  2934. out:
  2935. return error;
  2936. }
  2937. static void
  2938. mptsas_expander_refresh(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  2939. {
  2940. struct mptsas_portinfo *parent;
  2941. struct device *parent_dev;
  2942. struct sas_rphy *rphy;
  2943. int i;
  2944. u64 sas_address; /* expander sas address */
  2945. u32 handle;
  2946. handle = port_info->phy_info[0].handle;
  2947. sas_address = port_info->phy_info[0].identify.sas_address;
  2948. for (i = 0; i < port_info->num_phys; i++) {
  2949. mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
  2950. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
  2951. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + handle);
  2952. mptsas_sas_device_pg0(ioc,
  2953. &port_info->phy_info[i].identify,
  2954. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  2955. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2956. port_info->phy_info[i].identify.handle);
  2957. port_info->phy_info[i].identify.phy_id =
  2958. port_info->phy_info[i].phy_id;
  2959. if (port_info->phy_info[i].attached.handle) {
  2960. mptsas_sas_device_pg0(ioc,
  2961. &port_info->phy_info[i].attached,
  2962. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  2963. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2964. port_info->phy_info[i].attached.handle);
  2965. port_info->phy_info[i].attached.phy_id =
  2966. port_info->phy_info[i].phy_id;
  2967. }
  2968. }
  2969. mutex_lock(&ioc->sas_topology_mutex);
  2970. parent = mptsas_find_portinfo_by_handle(ioc,
  2971. port_info->phy_info[0].identify.handle_parent);
  2972. if (!parent) {
  2973. mutex_unlock(&ioc->sas_topology_mutex);
  2974. return;
  2975. }
  2976. for (i = 0, parent_dev = NULL; i < parent->num_phys && !parent_dev;
  2977. i++) {
  2978. if (parent->phy_info[i].attached.sas_address == sas_address) {
  2979. rphy = mptsas_get_rphy(&parent->phy_info[i]);
  2980. parent_dev = &rphy->dev;
  2981. }
  2982. }
  2983. mutex_unlock(&ioc->sas_topology_mutex);
  2984. mptsas_setup_wide_ports(ioc, port_info);
  2985. for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
  2986. mptsas_probe_one_phy(parent_dev, &port_info->phy_info[i],
  2987. ioc->sas_index, 0);
  2988. }
  2989. static void
  2990. mptsas_expander_event_add(MPT_ADAPTER *ioc,
  2991. MpiEventDataSasExpanderStatusChange_t *expander_data)
  2992. {
  2993. struct mptsas_portinfo *port_info;
  2994. int i;
  2995. __le64 sas_address;
  2996. port_info = kzalloc(sizeof(struct mptsas_portinfo), GFP_KERNEL);
  2997. if (!port_info)
  2998. BUG();
  2999. port_info->num_phys = (expander_data->NumPhys) ?
  3000. expander_data->NumPhys : 1;
  3001. port_info->phy_info = kcalloc(port_info->num_phys,
  3002. sizeof(struct mptsas_phyinfo), GFP_KERNEL);
  3003. if (!port_info->phy_info)
  3004. BUG();
  3005. memcpy(&sas_address, &expander_data->SASAddress, sizeof(__le64));
  3006. for (i = 0; i < port_info->num_phys; i++) {
  3007. port_info->phy_info[i].portinfo = port_info;
  3008. port_info->phy_info[i].handle =
  3009. le16_to_cpu(expander_data->DevHandle);
  3010. port_info->phy_info[i].identify.sas_address =
  3011. le64_to_cpu(sas_address);
  3012. port_info->phy_info[i].identify.handle_parent =
  3013. le16_to_cpu(expander_data->ParentDevHandle);
  3014. }
  3015. mutex_lock(&ioc->sas_topology_mutex);
  3016. list_add_tail(&port_info->list, &ioc->sas_topology);
  3017. mutex_unlock(&ioc->sas_topology_mutex);
  3018. printk(MYIOC_s_INFO_FMT "add expander: num_phys %d, "
  3019. "sas_addr (0x%llx)\n", ioc->name, port_info->num_phys,
  3020. (unsigned long long)sas_address);
  3021. mptsas_expander_refresh(ioc, port_info);
  3022. }
  3023. /**
  3024. * mptsas_delete_expander_siblings - remove siblings attached to expander
  3025. * @ioc: Pointer to MPT_ADAPTER structure
  3026. * @parent: the parent port_info object
  3027. * @expander: the expander port_info object
  3028. **/
  3029. static void
  3030. mptsas_delete_expander_siblings(MPT_ADAPTER *ioc, struct mptsas_portinfo
  3031. *parent, struct mptsas_portinfo *expander)
  3032. {
  3033. struct mptsas_phyinfo *phy_info;
  3034. struct mptsas_portinfo *port_info;
  3035. struct sas_rphy *rphy;
  3036. int i;
  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 == SAS_END_DEVICE)
  3043. mptsas_del_end_device(ioc, phy_info);
  3044. }
  3045. phy_info = expander->phy_info;
  3046. for (i = 0; i < expander->num_phys; i++, phy_info++) {
  3047. rphy = mptsas_get_rphy(phy_info);
  3048. if (!rphy)
  3049. continue;
  3050. if (rphy->identify.device_type ==
  3051. MPI_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  3052. rphy->identify.device_type ==
  3053. MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
  3054. port_info = mptsas_find_portinfo_by_sas_address(ioc,
  3055. rphy->identify.sas_address);
  3056. if (!port_info)
  3057. continue;
  3058. if (port_info == parent) /* backlink rphy */
  3059. continue;
  3060. /*
  3061. Delete this expander even if the expdevpage is exists
  3062. because the parent expander is already deleted
  3063. */
  3064. mptsas_expander_delete(ioc, port_info, 1);
  3065. }
  3066. }
  3067. }
  3068. /**
  3069. * mptsas_expander_delete - remove this expander
  3070. * @ioc: Pointer to MPT_ADAPTER structure
  3071. * @port_info: expander port_info struct
  3072. * @force: Flag to forcefully delete the expander
  3073. *
  3074. **/
  3075. static void mptsas_expander_delete(MPT_ADAPTER *ioc,
  3076. struct mptsas_portinfo *port_info, u8 force)
  3077. {
  3078. struct mptsas_portinfo *parent;
  3079. int i;
  3080. u64 expander_sas_address;
  3081. struct mptsas_phyinfo *phy_info;
  3082. struct mptsas_portinfo buffer;
  3083. struct mptsas_portinfo_details *port_details;
  3084. struct sas_port *port;
  3085. if (!port_info)
  3086. return;
  3087. /* see if expander is still there before deleting */
  3088. mptsas_sas_expander_pg0(ioc, &buffer,
  3089. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
  3090. MPI_SAS_EXPAND_PGAD_FORM_SHIFT),
  3091. port_info->phy_info[0].identify.handle);
  3092. if (buffer.num_phys) {
  3093. kfree(buffer.phy_info);
  3094. if (!force)
  3095. return;
  3096. }
  3097. /*
  3098. * Obtain the port_info instance to the parent port
  3099. */
  3100. port_details = NULL;
  3101. expander_sas_address =
  3102. port_info->phy_info[0].identify.sas_address;
  3103. parent = mptsas_find_portinfo_by_handle(ioc,
  3104. port_info->phy_info[0].identify.handle_parent);
  3105. mptsas_delete_expander_siblings(ioc, parent, port_info);
  3106. if (!parent)
  3107. goto out;
  3108. /*
  3109. * Delete rphys in the parent that point
  3110. * to this expander.
  3111. */
  3112. phy_info = parent->phy_info;
  3113. port = NULL;
  3114. for (i = 0; i < parent->num_phys; i++, phy_info++) {
  3115. if (!phy_info->phy)
  3116. continue;
  3117. if (phy_info->attached.sas_address !=
  3118. expander_sas_address)
  3119. continue;
  3120. if (!port) {
  3121. port = mptsas_get_port(phy_info);
  3122. port_details = phy_info->port_details;
  3123. }
  3124. dev_printk(KERN_DEBUG, &phy_info->phy->dev,
  3125. MYIOC_s_FMT "delete phy %d, phy-obj (0x%p)\n", ioc->name,
  3126. phy_info->phy_id, phy_info->phy);
  3127. sas_port_delete_phy(port, phy_info->phy);
  3128. }
  3129. if (port) {
  3130. dev_printk(KERN_DEBUG, &port->dev,
  3131. MYIOC_s_FMT "delete port %d, sas_addr (0x%llx)\n",
  3132. ioc->name, port->port_identifier,
  3133. (unsigned long long)expander_sas_address);
  3134. sas_port_delete(port);
  3135. mptsas_port_delete(ioc, port_details);
  3136. }
  3137. out:
  3138. printk(MYIOC_s_INFO_FMT "delete expander: num_phys %d, "
  3139. "sas_addr (0x%llx)\n", ioc->name, port_info->num_phys,
  3140. (unsigned long long)expander_sas_address);
  3141. /*
  3142. * free link
  3143. */
  3144. list_del(&port_info->list);
  3145. kfree(port_info->phy_info);
  3146. kfree(port_info);
  3147. }
  3148. /**
  3149. * mptsas_send_expander_event - expanders events
  3150. * @ioc: Pointer to MPT_ADAPTER structure
  3151. * @expander_data: event data
  3152. *
  3153. *
  3154. * This function handles adding, removing, and refreshing
  3155. * device handles within the expander objects.
  3156. */
  3157. static void
  3158. mptsas_send_expander_event(struct fw_event_work *fw_event)
  3159. {
  3160. MPT_ADAPTER *ioc;
  3161. MpiEventDataSasExpanderStatusChange_t *expander_data;
  3162. struct mptsas_portinfo *port_info;
  3163. __le64 sas_address;
  3164. int i;
  3165. ioc = fw_event->ioc;
  3166. expander_data = (MpiEventDataSasExpanderStatusChange_t *)
  3167. fw_event->event_data;
  3168. memcpy(&sas_address, &expander_data->SASAddress, sizeof(__le64));
  3169. sas_address = le64_to_cpu(sas_address);
  3170. port_info = mptsas_find_portinfo_by_sas_address(ioc, sas_address);
  3171. if (expander_data->ReasonCode == MPI_EVENT_SAS_EXP_RC_ADDED) {
  3172. if (port_info) {
  3173. for (i = 0; i < port_info->num_phys; i++) {
  3174. port_info->phy_info[i].portinfo = port_info;
  3175. port_info->phy_info[i].handle =
  3176. le16_to_cpu(expander_data->DevHandle);
  3177. port_info->phy_info[i].identify.sas_address =
  3178. le64_to_cpu(sas_address);
  3179. port_info->phy_info[i].identify.handle_parent =
  3180. le16_to_cpu(expander_data->ParentDevHandle);
  3181. }
  3182. mptsas_expander_refresh(ioc, port_info);
  3183. } else if (!port_info && expander_data->NumPhys)
  3184. mptsas_expander_event_add(ioc, expander_data);
  3185. } else if (expander_data->ReasonCode ==
  3186. MPI_EVENT_SAS_EXP_RC_NOT_RESPONDING)
  3187. mptsas_expander_delete(ioc, port_info, 0);
  3188. mptsas_free_fw_event(ioc, fw_event);
  3189. }
  3190. /**
  3191. * mptsas_expander_add -
  3192. * @ioc: Pointer to MPT_ADAPTER structure
  3193. * @handle:
  3194. *
  3195. */
  3196. static struct mptsas_portinfo *
  3197. mptsas_expander_add(MPT_ADAPTER *ioc, u16 handle)
  3198. {
  3199. struct mptsas_portinfo buffer, *port_info;
  3200. int i;
  3201. if ((mptsas_sas_expander_pg0(ioc, &buffer,
  3202. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
  3203. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), handle)))
  3204. return NULL;
  3205. port_info = kzalloc(sizeof(struct mptsas_portinfo), GFP_ATOMIC);
  3206. if (!port_info) {
  3207. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3208. "%s: exit at line=%d\n", ioc->name,
  3209. __func__, __LINE__));
  3210. return NULL;
  3211. }
  3212. port_info->num_phys = buffer.num_phys;
  3213. port_info->phy_info = buffer.phy_info;
  3214. for (i = 0; i < port_info->num_phys; i++)
  3215. port_info->phy_info[i].portinfo = port_info;
  3216. mutex_lock(&ioc->sas_topology_mutex);
  3217. list_add_tail(&port_info->list, &ioc->sas_topology);
  3218. mutex_unlock(&ioc->sas_topology_mutex);
  3219. printk(MYIOC_s_INFO_FMT "add expander: num_phys %d, "
  3220. "sas_addr (0x%llx)\n", ioc->name, port_info->num_phys,
  3221. (unsigned long long)buffer.phy_info[0].identify.sas_address);
  3222. mptsas_expander_refresh(ioc, port_info);
  3223. return port_info;
  3224. }
  3225. static void
  3226. mptsas_send_link_status_event(struct fw_event_work *fw_event)
  3227. {
  3228. MPT_ADAPTER *ioc;
  3229. MpiEventDataSasPhyLinkStatus_t *link_data;
  3230. struct mptsas_portinfo *port_info;
  3231. struct mptsas_phyinfo *phy_info = NULL;
  3232. __le64 sas_address;
  3233. u8 phy_num;
  3234. u8 link_rate;
  3235. ioc = fw_event->ioc;
  3236. link_data = (MpiEventDataSasPhyLinkStatus_t *)fw_event->event_data;
  3237. memcpy(&sas_address, &link_data->SASAddress, sizeof(__le64));
  3238. sas_address = le64_to_cpu(sas_address);
  3239. link_rate = link_data->LinkRates >> 4;
  3240. phy_num = link_data->PhyNum;
  3241. port_info = mptsas_find_portinfo_by_sas_address(ioc, sas_address);
  3242. if (port_info) {
  3243. phy_info = &port_info->phy_info[phy_num];
  3244. if (phy_info)
  3245. phy_info->negotiated_link_rate = link_rate;
  3246. }
  3247. if (link_rate == MPI_SAS_IOUNIT0_RATE_1_5 ||
  3248. link_rate == MPI_SAS_IOUNIT0_RATE_3_0 ||
  3249. link_rate == MPI_SAS_IOUNIT0_RATE_6_0) {
  3250. if (!port_info) {
  3251. if (ioc->old_sas_discovery_protocal) {
  3252. port_info = mptsas_expander_add(ioc,
  3253. le16_to_cpu(link_data->DevHandle));
  3254. if (port_info)
  3255. goto out;
  3256. }
  3257. goto out;
  3258. }
  3259. if (port_info == ioc->hba_port_info)
  3260. mptsas_probe_hba_phys(ioc);
  3261. else
  3262. mptsas_expander_refresh(ioc, port_info);
  3263. } else if (phy_info && phy_info->phy) {
  3264. if (link_rate == MPI_SAS_IOUNIT0_RATE_PHY_DISABLED)
  3265. phy_info->phy->negotiated_linkrate =
  3266. SAS_PHY_DISABLED;
  3267. else if (link_rate ==
  3268. MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION)
  3269. phy_info->phy->negotiated_linkrate =
  3270. SAS_LINK_RATE_FAILED;
  3271. else {
  3272. phy_info->phy->negotiated_linkrate =
  3273. SAS_LINK_RATE_UNKNOWN;
  3274. if (ioc->device_missing_delay &&
  3275. mptsas_is_end_device(&phy_info->attached)) {
  3276. struct scsi_device *sdev;
  3277. VirtDevice *vdevice;
  3278. u8 channel, id;
  3279. id = phy_info->attached.id;
  3280. channel = phy_info->attached.channel;
  3281. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  3282. "Link down for fw_id %d:fw_channel %d\n",
  3283. ioc->name, phy_info->attached.id,
  3284. phy_info->attached.channel));
  3285. shost_for_each_device(sdev, ioc->sh) {
  3286. vdevice = sdev->hostdata;
  3287. if ((vdevice == NULL) ||
  3288. (vdevice->vtarget == NULL))
  3289. continue;
  3290. if ((vdevice->vtarget->tflags &
  3291. MPT_TARGET_FLAGS_RAID_COMPONENT ||
  3292. vdevice->vtarget->raidVolume))
  3293. continue;
  3294. if (vdevice->vtarget->id == id &&
  3295. vdevice->vtarget->channel ==
  3296. channel)
  3297. devtprintk(ioc,
  3298. printk(MYIOC_s_DEBUG_FMT
  3299. "SDEV OUTSTANDING CMDS"
  3300. "%d\n", ioc->name,
  3301. atomic_read(&sdev->device_busy)));
  3302. }
  3303. }
  3304. }
  3305. }
  3306. out:
  3307. mptsas_free_fw_event(ioc, fw_event);
  3308. }
  3309. static void
  3310. mptsas_not_responding_devices(MPT_ADAPTER *ioc)
  3311. {
  3312. struct mptsas_portinfo buffer, *port_info;
  3313. struct mptsas_device_info *sas_info;
  3314. struct mptsas_devinfo sas_device;
  3315. u32 handle;
  3316. VirtTarget *vtarget = NULL;
  3317. struct mptsas_phyinfo *phy_info;
  3318. u8 found_expander;
  3319. int retval, retry_count;
  3320. unsigned long flags;
  3321. mpt_findImVolumes(ioc);
  3322. spin_lock_irqsave(&ioc->taskmgmt_lock, flags);
  3323. if (ioc->ioc_reset_in_progress) {
  3324. dfailprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  3325. "%s: exiting due to a parallel reset \n", ioc->name,
  3326. __func__));
  3327. spin_unlock_irqrestore(&ioc->taskmgmt_lock, flags);
  3328. return;
  3329. }
  3330. spin_unlock_irqrestore(&ioc->taskmgmt_lock, flags);
  3331. /* devices, logical volumes */
  3332. mutex_lock(&ioc->sas_device_info_mutex);
  3333. redo_device_scan:
  3334. list_for_each_entry(sas_info, &ioc->sas_device_info_list, list) {
  3335. if (sas_info->is_cached)
  3336. continue;
  3337. if (!sas_info->is_logical_volume) {
  3338. sas_device.handle = 0;
  3339. retry_count = 0;
  3340. retry_page:
  3341. retval = mptsas_sas_device_pg0(ioc, &sas_device,
  3342. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID
  3343. << MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  3344. (sas_info->fw.channel << 8) +
  3345. sas_info->fw.id);
  3346. if (sas_device.handle)
  3347. continue;
  3348. if (retval == -EBUSY) {
  3349. spin_lock_irqsave(&ioc->taskmgmt_lock, flags);
  3350. if (ioc->ioc_reset_in_progress) {
  3351. dfailprintk(ioc,
  3352. printk(MYIOC_s_DEBUG_FMT
  3353. "%s: exiting due to reset\n",
  3354. ioc->name, __func__));
  3355. spin_unlock_irqrestore
  3356. (&ioc->taskmgmt_lock, flags);
  3357. mutex_unlock(&ioc->
  3358. sas_device_info_mutex);
  3359. return;
  3360. }
  3361. spin_unlock_irqrestore(&ioc->taskmgmt_lock,
  3362. flags);
  3363. }
  3364. if (retval && (retval != -ENODEV)) {
  3365. if (retry_count < 10) {
  3366. retry_count++;
  3367. goto retry_page;
  3368. } else {
  3369. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  3370. "%s: Config page retry exceeded retry "
  3371. "count deleting device 0x%llx\n",
  3372. ioc->name, __func__,
  3373. sas_info->sas_address));
  3374. }
  3375. }
  3376. /* delete device */
  3377. vtarget = mptsas_find_vtarget(ioc,
  3378. sas_info->fw.channel, sas_info->fw.id);
  3379. if (vtarget)
  3380. vtarget->deleted = 1;
  3381. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  3382. sas_info->sas_address);
  3383. mptsas_del_end_device(ioc, phy_info);
  3384. goto redo_device_scan;
  3385. } else
  3386. mptsas_volume_delete(ioc, sas_info->fw.id);
  3387. }
  3388. mutex_unlock(&ioc->sas_device_info_mutex);
  3389. /* expanders */
  3390. mutex_lock(&ioc->sas_topology_mutex);
  3391. redo_expander_scan:
  3392. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  3393. if (!(port_info->phy_info[0].identify.device_info &
  3394. MPI_SAS_DEVICE_INFO_SMP_TARGET))
  3395. continue;
  3396. found_expander = 0;
  3397. handle = 0xFFFF;
  3398. while (!mptsas_sas_expander_pg0(ioc, &buffer,
  3399. (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
  3400. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), handle) &&
  3401. !found_expander) {
  3402. handle = buffer.phy_info[0].handle;
  3403. if (buffer.phy_info[0].identify.sas_address ==
  3404. port_info->phy_info[0].identify.sas_address) {
  3405. found_expander = 1;
  3406. }
  3407. kfree(buffer.phy_info);
  3408. }
  3409. if (!found_expander) {
  3410. mptsas_expander_delete(ioc, port_info, 0);
  3411. goto redo_expander_scan;
  3412. }
  3413. }
  3414. mutex_unlock(&ioc->sas_topology_mutex);
  3415. }
  3416. /**
  3417. * mptsas_probe_expanders - adding expanders
  3418. * @ioc: Pointer to MPT_ADAPTER structure
  3419. *
  3420. **/
  3421. static void
  3422. mptsas_probe_expanders(MPT_ADAPTER *ioc)
  3423. {
  3424. struct mptsas_portinfo buffer, *port_info;
  3425. u32 handle;
  3426. int i;
  3427. handle = 0xFFFF;
  3428. while (!mptsas_sas_expander_pg0(ioc, &buffer,
  3429. (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
  3430. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), handle)) {
  3431. handle = buffer.phy_info[0].handle;
  3432. port_info = mptsas_find_portinfo_by_sas_address(ioc,
  3433. buffer.phy_info[0].identify.sas_address);
  3434. if (port_info) {
  3435. /* refreshing handles */
  3436. for (i = 0; i < buffer.num_phys; i++) {
  3437. port_info->phy_info[i].handle = handle;
  3438. port_info->phy_info[i].identify.handle_parent =
  3439. buffer.phy_info[0].identify.handle_parent;
  3440. }
  3441. mptsas_expander_refresh(ioc, port_info);
  3442. kfree(buffer.phy_info);
  3443. continue;
  3444. }
  3445. port_info = kzalloc(sizeof(struct mptsas_portinfo), GFP_KERNEL);
  3446. if (!port_info) {
  3447. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3448. "%s: exit at line=%d\n", ioc->name,
  3449. __func__, __LINE__));
  3450. return;
  3451. }
  3452. port_info->num_phys = buffer.num_phys;
  3453. port_info->phy_info = buffer.phy_info;
  3454. for (i = 0; i < port_info->num_phys; i++)
  3455. port_info->phy_info[i].portinfo = port_info;
  3456. mutex_lock(&ioc->sas_topology_mutex);
  3457. list_add_tail(&port_info->list, &ioc->sas_topology);
  3458. mutex_unlock(&ioc->sas_topology_mutex);
  3459. printk(MYIOC_s_INFO_FMT "add expander: num_phys %d, "
  3460. "sas_addr (0x%llx)\n", ioc->name, port_info->num_phys,
  3461. (unsigned long long)buffer.phy_info[0].identify.sas_address);
  3462. mptsas_expander_refresh(ioc, port_info);
  3463. }
  3464. }
  3465. static void
  3466. mptsas_probe_devices(MPT_ADAPTER *ioc)
  3467. {
  3468. u16 handle;
  3469. struct mptsas_devinfo sas_device;
  3470. struct mptsas_phyinfo *phy_info;
  3471. handle = 0xFFFF;
  3472. while (!(mptsas_sas_device_pg0(ioc, &sas_device,
  3473. MPI_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  3474. handle = sas_device.handle;
  3475. if ((sas_device.device_info &
  3476. (MPI_SAS_DEVICE_INFO_SSP_TARGET |
  3477. MPI_SAS_DEVICE_INFO_STP_TARGET |
  3478. MPI_SAS_DEVICE_INFO_SATA_DEVICE)) == 0)
  3479. continue;
  3480. /* If there is no FW B_T mapping for this device then continue
  3481. * */
  3482. if (!(sas_device.flags & MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)
  3483. || !(sas_device.flags &
  3484. MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED))
  3485. continue;
  3486. phy_info = mptsas_refreshing_device_handles(ioc, &sas_device);
  3487. if (!phy_info)
  3488. continue;
  3489. if (mptsas_get_rphy(phy_info))
  3490. continue;
  3491. mptsas_add_end_device(ioc, phy_info);
  3492. }
  3493. }
  3494. /**
  3495. * mptsas_scan_sas_topology -
  3496. * @ioc: Pointer to MPT_ADAPTER structure
  3497. * @sas_address:
  3498. *
  3499. **/
  3500. static void
  3501. mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
  3502. {
  3503. struct scsi_device *sdev;
  3504. int i;
  3505. mptsas_probe_hba_phys(ioc);
  3506. mptsas_probe_expanders(ioc);
  3507. mptsas_probe_devices(ioc);
  3508. /*
  3509. Reporting RAID volumes.
  3510. */
  3511. if (!ioc->ir_firmware || !ioc->raid_data.pIocPg2 ||
  3512. !ioc->raid_data.pIocPg2->NumActiveVolumes)
  3513. return;
  3514. for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  3515. sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
  3516. ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
  3517. if (sdev) {
  3518. scsi_device_put(sdev);
  3519. continue;
  3520. }
  3521. printk(MYIOC_s_INFO_FMT "attaching raid volume, channel %d, "
  3522. "id %d\n", ioc->name, MPTSAS_RAID_CHANNEL,
  3523. ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID);
  3524. scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL,
  3525. ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
  3526. }
  3527. }
  3528. static void
  3529. mptsas_handle_queue_full_event(struct fw_event_work *fw_event)
  3530. {
  3531. MPT_ADAPTER *ioc;
  3532. EventDataQueueFull_t *qfull_data;
  3533. struct mptsas_device_info *sas_info;
  3534. struct scsi_device *sdev;
  3535. int depth;
  3536. int id = -1;
  3537. int channel = -1;
  3538. int fw_id, fw_channel;
  3539. u16 current_depth;
  3540. ioc = fw_event->ioc;
  3541. qfull_data = (EventDataQueueFull_t *)fw_event->event_data;
  3542. fw_id = qfull_data->TargetID;
  3543. fw_channel = qfull_data->Bus;
  3544. current_depth = le16_to_cpu(qfull_data->CurrentDepth);
  3545. /* if hidden raid component, look for the volume id */
  3546. mutex_lock(&ioc->sas_device_info_mutex);
  3547. if (mptscsih_is_phys_disk(ioc, fw_channel, fw_id)) {
  3548. list_for_each_entry(sas_info, &ioc->sas_device_info_list,
  3549. list) {
  3550. if (sas_info->is_cached ||
  3551. sas_info->is_logical_volume)
  3552. continue;
  3553. if (sas_info->is_hidden_raid_component &&
  3554. (sas_info->fw.channel == fw_channel &&
  3555. sas_info->fw.id == fw_id)) {
  3556. id = sas_info->volume_id;
  3557. channel = MPTSAS_RAID_CHANNEL;
  3558. goto out;
  3559. }
  3560. }
  3561. } else {
  3562. list_for_each_entry(sas_info, &ioc->sas_device_info_list,
  3563. list) {
  3564. if (sas_info->is_cached ||
  3565. sas_info->is_hidden_raid_component ||
  3566. sas_info->is_logical_volume)
  3567. continue;
  3568. if (sas_info->fw.channel == fw_channel &&
  3569. sas_info->fw.id == fw_id) {
  3570. id = sas_info->os.id;
  3571. channel = sas_info->os.channel;
  3572. goto out;
  3573. }
  3574. }
  3575. }
  3576. out:
  3577. mutex_unlock(&ioc->sas_device_info_mutex);
  3578. if (id != -1) {
  3579. shost_for_each_device(sdev, ioc->sh) {
  3580. if (sdev->id == id && sdev->channel == channel) {
  3581. if (current_depth > sdev->queue_depth) {
  3582. sdev_printk(KERN_INFO, sdev,
  3583. "strange observation, the queue "
  3584. "depth is (%d) meanwhile fw queue "
  3585. "depth (%d)\n", sdev->queue_depth,
  3586. current_depth);
  3587. continue;
  3588. }
  3589. depth = scsi_track_queue_full(sdev,
  3590. sdev->queue_depth - 1);
  3591. if (depth > 0)
  3592. sdev_printk(KERN_INFO, sdev,
  3593. "Queue depth reduced to (%d)\n",
  3594. depth);
  3595. else if (depth < 0)
  3596. sdev_printk(KERN_INFO, sdev,
  3597. "Tagged Command Queueing is being "
  3598. "disabled\n");
  3599. else if (depth == 0)
  3600. sdev_printk(KERN_DEBUG, sdev,
  3601. "Queue depth not changed yet\n");
  3602. }
  3603. }
  3604. }
  3605. mptsas_free_fw_event(ioc, fw_event);
  3606. }
  3607. static struct mptsas_phyinfo *
  3608. mptsas_find_phyinfo_by_sas_address(MPT_ADAPTER *ioc, u64 sas_address)
  3609. {
  3610. struct mptsas_portinfo *port_info;
  3611. struct mptsas_phyinfo *phy_info = NULL;
  3612. int i;
  3613. mutex_lock(&ioc->sas_topology_mutex);
  3614. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  3615. for (i = 0; i < port_info->num_phys; i++) {
  3616. if (!mptsas_is_end_device(
  3617. &port_info->phy_info[i].attached))
  3618. continue;
  3619. if (port_info->phy_info[i].attached.sas_address
  3620. != sas_address)
  3621. continue;
  3622. phy_info = &port_info->phy_info[i];
  3623. break;
  3624. }
  3625. }
  3626. mutex_unlock(&ioc->sas_topology_mutex);
  3627. return phy_info;
  3628. }
  3629. /**
  3630. * mptsas_find_phyinfo_by_phys_disk_num -
  3631. * @ioc: Pointer to MPT_ADAPTER structure
  3632. * @phys_disk_num:
  3633. * @channel:
  3634. * @id:
  3635. *
  3636. **/
  3637. static struct mptsas_phyinfo *
  3638. mptsas_find_phyinfo_by_phys_disk_num(MPT_ADAPTER *ioc, u8 phys_disk_num,
  3639. u8 channel, u8 id)
  3640. {
  3641. struct mptsas_phyinfo *phy_info = NULL;
  3642. struct mptsas_portinfo *port_info;
  3643. RaidPhysDiskPage1_t *phys_disk = NULL;
  3644. int num_paths;
  3645. u64 sas_address = 0;
  3646. int i;
  3647. phy_info = NULL;
  3648. if (!ioc->raid_data.pIocPg3)
  3649. return NULL;
  3650. /* dual port support */
  3651. num_paths = mpt_raid_phys_disk_get_num_paths(ioc, phys_disk_num);
  3652. if (!num_paths)
  3653. goto out;
  3654. phys_disk = kzalloc(offsetof(RaidPhysDiskPage1_t, Path) +
  3655. (num_paths * sizeof(RAID_PHYS_DISK1_PATH)), GFP_KERNEL);
  3656. if (!phys_disk)
  3657. goto out;
  3658. mpt_raid_phys_disk_pg1(ioc, phys_disk_num, phys_disk);
  3659. for (i = 0; i < num_paths; i++) {
  3660. if ((phys_disk->Path[i].Flags & 1) != 0)
  3661. /* entry no longer valid */
  3662. continue;
  3663. if ((id == phys_disk->Path[i].PhysDiskID) &&
  3664. (channel == phys_disk->Path[i].PhysDiskBus)) {
  3665. memcpy(&sas_address, &phys_disk->Path[i].WWID,
  3666. sizeof(u64));
  3667. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  3668. sas_address);
  3669. goto out;
  3670. }
  3671. }
  3672. out:
  3673. kfree(phys_disk);
  3674. if (phy_info)
  3675. return phy_info;
  3676. /*
  3677. * Extra code to handle RAID0 case, where the sas_address is not updated
  3678. * in phys_disk_page_1 when hotswapped
  3679. */
  3680. mutex_lock(&ioc->sas_topology_mutex);
  3681. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  3682. for (i = 0; i < port_info->num_phys && !phy_info; i++) {
  3683. if (!mptsas_is_end_device(
  3684. &port_info->phy_info[i].attached))
  3685. continue;
  3686. if (port_info->phy_info[i].attached.phys_disk_num == ~0)
  3687. continue;
  3688. if ((port_info->phy_info[i].attached.phys_disk_num ==
  3689. phys_disk_num) &&
  3690. (port_info->phy_info[i].attached.id == id) &&
  3691. (port_info->phy_info[i].attached.channel ==
  3692. channel))
  3693. phy_info = &port_info->phy_info[i];
  3694. }
  3695. }
  3696. mutex_unlock(&ioc->sas_topology_mutex);
  3697. return phy_info;
  3698. }
  3699. static void
  3700. mptsas_reprobe_lun(struct scsi_device *sdev, void *data)
  3701. {
  3702. int rc;
  3703. sdev->no_uld_attach = data ? 1 : 0;
  3704. rc = scsi_device_reprobe(sdev);
  3705. }
  3706. static void
  3707. mptsas_reprobe_target(struct scsi_target *starget, int uld_attach)
  3708. {
  3709. starget_for_each_device(starget, uld_attach ? (void *)1 : NULL,
  3710. mptsas_reprobe_lun);
  3711. }
  3712. static void
  3713. mptsas_adding_inactive_raid_components(MPT_ADAPTER *ioc, u8 channel, u8 id)
  3714. {
  3715. CONFIGPARMS cfg;
  3716. ConfigPageHeader_t hdr;
  3717. dma_addr_t dma_handle;
  3718. pRaidVolumePage0_t buffer = NULL;
  3719. RaidPhysDiskPage0_t phys_disk;
  3720. int i;
  3721. struct mptsas_phyinfo *phy_info;
  3722. struct mptsas_devinfo sas_device;
  3723. memset(&cfg, 0 , sizeof(CONFIGPARMS));
  3724. memset(&hdr, 0 , sizeof(ConfigPageHeader_t));
  3725. hdr.PageType = MPI_CONFIG_PAGETYPE_RAID_VOLUME;
  3726. cfg.pageAddr = (channel << 8) + id;
  3727. cfg.cfghdr.hdr = &hdr;
  3728. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  3729. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  3730. if (mpt_config(ioc, &cfg) != 0)
  3731. goto out;
  3732. if (!hdr.PageLength)
  3733. goto out;
  3734. buffer = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4,
  3735. &dma_handle);
  3736. if (!buffer)
  3737. goto out;
  3738. cfg.physAddr = dma_handle;
  3739. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  3740. if (mpt_config(ioc, &cfg) != 0)
  3741. goto out;
  3742. if (!(buffer->VolumeStatus.Flags &
  3743. MPI_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE))
  3744. goto out;
  3745. if (!buffer->NumPhysDisks)
  3746. goto out;
  3747. for (i = 0; i < buffer->NumPhysDisks; i++) {
  3748. if (mpt_raid_phys_disk_pg0(ioc,
  3749. buffer->PhysDisk[i].PhysDiskNum, &phys_disk) != 0)
  3750. continue;
  3751. if (mptsas_sas_device_pg0(ioc, &sas_device,
  3752. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  3753. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  3754. (phys_disk.PhysDiskBus << 8) +
  3755. phys_disk.PhysDiskID))
  3756. continue;
  3757. /* If there is no FW B_T mapping for this device then continue
  3758. * */
  3759. if (!(sas_device.flags & MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)
  3760. || !(sas_device.flags &
  3761. MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED))
  3762. continue;
  3763. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  3764. sas_device.sas_address);
  3765. mptsas_add_end_device(ioc, phy_info);
  3766. }
  3767. out:
  3768. if (buffer)
  3769. pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, buffer,
  3770. dma_handle);
  3771. }
  3772. /*
  3773. * Work queue thread to handle SAS hotplug events
  3774. */
  3775. static void
  3776. mptsas_hotplug_work(MPT_ADAPTER *ioc, struct fw_event_work *fw_event,
  3777. struct mptsas_hotplug_event *hot_plug_info)
  3778. {
  3779. struct mptsas_phyinfo *phy_info;
  3780. struct scsi_target * starget;
  3781. struct mptsas_devinfo sas_device;
  3782. VirtTarget *vtarget;
  3783. int i;
  3784. struct mptsas_portinfo *port_info;
  3785. switch (hot_plug_info->event_type) {
  3786. case MPTSAS_ADD_PHYSDISK:
  3787. if (!ioc->raid_data.pIocPg2)
  3788. break;
  3789. for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  3790. if (ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID ==
  3791. hot_plug_info->id) {
  3792. printk(MYIOC_s_WARN_FMT "firmware bug: unable "
  3793. "to add hidden disk - target_id matchs "
  3794. "volume_id\n", ioc->name);
  3795. mptsas_free_fw_event(ioc, fw_event);
  3796. return;
  3797. }
  3798. }
  3799. mpt_findImVolumes(ioc);
  3800. case MPTSAS_ADD_DEVICE:
  3801. memset(&sas_device, 0, sizeof(struct mptsas_devinfo));
  3802. mptsas_sas_device_pg0(ioc, &sas_device,
  3803. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  3804. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  3805. (hot_plug_info->channel << 8) +
  3806. hot_plug_info->id);
  3807. /* If there is no FW B_T mapping for this device then break
  3808. * */
  3809. if (!(sas_device.flags & MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)
  3810. || !(sas_device.flags &
  3811. MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED))
  3812. break;
  3813. if (!sas_device.handle)
  3814. return;
  3815. phy_info = mptsas_refreshing_device_handles(ioc, &sas_device);
  3816. /* Only For SATA Device ADD */
  3817. if (!phy_info && (sas_device.device_info &
  3818. MPI_SAS_DEVICE_INFO_SATA_DEVICE)) {
  3819. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  3820. "%s %d SATA HOT PLUG: "
  3821. "parent handle of device %x\n", ioc->name,
  3822. __func__, __LINE__, sas_device.handle_parent));
  3823. port_info = mptsas_find_portinfo_by_handle(ioc,
  3824. sas_device.handle_parent);
  3825. if (port_info == ioc->hba_port_info)
  3826. mptsas_probe_hba_phys(ioc);
  3827. else if (port_info)
  3828. mptsas_expander_refresh(ioc, port_info);
  3829. else {
  3830. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3831. "%s %d port info is NULL\n",
  3832. ioc->name, __func__, __LINE__));
  3833. break;
  3834. }
  3835. phy_info = mptsas_refreshing_device_handles
  3836. (ioc, &sas_device);
  3837. }
  3838. if (!phy_info) {
  3839. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3840. "%s %d phy info is NULL\n",
  3841. ioc->name, __func__, __LINE__));
  3842. break;
  3843. }
  3844. if (mptsas_get_rphy(phy_info))
  3845. break;
  3846. mptsas_add_end_device(ioc, phy_info);
  3847. break;
  3848. case MPTSAS_DEL_DEVICE:
  3849. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  3850. hot_plug_info->sas_address);
  3851. mptsas_del_end_device(ioc, phy_info);
  3852. break;
  3853. case MPTSAS_DEL_PHYSDISK:
  3854. mpt_findImVolumes(ioc);
  3855. phy_info = mptsas_find_phyinfo_by_phys_disk_num(
  3856. ioc, hot_plug_info->phys_disk_num,
  3857. hot_plug_info->channel,
  3858. hot_plug_info->id);
  3859. mptsas_del_end_device(ioc, phy_info);
  3860. break;
  3861. case MPTSAS_ADD_PHYSDISK_REPROBE:
  3862. if (mptsas_sas_device_pg0(ioc, &sas_device,
  3863. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  3864. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  3865. (hot_plug_info->channel << 8) + hot_plug_info->id)) {
  3866. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3867. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3868. __func__, hot_plug_info->id, __LINE__));
  3869. break;
  3870. }
  3871. /* If there is no FW B_T mapping for this device then break
  3872. * */
  3873. if (!(sas_device.flags & MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)
  3874. || !(sas_device.flags &
  3875. MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED))
  3876. break;
  3877. phy_info = mptsas_find_phyinfo_by_sas_address(
  3878. ioc, sas_device.sas_address);
  3879. if (!phy_info) {
  3880. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3881. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3882. __func__, hot_plug_info->id, __LINE__));
  3883. break;
  3884. }
  3885. starget = mptsas_get_starget(phy_info);
  3886. if (!starget) {
  3887. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3888. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3889. __func__, hot_plug_info->id, __LINE__));
  3890. break;
  3891. }
  3892. vtarget = starget->hostdata;
  3893. if (!vtarget) {
  3894. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3895. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3896. __func__, hot_plug_info->id, __LINE__));
  3897. break;
  3898. }
  3899. mpt_findImVolumes(ioc);
  3900. starget_printk(KERN_INFO, starget, MYIOC_s_FMT "RAID Hidding: "
  3901. "fw_channel=%d, fw_id=%d, physdsk %d, sas_addr 0x%llx\n",
  3902. ioc->name, hot_plug_info->channel, hot_plug_info->id,
  3903. hot_plug_info->phys_disk_num, (unsigned long long)
  3904. sas_device.sas_address);
  3905. vtarget->id = hot_plug_info->phys_disk_num;
  3906. vtarget->tflags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
  3907. phy_info->attached.phys_disk_num = hot_plug_info->phys_disk_num;
  3908. mptsas_reprobe_target(starget, 1);
  3909. break;
  3910. case MPTSAS_DEL_PHYSDISK_REPROBE:
  3911. if (mptsas_sas_device_pg0(ioc, &sas_device,
  3912. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  3913. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  3914. (hot_plug_info->channel << 8) + hot_plug_info->id)) {
  3915. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3916. "%s: fw_id=%d exit at line=%d\n",
  3917. ioc->name, __func__,
  3918. hot_plug_info->id, __LINE__));
  3919. break;
  3920. }
  3921. /* If there is no FW B_T mapping for this device then break
  3922. * */
  3923. if (!(sas_device.flags & MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)
  3924. || !(sas_device.flags &
  3925. MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED))
  3926. break;
  3927. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  3928. sas_device.sas_address);
  3929. if (!phy_info) {
  3930. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3931. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3932. __func__, hot_plug_info->id, __LINE__));
  3933. break;
  3934. }
  3935. starget = mptsas_get_starget(phy_info);
  3936. if (!starget) {
  3937. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3938. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3939. __func__, hot_plug_info->id, __LINE__));
  3940. break;
  3941. }
  3942. vtarget = starget->hostdata;
  3943. if (!vtarget) {
  3944. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3945. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3946. __func__, hot_plug_info->id, __LINE__));
  3947. break;
  3948. }
  3949. if (!(vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT)) {
  3950. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3951. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3952. __func__, hot_plug_info->id, __LINE__));
  3953. break;
  3954. }
  3955. mpt_findImVolumes(ioc);
  3956. starget_printk(KERN_INFO, starget, MYIOC_s_FMT "RAID Exposing:"
  3957. " fw_channel=%d, fw_id=%d, physdsk %d, sas_addr 0x%llx\n",
  3958. ioc->name, hot_plug_info->channel, hot_plug_info->id,
  3959. hot_plug_info->phys_disk_num, (unsigned long long)
  3960. sas_device.sas_address);
  3961. vtarget->tflags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  3962. vtarget->id = hot_plug_info->id;
  3963. phy_info->attached.phys_disk_num = ~0;
  3964. mptsas_reprobe_target(starget, 0);
  3965. mptsas_add_device_component_by_fw(ioc,
  3966. hot_plug_info->channel, hot_plug_info->id);
  3967. break;
  3968. case MPTSAS_ADD_RAID:
  3969. mpt_findImVolumes(ioc);
  3970. printk(MYIOC_s_INFO_FMT "attaching raid volume, channel %d, "
  3971. "id %d\n", ioc->name, MPTSAS_RAID_CHANNEL,
  3972. hot_plug_info->id);
  3973. scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL,
  3974. hot_plug_info->id, 0);
  3975. break;
  3976. case MPTSAS_DEL_RAID:
  3977. mpt_findImVolumes(ioc);
  3978. printk(MYIOC_s_INFO_FMT "removing raid volume, channel %d, "
  3979. "id %d\n", ioc->name, MPTSAS_RAID_CHANNEL,
  3980. hot_plug_info->id);
  3981. scsi_remove_device(hot_plug_info->sdev);
  3982. scsi_device_put(hot_plug_info->sdev);
  3983. break;
  3984. case MPTSAS_ADD_INACTIVE_VOLUME:
  3985. mpt_findImVolumes(ioc);
  3986. mptsas_adding_inactive_raid_components(ioc,
  3987. hot_plug_info->channel, hot_plug_info->id);
  3988. break;
  3989. default:
  3990. break;
  3991. }
  3992. mptsas_free_fw_event(ioc, fw_event);
  3993. }
  3994. static void
  3995. mptsas_send_sas_event(struct fw_event_work *fw_event)
  3996. {
  3997. MPT_ADAPTER *ioc;
  3998. struct mptsas_hotplug_event hot_plug_info;
  3999. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data;
  4000. u32 device_info;
  4001. u64 sas_address;
  4002. ioc = fw_event->ioc;
  4003. sas_event_data = (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)
  4004. fw_event->event_data;
  4005. device_info = le32_to_cpu(sas_event_data->DeviceInfo);
  4006. if ((device_info &
  4007. (MPI_SAS_DEVICE_INFO_SSP_TARGET |
  4008. MPI_SAS_DEVICE_INFO_STP_TARGET |
  4009. MPI_SAS_DEVICE_INFO_SATA_DEVICE)) == 0) {
  4010. mptsas_free_fw_event(ioc, fw_event);
  4011. return;
  4012. }
  4013. if (sas_event_data->ReasonCode ==
  4014. MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED) {
  4015. mptbase_sas_persist_operation(ioc,
  4016. MPI_SAS_OP_CLEAR_NOT_PRESENT);
  4017. mptsas_free_fw_event(ioc, fw_event);
  4018. return;
  4019. }
  4020. switch (sas_event_data->ReasonCode) {
  4021. case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
  4022. case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
  4023. memset(&hot_plug_info, 0, sizeof(struct mptsas_hotplug_event));
  4024. hot_plug_info.handle = le16_to_cpu(sas_event_data->DevHandle);
  4025. hot_plug_info.channel = sas_event_data->Bus;
  4026. hot_plug_info.id = sas_event_data->TargetID;
  4027. hot_plug_info.phy_id = sas_event_data->PhyNum;
  4028. memcpy(&sas_address, &sas_event_data->SASAddress,
  4029. sizeof(u64));
  4030. hot_plug_info.sas_address = le64_to_cpu(sas_address);
  4031. hot_plug_info.device_info = device_info;
  4032. if (sas_event_data->ReasonCode &
  4033. MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
  4034. hot_plug_info.event_type = MPTSAS_ADD_DEVICE;
  4035. else
  4036. hot_plug_info.event_type = MPTSAS_DEL_DEVICE;
  4037. mptsas_hotplug_work(ioc, fw_event, &hot_plug_info);
  4038. break;
  4039. case MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED:
  4040. mptbase_sas_persist_operation(ioc,
  4041. MPI_SAS_OP_CLEAR_NOT_PRESENT);
  4042. mptsas_free_fw_event(ioc, fw_event);
  4043. break;
  4044. case MPI_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  4045. /* TODO */
  4046. case MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  4047. /* TODO */
  4048. default:
  4049. mptsas_free_fw_event(ioc, fw_event);
  4050. break;
  4051. }
  4052. }
  4053. static void
  4054. mptsas_send_raid_event(struct fw_event_work *fw_event)
  4055. {
  4056. MPT_ADAPTER *ioc;
  4057. EVENT_DATA_RAID *raid_event_data;
  4058. struct mptsas_hotplug_event hot_plug_info;
  4059. int status;
  4060. int state;
  4061. struct scsi_device *sdev = NULL;
  4062. VirtDevice *vdevice = NULL;
  4063. RaidPhysDiskPage0_t phys_disk;
  4064. ioc = fw_event->ioc;
  4065. raid_event_data = (EVENT_DATA_RAID *)fw_event->event_data;
  4066. status = le32_to_cpu(raid_event_data->SettingsStatus);
  4067. state = (status >> 8) & 0xff;
  4068. memset(&hot_plug_info, 0, sizeof(struct mptsas_hotplug_event));
  4069. hot_plug_info.id = raid_event_data->VolumeID;
  4070. hot_plug_info.channel = raid_event_data->VolumeBus;
  4071. hot_plug_info.phys_disk_num = raid_event_data->PhysDiskNum;
  4072. if (raid_event_data->ReasonCode == MPI_EVENT_RAID_RC_VOLUME_DELETED ||
  4073. raid_event_data->ReasonCode == MPI_EVENT_RAID_RC_VOLUME_CREATED ||
  4074. raid_event_data->ReasonCode ==
  4075. MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED) {
  4076. sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
  4077. hot_plug_info.id, 0);
  4078. hot_plug_info.sdev = sdev;
  4079. if (sdev)
  4080. vdevice = sdev->hostdata;
  4081. }
  4082. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Entering %s: "
  4083. "ReasonCode=%02x\n", ioc->name, __func__,
  4084. raid_event_data->ReasonCode));
  4085. switch (raid_event_data->ReasonCode) {
  4086. case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
  4087. hot_plug_info.event_type = MPTSAS_DEL_PHYSDISK_REPROBE;
  4088. break;
  4089. case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
  4090. hot_plug_info.event_type = MPTSAS_ADD_PHYSDISK_REPROBE;
  4091. break;
  4092. case MPI_EVENT_RAID_RC_PHYSDISK_STATUS_CHANGED:
  4093. switch (state) {
  4094. case MPI_PD_STATE_ONLINE:
  4095. case MPI_PD_STATE_NOT_COMPATIBLE:
  4096. mpt_raid_phys_disk_pg0(ioc,
  4097. raid_event_data->PhysDiskNum, &phys_disk);
  4098. hot_plug_info.id = phys_disk.PhysDiskID;
  4099. hot_plug_info.channel = phys_disk.PhysDiskBus;
  4100. hot_plug_info.event_type = MPTSAS_ADD_PHYSDISK;
  4101. break;
  4102. case MPI_PD_STATE_FAILED:
  4103. case MPI_PD_STATE_MISSING:
  4104. case MPI_PD_STATE_OFFLINE_AT_HOST_REQUEST:
  4105. case MPI_PD_STATE_FAILED_AT_HOST_REQUEST:
  4106. case MPI_PD_STATE_OFFLINE_FOR_ANOTHER_REASON:
  4107. hot_plug_info.event_type = MPTSAS_DEL_PHYSDISK;
  4108. break;
  4109. default:
  4110. break;
  4111. }
  4112. break;
  4113. case MPI_EVENT_RAID_RC_VOLUME_DELETED:
  4114. if (!sdev)
  4115. break;
  4116. vdevice->vtarget->deleted = 1; /* block IO */
  4117. hot_plug_info.event_type = MPTSAS_DEL_RAID;
  4118. break;
  4119. case MPI_EVENT_RAID_RC_VOLUME_CREATED:
  4120. if (sdev) {
  4121. scsi_device_put(sdev);
  4122. break;
  4123. }
  4124. hot_plug_info.event_type = MPTSAS_ADD_RAID;
  4125. break;
  4126. case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
  4127. if (!(status & MPI_RAIDVOL0_STATUS_FLAG_ENABLED)) {
  4128. if (!sdev)
  4129. break;
  4130. vdevice->vtarget->deleted = 1; /* block IO */
  4131. hot_plug_info.event_type = MPTSAS_DEL_RAID;
  4132. break;
  4133. }
  4134. switch (state) {
  4135. case MPI_RAIDVOL0_STATUS_STATE_FAILED:
  4136. case MPI_RAIDVOL0_STATUS_STATE_MISSING:
  4137. if (!sdev)
  4138. break;
  4139. vdevice->vtarget->deleted = 1; /* block IO */
  4140. hot_plug_info.event_type = MPTSAS_DEL_RAID;
  4141. break;
  4142. case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL:
  4143. case MPI_RAIDVOL0_STATUS_STATE_DEGRADED:
  4144. if (sdev) {
  4145. scsi_device_put(sdev);
  4146. break;
  4147. }
  4148. hot_plug_info.event_type = MPTSAS_ADD_RAID;
  4149. break;
  4150. default:
  4151. break;
  4152. }
  4153. break;
  4154. default:
  4155. break;
  4156. }
  4157. if (hot_plug_info.event_type != MPTSAS_IGNORE_EVENT)
  4158. mptsas_hotplug_work(ioc, fw_event, &hot_plug_info);
  4159. else
  4160. mptsas_free_fw_event(ioc, fw_event);
  4161. }
  4162. /**
  4163. * mptsas_issue_tm - send mptsas internal tm request
  4164. * @ioc: Pointer to MPT_ADAPTER structure
  4165. * @type: Task Management type
  4166. * @channel: channel number for task management
  4167. * @id: Logical Target ID for reset (if appropriate)
  4168. * @lun: Logical unit for reset (if appropriate)
  4169. * @task_context: Context for the task to be aborted
  4170. * @timeout: timeout for task management control
  4171. *
  4172. * return 0 on success and -1 on failure:
  4173. *
  4174. */
  4175. static int
  4176. mptsas_issue_tm(MPT_ADAPTER *ioc, u8 type, u8 channel, u8 id, u64 lun,
  4177. int task_context, ulong timeout, u8 *issue_reset)
  4178. {
  4179. MPT_FRAME_HDR *mf;
  4180. SCSITaskMgmt_t *pScsiTm;
  4181. int retval;
  4182. unsigned long timeleft;
  4183. *issue_reset = 0;
  4184. mf = mpt_get_msg_frame(mptsasDeviceResetCtx, ioc);
  4185. if (mf == NULL) {
  4186. retval = -1; /* return failure */
  4187. dtmprintk(ioc, printk(MYIOC_s_WARN_FMT "TaskMgmt request: no "
  4188. "msg frames!!\n", ioc->name));
  4189. goto out;
  4190. }
  4191. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TaskMgmt request: mr = %p, "
  4192. "task_type = 0x%02X,\n\t timeout = %ld, fw_channel = %d, "
  4193. "fw_id = %d, lun = %lld,\n\t task_context = 0x%x\n", ioc->name, mf,
  4194. type, timeout, channel, id, (unsigned long long)lun,
  4195. task_context));
  4196. pScsiTm = (SCSITaskMgmt_t *) mf;
  4197. memset(pScsiTm, 0, sizeof(SCSITaskMgmt_t));
  4198. pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
  4199. pScsiTm->TaskType = type;
  4200. pScsiTm->MsgFlags = 0;
  4201. pScsiTm->TargetID = id;
  4202. pScsiTm->Bus = channel;
  4203. pScsiTm->ChainOffset = 0;
  4204. pScsiTm->Reserved = 0;
  4205. pScsiTm->Reserved1 = 0;
  4206. pScsiTm->TaskMsgContext = task_context;
  4207. int_to_scsilun(lun, (struct scsi_lun *)pScsiTm->LUN);
  4208. INITIALIZE_MGMT_STATUS(ioc->taskmgmt_cmds.status)
  4209. CLEAR_MGMT_STATUS(ioc->internal_cmds.status)
  4210. retval = 0;
  4211. mpt_put_msg_frame_hi_pri(mptsasDeviceResetCtx, ioc, mf);
  4212. /* Now wait for the command to complete */
  4213. timeleft = wait_for_completion_timeout(&ioc->taskmgmt_cmds.done,
  4214. timeout*HZ);
  4215. if (!(ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_COMMAND_GOOD)) {
  4216. retval = -1; /* return failure */
  4217. dtmprintk(ioc, printk(MYIOC_s_ERR_FMT
  4218. "TaskMgmt request: TIMED OUT!(mr=%p)\n", ioc->name, mf));
  4219. mpt_free_msg_frame(ioc, mf);
  4220. if (ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_DID_IOCRESET)
  4221. goto out;
  4222. *issue_reset = 1;
  4223. goto out;
  4224. }
  4225. if (!(ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_RF_VALID)) {
  4226. retval = -1; /* return failure */
  4227. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4228. "TaskMgmt request: failed with no reply\n", ioc->name));
  4229. goto out;
  4230. }
  4231. out:
  4232. CLEAR_MGMT_STATUS(ioc->taskmgmt_cmds.status)
  4233. return retval;
  4234. }
  4235. /**
  4236. * mptsas_broadcast_primative_work - Handle broadcast primitives
  4237. * @work: work queue payload containing info describing the event
  4238. *
  4239. * this will be handled in workqueue context.
  4240. */
  4241. static void
  4242. mptsas_broadcast_primative_work(struct fw_event_work *fw_event)
  4243. {
  4244. MPT_ADAPTER *ioc = fw_event->ioc;
  4245. MPT_FRAME_HDR *mf;
  4246. VirtDevice *vdevice;
  4247. int ii;
  4248. struct scsi_cmnd *sc;
  4249. SCSITaskMgmtReply_t *pScsiTmReply;
  4250. u8 issue_reset;
  4251. int task_context;
  4252. u8 channel, id;
  4253. int lun;
  4254. u32 termination_count;
  4255. u32 query_count;
  4256. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4257. "%s - enter\n", ioc->name, __func__));
  4258. mutex_lock(&ioc->taskmgmt_cmds.mutex);
  4259. if (mpt_set_taskmgmt_in_progress_flag(ioc) != 0) {
  4260. mutex_unlock(&ioc->taskmgmt_cmds.mutex);
  4261. mptsas_requeue_fw_event(ioc, fw_event, 1000);
  4262. return;
  4263. }
  4264. issue_reset = 0;
  4265. termination_count = 0;
  4266. query_count = 0;
  4267. mpt_findImVolumes(ioc);
  4268. pScsiTmReply = (SCSITaskMgmtReply_t *) ioc->taskmgmt_cmds.reply;
  4269. for (ii = 0; ii < ioc->req_depth; ii++) {
  4270. if (ioc->fw_events_off)
  4271. goto out;
  4272. sc = mptscsih_get_scsi_lookup(ioc, ii);
  4273. if (!sc)
  4274. continue;
  4275. mf = MPT_INDEX_2_MFPTR(ioc, ii);
  4276. if (!mf)
  4277. continue;
  4278. task_context = mf->u.frame.hwhdr.msgctxu.MsgContext;
  4279. vdevice = sc->device->hostdata;
  4280. if (!vdevice || !vdevice->vtarget)
  4281. continue;
  4282. if (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT)
  4283. continue; /* skip hidden raid components */
  4284. if (vdevice->vtarget->raidVolume)
  4285. continue; /* skip hidden raid components */
  4286. channel = vdevice->vtarget->channel;
  4287. id = vdevice->vtarget->id;
  4288. lun = vdevice->lun;
  4289. if (mptsas_issue_tm(ioc, MPI_SCSITASKMGMT_TASKTYPE_QUERY_TASK,
  4290. channel, id, (u64)lun, task_context, 30, &issue_reset))
  4291. goto out;
  4292. query_count++;
  4293. termination_count +=
  4294. le32_to_cpu(pScsiTmReply->TerminationCount);
  4295. if ((pScsiTmReply->IOCStatus == MPI_IOCSTATUS_SUCCESS) &&
  4296. (pScsiTmReply->ResponseCode ==
  4297. MPI_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
  4298. pScsiTmReply->ResponseCode ==
  4299. MPI_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
  4300. continue;
  4301. if (mptsas_issue_tm(ioc,
  4302. MPI_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET,
  4303. channel, id, (u64)lun, 0, 30, &issue_reset))
  4304. goto out;
  4305. termination_count +=
  4306. le32_to_cpu(pScsiTmReply->TerminationCount);
  4307. }
  4308. out:
  4309. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4310. "%s - exit, query_count = %d termination_count = %d\n",
  4311. ioc->name, __func__, query_count, termination_count));
  4312. ioc->broadcast_aen_busy = 0;
  4313. mpt_clear_taskmgmt_in_progress_flag(ioc);
  4314. mutex_unlock(&ioc->taskmgmt_cmds.mutex);
  4315. if (issue_reset) {
  4316. printk(MYIOC_s_WARN_FMT
  4317. "Issuing Reset from %s!! doorbell=0x%08x\n",
  4318. ioc->name, __func__, mpt_GetIocState(ioc, 0));
  4319. mpt_Soft_Hard_ResetHandler(ioc, CAN_SLEEP);
  4320. }
  4321. mptsas_free_fw_event(ioc, fw_event);
  4322. }
  4323. /*
  4324. * mptsas_send_ir2_event - handle exposing hidden disk when
  4325. * an inactive raid volume is added
  4326. *
  4327. * @ioc: Pointer to MPT_ADAPTER structure
  4328. * @ir2_data
  4329. *
  4330. */
  4331. static void
  4332. mptsas_send_ir2_event(struct fw_event_work *fw_event)
  4333. {
  4334. MPT_ADAPTER *ioc;
  4335. struct mptsas_hotplug_event hot_plug_info;
  4336. MPI_EVENT_DATA_IR2 *ir2_data;
  4337. u8 reasonCode;
  4338. RaidPhysDiskPage0_t phys_disk;
  4339. ioc = fw_event->ioc;
  4340. ir2_data = (MPI_EVENT_DATA_IR2 *)fw_event->event_data;
  4341. reasonCode = ir2_data->ReasonCode;
  4342. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Entering %s: "
  4343. "ReasonCode=%02x\n", ioc->name, __func__, reasonCode));
  4344. memset(&hot_plug_info, 0, sizeof(struct mptsas_hotplug_event));
  4345. hot_plug_info.id = ir2_data->TargetID;
  4346. hot_plug_info.channel = ir2_data->Bus;
  4347. switch (reasonCode) {
  4348. case MPI_EVENT_IR2_RC_FOREIGN_CFG_DETECTED:
  4349. hot_plug_info.event_type = MPTSAS_ADD_INACTIVE_VOLUME;
  4350. break;
  4351. case MPI_EVENT_IR2_RC_DUAL_PORT_REMOVED:
  4352. hot_plug_info.phys_disk_num = ir2_data->PhysDiskNum;
  4353. hot_plug_info.event_type = MPTSAS_DEL_PHYSDISK;
  4354. break;
  4355. case MPI_EVENT_IR2_RC_DUAL_PORT_ADDED:
  4356. hot_plug_info.phys_disk_num = ir2_data->PhysDiskNum;
  4357. mpt_raid_phys_disk_pg0(ioc,
  4358. ir2_data->PhysDiskNum, &phys_disk);
  4359. hot_plug_info.id = phys_disk.PhysDiskID;
  4360. hot_plug_info.event_type = MPTSAS_ADD_PHYSDISK;
  4361. break;
  4362. default:
  4363. mptsas_free_fw_event(ioc, fw_event);
  4364. return;
  4365. }
  4366. mptsas_hotplug_work(ioc, fw_event, &hot_plug_info);
  4367. }
  4368. static int
  4369. mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
  4370. {
  4371. u32 event = le32_to_cpu(reply->Event);
  4372. int event_data_sz;
  4373. struct fw_event_work *fw_event;
  4374. unsigned long delay;
  4375. if (ioc->bus_type != SAS)
  4376. return 0;
  4377. /* events turned off due to host reset or driver unloading */
  4378. if (ioc->fw_events_off)
  4379. return 0;
  4380. delay = msecs_to_jiffies(1);
  4381. switch (event) {
  4382. case MPI_EVENT_SAS_BROADCAST_PRIMITIVE:
  4383. {
  4384. EVENT_DATA_SAS_BROADCAST_PRIMITIVE *broadcast_event_data =
  4385. (EVENT_DATA_SAS_BROADCAST_PRIMITIVE *)reply->Data;
  4386. if (broadcast_event_data->Primitive !=
  4387. MPI_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
  4388. return 0;
  4389. if (ioc->broadcast_aen_busy)
  4390. return 0;
  4391. ioc->broadcast_aen_busy = 1;
  4392. break;
  4393. }
  4394. case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
  4395. {
  4396. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data =
  4397. (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data;
  4398. u16 ioc_stat;
  4399. ioc_stat = le16_to_cpu(reply->IOCStatus);
  4400. if (sas_event_data->ReasonCode ==
  4401. MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING) {
  4402. mptsas_target_reset_queue(ioc, sas_event_data);
  4403. return 0;
  4404. }
  4405. if (sas_event_data->ReasonCode ==
  4406. MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
  4407. ioc->device_missing_delay &&
  4408. (ioc_stat & MPI_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)) {
  4409. VirtTarget *vtarget = NULL;
  4410. u8 id, channel;
  4411. id = sas_event_data->TargetID;
  4412. channel = sas_event_data->Bus;
  4413. vtarget = mptsas_find_vtarget(ioc, channel, id);
  4414. if (vtarget) {
  4415. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4416. "LogInfo (0x%x) available for "
  4417. "INTERNAL_DEVICE_RESET"
  4418. "fw_id %d fw_channel %d\n", ioc->name,
  4419. le32_to_cpu(reply->IOCLogInfo),
  4420. id, channel));
  4421. if (vtarget->raidVolume) {
  4422. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4423. "Skipping Raid Volume for inDMD\n",
  4424. ioc->name));
  4425. } else {
  4426. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4427. "Setting device flag inDMD\n",
  4428. ioc->name));
  4429. vtarget->inDMD = 1;
  4430. }
  4431. }
  4432. }
  4433. break;
  4434. }
  4435. case MPI_EVENT_SAS_EXPANDER_STATUS_CHANGE:
  4436. {
  4437. MpiEventDataSasExpanderStatusChange_t *expander_data =
  4438. (MpiEventDataSasExpanderStatusChange_t *)reply->Data;
  4439. if (ioc->old_sas_discovery_protocal)
  4440. return 0;
  4441. if (expander_data->ReasonCode ==
  4442. MPI_EVENT_SAS_EXP_RC_NOT_RESPONDING &&
  4443. ioc->device_missing_delay)
  4444. delay = HZ * ioc->device_missing_delay;
  4445. break;
  4446. }
  4447. case MPI_EVENT_SAS_DISCOVERY:
  4448. {
  4449. u32 discovery_status;
  4450. EventDataSasDiscovery_t *discovery_data =
  4451. (EventDataSasDiscovery_t *)reply->Data;
  4452. discovery_status = le32_to_cpu(discovery_data->DiscoveryStatus);
  4453. ioc->sas_discovery_quiesce_io = discovery_status ? 1 : 0;
  4454. if (ioc->old_sas_discovery_protocal && !discovery_status)
  4455. mptsas_queue_rescan(ioc);
  4456. return 0;
  4457. }
  4458. case MPI_EVENT_INTEGRATED_RAID:
  4459. case MPI_EVENT_PERSISTENT_TABLE_FULL:
  4460. case MPI_EVENT_IR2:
  4461. case MPI_EVENT_SAS_PHY_LINK_STATUS:
  4462. case MPI_EVENT_QUEUE_FULL:
  4463. break;
  4464. default:
  4465. return 0;
  4466. }
  4467. event_data_sz = ((reply->MsgLength * 4) -
  4468. offsetof(EventNotificationReply_t, Data));
  4469. fw_event = kzalloc(sizeof(*fw_event) + event_data_sz, GFP_ATOMIC);
  4470. if (!fw_event) {
  4471. printk(MYIOC_s_WARN_FMT "%s: failed at (line=%d)\n", ioc->name,
  4472. __func__, __LINE__);
  4473. return 0;
  4474. }
  4475. memcpy(fw_event->event_data, reply->Data, event_data_sz);
  4476. fw_event->event = event;
  4477. fw_event->ioc = ioc;
  4478. mptsas_add_fw_event(ioc, fw_event, delay);
  4479. return 0;
  4480. }
  4481. /* Delete a volume when no longer listed in ioc pg2
  4482. */
  4483. static void mptsas_volume_delete(MPT_ADAPTER *ioc, u8 id)
  4484. {
  4485. struct scsi_device *sdev;
  4486. int i;
  4487. sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL, id, 0);
  4488. if (!sdev)
  4489. return;
  4490. if (!ioc->raid_data.pIocPg2)
  4491. goto out;
  4492. if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
  4493. goto out;
  4494. for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++)
  4495. if (ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID == id)
  4496. goto release_sdev;
  4497. out:
  4498. printk(MYIOC_s_INFO_FMT "removing raid volume, channel %d, "
  4499. "id %d\n", ioc->name, MPTSAS_RAID_CHANNEL, id);
  4500. scsi_remove_device(sdev);
  4501. release_sdev:
  4502. scsi_device_put(sdev);
  4503. }
  4504. static int
  4505. mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  4506. {
  4507. struct Scsi_Host *sh;
  4508. MPT_SCSI_HOST *hd;
  4509. MPT_ADAPTER *ioc;
  4510. unsigned long flags;
  4511. int ii;
  4512. int numSGE = 0;
  4513. int scale;
  4514. int ioc_cap;
  4515. int error=0;
  4516. int r;
  4517. r = mpt_attach(pdev,id);
  4518. if (r)
  4519. return r;
  4520. ioc = pci_get_drvdata(pdev);
  4521. mptsas_fw_event_off(ioc);
  4522. ioc->DoneCtx = mptsasDoneCtx;
  4523. ioc->TaskCtx = mptsasTaskCtx;
  4524. ioc->InternalCtx = mptsasInternalCtx;
  4525. ioc->schedule_target_reset = &mptsas_schedule_target_reset;
  4526. ioc->schedule_dead_ioc_flush_running_cmds =
  4527. &mptscsih_flush_running_cmds;
  4528. /* Added sanity check on readiness of the MPT adapter.
  4529. */
  4530. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  4531. printk(MYIOC_s_WARN_FMT
  4532. "Skipping because it's not operational!\n",
  4533. ioc->name);
  4534. error = -ENODEV;
  4535. goto out_mptsas_probe;
  4536. }
  4537. if (!ioc->active) {
  4538. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  4539. ioc->name);
  4540. error = -ENODEV;
  4541. goto out_mptsas_probe;
  4542. }
  4543. /* Sanity check - ensure at least 1 port is INITIATOR capable
  4544. */
  4545. ioc_cap = 0;
  4546. for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
  4547. if (ioc->pfacts[ii].ProtocolFlags &
  4548. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  4549. ioc_cap++;
  4550. }
  4551. if (!ioc_cap) {
  4552. printk(MYIOC_s_WARN_FMT
  4553. "Skipping ioc=%p because SCSI Initiator mode "
  4554. "is NOT enabled!\n", ioc->name, ioc);
  4555. return 0;
  4556. }
  4557. sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
  4558. if (!sh) {
  4559. printk(MYIOC_s_WARN_FMT
  4560. "Unable to register controller with SCSI subsystem\n",
  4561. ioc->name);
  4562. error = -1;
  4563. goto out_mptsas_probe;
  4564. }
  4565. spin_lock_irqsave(&ioc->FreeQlock, flags);
  4566. /* Attach the SCSI Host to the IOC structure
  4567. */
  4568. ioc->sh = sh;
  4569. sh->io_port = 0;
  4570. sh->n_io_port = 0;
  4571. sh->irq = 0;
  4572. /* set 16 byte cdb's */
  4573. sh->max_cmd_len = 16;
  4574. sh->can_queue = min_t(int, ioc->req_depth - 10, sh->can_queue);
  4575. sh->max_id = -1;
  4576. sh->max_lun = max_lun;
  4577. sh->transportt = mptsas_transport_template;
  4578. /* Required entry.
  4579. */
  4580. sh->unique_id = ioc->id;
  4581. INIT_LIST_HEAD(&ioc->sas_topology);
  4582. mutex_init(&ioc->sas_topology_mutex);
  4583. mutex_init(&ioc->sas_discovery_mutex);
  4584. mutex_init(&ioc->sas_mgmt.mutex);
  4585. init_completion(&ioc->sas_mgmt.done);
  4586. /* Verify that we won't exceed the maximum
  4587. * number of chain buffers
  4588. * We can optimize: ZZ = req_sz/sizeof(SGE)
  4589. * For 32bit SGE's:
  4590. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  4591. * + (req_sz - 64)/sizeof(SGE)
  4592. * A slightly different algorithm is required for
  4593. * 64bit SGEs.
  4594. */
  4595. scale = ioc->req_sz/ioc->SGE_size;
  4596. if (ioc->sg_addr_size == sizeof(u64)) {
  4597. numSGE = (scale - 1) *
  4598. (ioc->facts.MaxChainDepth-1) + scale +
  4599. (ioc->req_sz - 60) / ioc->SGE_size;
  4600. } else {
  4601. numSGE = 1 + (scale - 1) *
  4602. (ioc->facts.MaxChainDepth-1) + scale +
  4603. (ioc->req_sz - 64) / ioc->SGE_size;
  4604. }
  4605. if (numSGE < sh->sg_tablesize) {
  4606. /* Reset this value */
  4607. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4608. "Resetting sg_tablesize to %d from %d\n",
  4609. ioc->name, numSGE, sh->sg_tablesize));
  4610. sh->sg_tablesize = numSGE;
  4611. }
  4612. if (mpt_loadtime_max_sectors) {
  4613. if (mpt_loadtime_max_sectors < 64 ||
  4614. mpt_loadtime_max_sectors > 8192) {
  4615. printk(MYIOC_s_INFO_FMT "Invalid value passed for"
  4616. "mpt_loadtime_max_sectors %d."
  4617. "Range from 64 to 8192\n", ioc->name,
  4618. mpt_loadtime_max_sectors);
  4619. }
  4620. mpt_loadtime_max_sectors &= 0xFFFFFFFE;
  4621. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4622. "Resetting max sector to %d from %d\n",
  4623. ioc->name, mpt_loadtime_max_sectors, sh->max_sectors));
  4624. sh->max_sectors = mpt_loadtime_max_sectors;
  4625. }
  4626. hd = shost_priv(sh);
  4627. hd->ioc = ioc;
  4628. /* SCSI needs scsi_cmnd lookup table!
  4629. * (with size equal to req_depth*PtrSz!)
  4630. */
  4631. ioc->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  4632. if (!ioc->ScsiLookup) {
  4633. error = -ENOMEM;
  4634. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  4635. goto out_mptsas_probe;
  4636. }
  4637. spin_lock_init(&ioc->scsi_lookup_lock);
  4638. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "ScsiLookup @ %p\n",
  4639. ioc->name, ioc->ScsiLookup));
  4640. ioc->sas_data.ptClear = mpt_pt_clear;
  4641. hd->last_queue_full = 0;
  4642. INIT_LIST_HEAD(&hd->target_reset_list);
  4643. INIT_LIST_HEAD(&ioc->sas_device_info_list);
  4644. mutex_init(&ioc->sas_device_info_mutex);
  4645. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  4646. if (ioc->sas_data.ptClear==1) {
  4647. mptbase_sas_persist_operation(
  4648. ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
  4649. }
  4650. error = scsi_add_host(sh, &ioc->pcidev->dev);
  4651. if (error) {
  4652. dprintk(ioc, printk(MYIOC_s_ERR_FMT
  4653. "scsi_add_host failed\n", ioc->name));
  4654. goto out_mptsas_probe;
  4655. }
  4656. /* older firmware doesn't support expander events */
  4657. if ((ioc->facts.HeaderVersion >> 8) < 0xE)
  4658. ioc->old_sas_discovery_protocal = 1;
  4659. mptsas_scan_sas_topology(ioc);
  4660. mptsas_fw_event_on(ioc);
  4661. return 0;
  4662. out_mptsas_probe:
  4663. mptscsih_remove(pdev);
  4664. return error;
  4665. }
  4666. static void
  4667. mptsas_shutdown(struct pci_dev *pdev)
  4668. {
  4669. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  4670. mptsas_fw_event_off(ioc);
  4671. mptsas_cleanup_fw_event_q(ioc);
  4672. }
  4673. static void mptsas_remove(struct pci_dev *pdev)
  4674. {
  4675. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  4676. struct mptsas_portinfo *p, *n;
  4677. int i;
  4678. if (!ioc->sh) {
  4679. printk(MYIOC_s_INFO_FMT "IOC is in Target mode\n", ioc->name);
  4680. mpt_detach(pdev);
  4681. return;
  4682. }
  4683. mptsas_shutdown(pdev);
  4684. mptsas_del_device_components(ioc);
  4685. ioc->sas_discovery_ignore_events = 1;
  4686. sas_remove_host(ioc->sh);
  4687. mutex_lock(&ioc->sas_topology_mutex);
  4688. list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
  4689. list_del(&p->list);
  4690. for (i = 0 ; i < p->num_phys ; i++)
  4691. mptsas_port_delete(ioc, p->phy_info[i].port_details);
  4692. kfree(p->phy_info);
  4693. kfree(p);
  4694. }
  4695. mutex_unlock(&ioc->sas_topology_mutex);
  4696. ioc->hba_port_info = NULL;
  4697. mptscsih_remove(pdev);
  4698. }
  4699. static struct pci_device_id mptsas_pci_table[] = {
  4700. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064,
  4701. PCI_ANY_ID, PCI_ANY_ID },
  4702. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068,
  4703. PCI_ANY_ID, PCI_ANY_ID },
  4704. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064E,
  4705. PCI_ANY_ID, PCI_ANY_ID },
  4706. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068E,
  4707. PCI_ANY_ID, PCI_ANY_ID },
  4708. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1078,
  4709. PCI_ANY_ID, PCI_ANY_ID },
  4710. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068_820XELP,
  4711. PCI_ANY_ID, PCI_ANY_ID },
  4712. {0} /* Terminating entry */
  4713. };
  4714. MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
  4715. static struct pci_driver mptsas_driver = {
  4716. .name = "mptsas",
  4717. .id_table = mptsas_pci_table,
  4718. .probe = mptsas_probe,
  4719. .remove = mptsas_remove,
  4720. .shutdown = mptsas_shutdown,
  4721. #ifdef CONFIG_PM
  4722. .suspend = mptscsih_suspend,
  4723. .resume = mptscsih_resume,
  4724. #endif
  4725. };
  4726. static int __init
  4727. mptsas_init(void)
  4728. {
  4729. int error;
  4730. show_mptmod_ver(my_NAME, my_VERSION);
  4731. mptsas_transport_template =
  4732. sas_attach_transport(&mptsas_transport_functions);
  4733. if (!mptsas_transport_template)
  4734. return -ENODEV;
  4735. mptsas_transport_template->eh_timed_out = mptsas_eh_timed_out;
  4736. mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER,
  4737. "mptscsih_io_done");
  4738. mptsasTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSAS_DRIVER,
  4739. "mptscsih_taskmgmt_complete");
  4740. mptsasInternalCtx =
  4741. mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER,
  4742. "mptscsih_scandv_complete");
  4743. mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER,
  4744. "mptsas_mgmt_done");
  4745. mptsasDeviceResetCtx =
  4746. mpt_register(mptsas_taskmgmt_complete, MPTSAS_DRIVER,
  4747. "mptsas_taskmgmt_complete");
  4748. mpt_event_register(mptsasDoneCtx, mptsas_event_process);
  4749. mpt_reset_register(mptsasDoneCtx, mptsas_ioc_reset);
  4750. error = pci_register_driver(&mptsas_driver);
  4751. if (error)
  4752. sas_release_transport(mptsas_transport_template);
  4753. return error;
  4754. }
  4755. static void __exit
  4756. mptsas_exit(void)
  4757. {
  4758. pci_unregister_driver(&mptsas_driver);
  4759. sas_release_transport(mptsas_transport_template);
  4760. mpt_reset_deregister(mptsasDoneCtx);
  4761. mpt_event_deregister(mptsasDoneCtx);
  4762. mpt_deregister(mptsasMgmtCtx);
  4763. mpt_deregister(mptsasInternalCtx);
  4764. mpt_deregister(mptsasTaskCtx);
  4765. mpt_deregister(mptsasDoneCtx);
  4766. mpt_deregister(mptsasDeviceResetCtx);
  4767. }
  4768. module_init(mptsas_init);
  4769. module_exit(mptsas_exit);