mptsas.c 134 KB

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