mptsas.c 134 KB

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