mpt2sas_scsih.c 160 KB

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