ibmvfc.c 141 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029
  1. /*
  2. * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
  3. *
  4. * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
  5. *
  6. * Copyright (C) IBM Corporation, 2008
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (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. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/dmapool.h>
  27. #include <linux/delay.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/kthread.h>
  30. #include <linux/slab.h>
  31. #include <linux/of.h>
  32. #include <linux/pm.h>
  33. #include <linux/stringify.h>
  34. #include <asm/firmware.h>
  35. #include <asm/irq.h>
  36. #include <asm/vio.h>
  37. #include <scsi/scsi.h>
  38. #include <scsi/scsi_cmnd.h>
  39. #include <scsi/scsi_host.h>
  40. #include <scsi/scsi_device.h>
  41. #include <scsi/scsi_tcq.h>
  42. #include <scsi/scsi_transport_fc.h>
  43. #include <scsi/scsi_bsg_fc.h>
  44. #include "ibmvfc.h"
  45. static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
  46. static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
  47. static u64 max_lun = IBMVFC_MAX_LUN;
  48. static unsigned int max_targets = IBMVFC_MAX_TARGETS;
  49. static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
  50. static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
  51. static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
  52. static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
  53. static unsigned int cls3_error = IBMVFC_CLS3_ERROR;
  54. static LIST_HEAD(ibmvfc_head);
  55. static DEFINE_SPINLOCK(ibmvfc_driver_lock);
  56. static struct scsi_transport_template *ibmvfc_transport_template;
  57. MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
  58. MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
  59. MODULE_LICENSE("GPL");
  60. MODULE_VERSION(IBMVFC_DRIVER_VERSION);
  61. module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
  62. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
  63. "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
  64. module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
  65. MODULE_PARM_DESC(default_timeout,
  66. "Default timeout in seconds for initialization and EH commands. "
  67. "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
  68. module_param_named(max_requests, max_requests, uint, S_IRUGO);
  69. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
  70. "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
  71. module_param_named(max_lun, max_lun, ullong, S_IRUGO);
  72. MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
  73. "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
  74. module_param_named(max_targets, max_targets, uint, S_IRUGO);
  75. MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
  76. "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
  77. module_param_named(disc_threads, disc_threads, uint, S_IRUGO);
  78. MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
  79. "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
  80. module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
  81. MODULE_PARM_DESC(debug, "Enable driver debug information. "
  82. "[Default=" __stringify(IBMVFC_DEBUG) "]");
  83. module_param_named(log_level, log_level, uint, 0);
  84. MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
  85. "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
  86. module_param_named(cls3_error, cls3_error, uint, 0);
  87. MODULE_PARM_DESC(cls3_error, "Enable FC Class 3 Error Recovery. "
  88. "[Default=" __stringify(IBMVFC_CLS3_ERROR) "]");
  89. static const struct {
  90. u16 status;
  91. u16 error;
  92. u8 result;
  93. u8 retry;
  94. int log;
  95. char *name;
  96. } cmd_status [] = {
  97. { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
  98. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
  99. { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
  100. { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
  101. { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
  102. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
  103. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
  104. { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
  105. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
  106. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
  107. { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
  108. { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
  109. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" },
  110. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
  111. { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
  112. { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
  113. { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
  114. { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
  115. { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
  116. { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
  117. { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
  118. { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
  119. { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
  120. { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
  121. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
  122. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
  123. { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
  124. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
  125. { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
  126. { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
  127. { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
  128. { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
  129. { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
  130. { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
  131. { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
  132. { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
  133. };
  134. static void ibmvfc_npiv_login(struct ibmvfc_host *);
  135. static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
  136. static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
  137. static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
  138. static void ibmvfc_npiv_logout(struct ibmvfc_host *);
  139. static const char *unknown_error = "unknown error";
  140. #ifdef CONFIG_SCSI_IBMVFC_TRACE
  141. /**
  142. * ibmvfc_trc_start - Log a start trace entry
  143. * @evt: ibmvfc event struct
  144. *
  145. **/
  146. static void ibmvfc_trc_start(struct ibmvfc_event *evt)
  147. {
  148. struct ibmvfc_host *vhost = evt->vhost;
  149. struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
  150. struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
  151. struct ibmvfc_trace_entry *entry;
  152. entry = &vhost->trace[vhost->trace_index++];
  153. entry->evt = evt;
  154. entry->time = jiffies;
  155. entry->fmt = evt->crq.format;
  156. entry->type = IBMVFC_TRC_START;
  157. switch (entry->fmt) {
  158. case IBMVFC_CMD_FORMAT:
  159. entry->op_code = vfc_cmd->iu.cdb[0];
  160. entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
  161. entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
  162. entry->tmf_flags = vfc_cmd->iu.tmf_flags;
  163. entry->u.start.xfer_len = be32_to_cpu(vfc_cmd->iu.xfer_len);
  164. break;
  165. case IBMVFC_MAD_FORMAT:
  166. entry->op_code = be32_to_cpu(mad->opcode);
  167. break;
  168. default:
  169. break;
  170. };
  171. }
  172. /**
  173. * ibmvfc_trc_end - Log an end trace entry
  174. * @evt: ibmvfc event struct
  175. *
  176. **/
  177. static void ibmvfc_trc_end(struct ibmvfc_event *evt)
  178. {
  179. struct ibmvfc_host *vhost = evt->vhost;
  180. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  181. struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
  182. struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
  183. entry->evt = evt;
  184. entry->time = jiffies;
  185. entry->fmt = evt->crq.format;
  186. entry->type = IBMVFC_TRC_END;
  187. switch (entry->fmt) {
  188. case IBMVFC_CMD_FORMAT:
  189. entry->op_code = vfc_cmd->iu.cdb[0];
  190. entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
  191. entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
  192. entry->tmf_flags = vfc_cmd->iu.tmf_flags;
  193. entry->u.end.status = be16_to_cpu(vfc_cmd->status);
  194. entry->u.end.error = be16_to_cpu(vfc_cmd->error);
  195. entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
  196. entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
  197. entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
  198. break;
  199. case IBMVFC_MAD_FORMAT:
  200. entry->op_code = be32_to_cpu(mad->opcode);
  201. entry->u.end.status = be16_to_cpu(mad->status);
  202. break;
  203. default:
  204. break;
  205. };
  206. }
  207. #else
  208. #define ibmvfc_trc_start(evt) do { } while (0)
  209. #define ibmvfc_trc_end(evt) do { } while (0)
  210. #endif
  211. /**
  212. * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
  213. * @status: status / error class
  214. * @error: error
  215. *
  216. * Return value:
  217. * index into cmd_status / -EINVAL on failure
  218. **/
  219. static int ibmvfc_get_err_index(u16 status, u16 error)
  220. {
  221. int i;
  222. for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
  223. if ((cmd_status[i].status & status) == cmd_status[i].status &&
  224. cmd_status[i].error == error)
  225. return i;
  226. return -EINVAL;
  227. }
  228. /**
  229. * ibmvfc_get_cmd_error - Find the error description for the fcp response
  230. * @status: status / error class
  231. * @error: error
  232. *
  233. * Return value:
  234. * error description string
  235. **/
  236. static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
  237. {
  238. int rc = ibmvfc_get_err_index(status, error);
  239. if (rc >= 0)
  240. return cmd_status[rc].name;
  241. return unknown_error;
  242. }
  243. /**
  244. * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
  245. * @vfc_cmd: ibmvfc command struct
  246. *
  247. * Return value:
  248. * SCSI result value to return for completed command
  249. **/
  250. static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
  251. {
  252. int err;
  253. struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
  254. int fc_rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
  255. if ((rsp->flags & FCP_RSP_LEN_VALID) &&
  256. ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
  257. rsp->data.info.rsp_code))
  258. return DID_ERROR << 16;
  259. err = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
  260. if (err >= 0)
  261. return rsp->scsi_status | (cmd_status[err].result << 16);
  262. return rsp->scsi_status | (DID_ERROR << 16);
  263. }
  264. /**
  265. * ibmvfc_retry_cmd - Determine if error status is retryable
  266. * @status: status / error class
  267. * @error: error
  268. *
  269. * Return value:
  270. * 1 if error should be retried / 0 if it should not
  271. **/
  272. static int ibmvfc_retry_cmd(u16 status, u16 error)
  273. {
  274. int rc = ibmvfc_get_err_index(status, error);
  275. if (rc >= 0)
  276. return cmd_status[rc].retry;
  277. return 1;
  278. }
  279. static const char *unknown_fc_explain = "unknown fc explain";
  280. static const struct {
  281. u16 fc_explain;
  282. char *name;
  283. } ls_explain [] = {
  284. { 0x00, "no additional explanation" },
  285. { 0x01, "service parameter error - options" },
  286. { 0x03, "service parameter error - initiator control" },
  287. { 0x05, "service parameter error - recipient control" },
  288. { 0x07, "service parameter error - received data field size" },
  289. { 0x09, "service parameter error - concurrent seq" },
  290. { 0x0B, "service parameter error - credit" },
  291. { 0x0D, "invalid N_Port/F_Port_Name" },
  292. { 0x0E, "invalid node/Fabric Name" },
  293. { 0x0F, "invalid common service parameters" },
  294. { 0x11, "invalid association header" },
  295. { 0x13, "association header required" },
  296. { 0x15, "invalid originator S_ID" },
  297. { 0x17, "invalid OX_ID-RX-ID combination" },
  298. { 0x19, "command (request) already in progress" },
  299. { 0x1E, "N_Port Login requested" },
  300. { 0x1F, "Invalid N_Port_ID" },
  301. };
  302. static const struct {
  303. u16 fc_explain;
  304. char *name;
  305. } gs_explain [] = {
  306. { 0x00, "no additional explanation" },
  307. { 0x01, "port identifier not registered" },
  308. { 0x02, "port name not registered" },
  309. { 0x03, "node name not registered" },
  310. { 0x04, "class of service not registered" },
  311. { 0x06, "initial process associator not registered" },
  312. { 0x07, "FC-4 TYPEs not registered" },
  313. { 0x08, "symbolic port name not registered" },
  314. { 0x09, "symbolic node name not registered" },
  315. { 0x0A, "port type not registered" },
  316. { 0xF0, "authorization exception" },
  317. { 0xF1, "authentication exception" },
  318. { 0xF2, "data base full" },
  319. { 0xF3, "data base empty" },
  320. { 0xF4, "processing request" },
  321. { 0xF5, "unable to verify connection" },
  322. { 0xF6, "devices not in a common zone" },
  323. };
  324. /**
  325. * ibmvfc_get_ls_explain - Return the FC Explain description text
  326. * @status: FC Explain status
  327. *
  328. * Returns:
  329. * error string
  330. **/
  331. static const char *ibmvfc_get_ls_explain(u16 status)
  332. {
  333. int i;
  334. for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
  335. if (ls_explain[i].fc_explain == status)
  336. return ls_explain[i].name;
  337. return unknown_fc_explain;
  338. }
  339. /**
  340. * ibmvfc_get_gs_explain - Return the FC Explain description text
  341. * @status: FC Explain status
  342. *
  343. * Returns:
  344. * error string
  345. **/
  346. static const char *ibmvfc_get_gs_explain(u16 status)
  347. {
  348. int i;
  349. for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
  350. if (gs_explain[i].fc_explain == status)
  351. return gs_explain[i].name;
  352. return unknown_fc_explain;
  353. }
  354. static const struct {
  355. enum ibmvfc_fc_type fc_type;
  356. char *name;
  357. } fc_type [] = {
  358. { IBMVFC_FABRIC_REJECT, "fabric reject" },
  359. { IBMVFC_PORT_REJECT, "port reject" },
  360. { IBMVFC_LS_REJECT, "ELS reject" },
  361. { IBMVFC_FABRIC_BUSY, "fabric busy" },
  362. { IBMVFC_PORT_BUSY, "port busy" },
  363. { IBMVFC_BASIC_REJECT, "basic reject" },
  364. };
  365. static const char *unknown_fc_type = "unknown fc type";
  366. /**
  367. * ibmvfc_get_fc_type - Return the FC Type description text
  368. * @status: FC Type error status
  369. *
  370. * Returns:
  371. * error string
  372. **/
  373. static const char *ibmvfc_get_fc_type(u16 status)
  374. {
  375. int i;
  376. for (i = 0; i < ARRAY_SIZE(fc_type); i++)
  377. if (fc_type[i].fc_type == status)
  378. return fc_type[i].name;
  379. return unknown_fc_type;
  380. }
  381. /**
  382. * ibmvfc_set_tgt_action - Set the next init action for the target
  383. * @tgt: ibmvfc target struct
  384. * @action: action to perform
  385. *
  386. **/
  387. static void ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
  388. enum ibmvfc_target_action action)
  389. {
  390. switch (tgt->action) {
  391. case IBMVFC_TGT_ACTION_DEL_RPORT:
  392. if (action == IBMVFC_TGT_ACTION_DELETED_RPORT)
  393. tgt->action = action;
  394. case IBMVFC_TGT_ACTION_DELETED_RPORT:
  395. break;
  396. default:
  397. if (action == IBMVFC_TGT_ACTION_DEL_RPORT)
  398. tgt->add_rport = 0;
  399. tgt->action = action;
  400. break;
  401. }
  402. }
  403. /**
  404. * ibmvfc_set_host_state - Set the state for the host
  405. * @vhost: ibmvfc host struct
  406. * @state: state to set host to
  407. *
  408. * Returns:
  409. * 0 if state changed / non-zero if not changed
  410. **/
  411. static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
  412. enum ibmvfc_host_state state)
  413. {
  414. int rc = 0;
  415. switch (vhost->state) {
  416. case IBMVFC_HOST_OFFLINE:
  417. rc = -EINVAL;
  418. break;
  419. default:
  420. vhost->state = state;
  421. break;
  422. };
  423. return rc;
  424. }
  425. /**
  426. * ibmvfc_set_host_action - Set the next init action for the host
  427. * @vhost: ibmvfc host struct
  428. * @action: action to perform
  429. *
  430. **/
  431. static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
  432. enum ibmvfc_host_action action)
  433. {
  434. switch (action) {
  435. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  436. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
  437. vhost->action = action;
  438. break;
  439. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  440. if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
  441. vhost->action = action;
  442. break;
  443. case IBMVFC_HOST_ACTION_INIT_WAIT:
  444. if (vhost->action == IBMVFC_HOST_ACTION_INIT)
  445. vhost->action = action;
  446. break;
  447. case IBMVFC_HOST_ACTION_QUERY:
  448. switch (vhost->action) {
  449. case IBMVFC_HOST_ACTION_INIT_WAIT:
  450. case IBMVFC_HOST_ACTION_NONE:
  451. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  452. vhost->action = action;
  453. break;
  454. default:
  455. break;
  456. };
  457. break;
  458. case IBMVFC_HOST_ACTION_TGT_INIT:
  459. if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
  460. vhost->action = action;
  461. break;
  462. case IBMVFC_HOST_ACTION_INIT:
  463. case IBMVFC_HOST_ACTION_TGT_DEL:
  464. switch (vhost->action) {
  465. case IBMVFC_HOST_ACTION_RESET:
  466. case IBMVFC_HOST_ACTION_REENABLE:
  467. break;
  468. default:
  469. vhost->action = action;
  470. break;
  471. };
  472. break;
  473. case IBMVFC_HOST_ACTION_LOGO:
  474. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  475. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  476. case IBMVFC_HOST_ACTION_NONE:
  477. case IBMVFC_HOST_ACTION_RESET:
  478. case IBMVFC_HOST_ACTION_REENABLE:
  479. default:
  480. vhost->action = action;
  481. break;
  482. };
  483. }
  484. /**
  485. * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
  486. * @vhost: ibmvfc host struct
  487. *
  488. * Return value:
  489. * nothing
  490. **/
  491. static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
  492. {
  493. if (vhost->action == IBMVFC_HOST_ACTION_NONE) {
  494. if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  495. scsi_block_requests(vhost->host);
  496. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  497. }
  498. } else
  499. vhost->reinit = 1;
  500. wake_up(&vhost->work_wait_q);
  501. }
  502. /**
  503. * ibmvfc_link_down - Handle a link down event from the adapter
  504. * @vhost: ibmvfc host struct
  505. * @state: ibmvfc host state to enter
  506. *
  507. **/
  508. static void ibmvfc_link_down(struct ibmvfc_host *vhost,
  509. enum ibmvfc_host_state state)
  510. {
  511. struct ibmvfc_target *tgt;
  512. ENTER;
  513. scsi_block_requests(vhost->host);
  514. list_for_each_entry(tgt, &vhost->targets, queue)
  515. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  516. ibmvfc_set_host_state(vhost, state);
  517. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
  518. vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
  519. wake_up(&vhost->work_wait_q);
  520. LEAVE;
  521. }
  522. /**
  523. * ibmvfc_init_host - Start host initialization
  524. * @vhost: ibmvfc host struct
  525. *
  526. * Return value:
  527. * nothing
  528. **/
  529. static void ibmvfc_init_host(struct ibmvfc_host *vhost)
  530. {
  531. struct ibmvfc_target *tgt;
  532. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
  533. if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
  534. dev_err(vhost->dev,
  535. "Host initialization retries exceeded. Taking adapter offline\n");
  536. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  537. return;
  538. }
  539. }
  540. if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  541. memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
  542. vhost->async_crq.cur = 0;
  543. list_for_each_entry(tgt, &vhost->targets, queue)
  544. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  545. scsi_block_requests(vhost->host);
  546. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  547. vhost->job_step = ibmvfc_npiv_login;
  548. wake_up(&vhost->work_wait_q);
  549. }
  550. }
  551. /**
  552. * ibmvfc_send_crq - Send a CRQ
  553. * @vhost: ibmvfc host struct
  554. * @word1: the first 64 bits of the data
  555. * @word2: the second 64 bits of the data
  556. *
  557. * Return value:
  558. * 0 on success / other on failure
  559. **/
  560. static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
  561. {
  562. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  563. return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
  564. }
  565. /**
  566. * ibmvfc_send_crq_init - Send a CRQ init message
  567. * @vhost: ibmvfc host struct
  568. *
  569. * Return value:
  570. * 0 on success / other on failure
  571. **/
  572. static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
  573. {
  574. ibmvfc_dbg(vhost, "Sending CRQ init\n");
  575. return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
  576. }
  577. /**
  578. * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
  579. * @vhost: ibmvfc host struct
  580. *
  581. * Return value:
  582. * 0 on success / other on failure
  583. **/
  584. static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
  585. {
  586. ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
  587. return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
  588. }
  589. /**
  590. * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
  591. * @vhost: ibmvfc host struct
  592. *
  593. * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
  594. * the crq with the hypervisor.
  595. **/
  596. static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
  597. {
  598. long rc = 0;
  599. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  600. struct ibmvfc_crq_queue *crq = &vhost->crq;
  601. ibmvfc_dbg(vhost, "Releasing CRQ\n");
  602. free_irq(vdev->irq, vhost);
  603. tasklet_kill(&vhost->tasklet);
  604. do {
  605. if (rc)
  606. msleep(100);
  607. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  608. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  609. vhost->state = IBMVFC_NO_CRQ;
  610. vhost->logged_in = 0;
  611. dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
  612. free_page((unsigned long)crq->msgs);
  613. }
  614. /**
  615. * ibmvfc_reenable_crq_queue - reenables the CRQ
  616. * @vhost: ibmvfc host struct
  617. *
  618. * Return value:
  619. * 0 on success / other on failure
  620. **/
  621. static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
  622. {
  623. int rc = 0;
  624. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  625. /* Re-enable the CRQ */
  626. do {
  627. if (rc)
  628. msleep(100);
  629. rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
  630. } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
  631. if (rc)
  632. dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
  633. return rc;
  634. }
  635. /**
  636. * ibmvfc_reset_crq - resets a crq after a failure
  637. * @vhost: ibmvfc host struct
  638. *
  639. * Return value:
  640. * 0 on success / other on failure
  641. **/
  642. static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
  643. {
  644. int rc = 0;
  645. unsigned long flags;
  646. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  647. struct ibmvfc_crq_queue *crq = &vhost->crq;
  648. /* Close the CRQ */
  649. do {
  650. if (rc)
  651. msleep(100);
  652. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  653. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  654. spin_lock_irqsave(vhost->host->host_lock, flags);
  655. vhost->state = IBMVFC_NO_CRQ;
  656. vhost->logged_in = 0;
  657. /* Clean out the queue */
  658. memset(crq->msgs, 0, PAGE_SIZE);
  659. crq->cur = 0;
  660. /* And re-open it again */
  661. rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
  662. crq->msg_token, PAGE_SIZE);
  663. if (rc == H_CLOSED)
  664. /* Adapter is good, but other end is not ready */
  665. dev_warn(vhost->dev, "Partner adapter not ready\n");
  666. else if (rc != 0)
  667. dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
  668. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  669. return rc;
  670. }
  671. /**
  672. * ibmvfc_valid_event - Determines if event is valid.
  673. * @pool: event_pool that contains the event
  674. * @evt: ibmvfc event to be checked for validity
  675. *
  676. * Return value:
  677. * 1 if event is valid / 0 if event is not valid
  678. **/
  679. static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
  680. struct ibmvfc_event *evt)
  681. {
  682. int index = evt - pool->events;
  683. if (index < 0 || index >= pool->size) /* outside of bounds */
  684. return 0;
  685. if (evt != pool->events + index) /* unaligned */
  686. return 0;
  687. return 1;
  688. }
  689. /**
  690. * ibmvfc_free_event - Free the specified event
  691. * @evt: ibmvfc_event to be freed
  692. *
  693. **/
  694. static void ibmvfc_free_event(struct ibmvfc_event *evt)
  695. {
  696. struct ibmvfc_host *vhost = evt->vhost;
  697. struct ibmvfc_event_pool *pool = &vhost->pool;
  698. BUG_ON(!ibmvfc_valid_event(pool, evt));
  699. BUG_ON(atomic_inc_return(&evt->free) != 1);
  700. list_add_tail(&evt->queue, &vhost->free);
  701. }
  702. /**
  703. * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
  704. * @evt: ibmvfc event struct
  705. *
  706. * This function does not setup any error status, that must be done
  707. * before this function gets called.
  708. **/
  709. static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
  710. {
  711. struct scsi_cmnd *cmnd = evt->cmnd;
  712. if (cmnd) {
  713. scsi_dma_unmap(cmnd);
  714. cmnd->scsi_done(cmnd);
  715. }
  716. if (evt->eh_comp)
  717. complete(evt->eh_comp);
  718. ibmvfc_free_event(evt);
  719. }
  720. /**
  721. * ibmvfc_fail_request - Fail request with specified error code
  722. * @evt: ibmvfc event struct
  723. * @error_code: error code to fail request with
  724. *
  725. * Return value:
  726. * none
  727. **/
  728. static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
  729. {
  730. if (evt->cmnd) {
  731. evt->cmnd->result = (error_code << 16);
  732. evt->done = ibmvfc_scsi_eh_done;
  733. } else
  734. evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED);
  735. list_del(&evt->queue);
  736. del_timer(&evt->timer);
  737. ibmvfc_trc_end(evt);
  738. evt->done(evt);
  739. }
  740. /**
  741. * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
  742. * @vhost: ibmvfc host struct
  743. * @error_code: error code to fail requests with
  744. *
  745. * Return value:
  746. * none
  747. **/
  748. static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
  749. {
  750. struct ibmvfc_event *evt, *pos;
  751. ibmvfc_dbg(vhost, "Purging all requests\n");
  752. list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
  753. ibmvfc_fail_request(evt, error_code);
  754. }
  755. /**
  756. * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
  757. * @vhost: struct ibmvfc host to reset
  758. **/
  759. static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
  760. {
  761. ibmvfc_purge_requests(vhost, DID_ERROR);
  762. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  763. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
  764. }
  765. /**
  766. * __ibmvfc_reset_host - Reset the connection to the server (no locking)
  767. * @vhost: struct ibmvfc host to reset
  768. **/
  769. static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
  770. {
  771. if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
  772. !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  773. scsi_block_requests(vhost->host);
  774. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
  775. vhost->job_step = ibmvfc_npiv_logout;
  776. wake_up(&vhost->work_wait_q);
  777. } else
  778. ibmvfc_hard_reset_host(vhost);
  779. }
  780. /**
  781. * ibmvfc_reset_host - Reset the connection to the server
  782. * @vhost: ibmvfc host struct
  783. **/
  784. static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
  785. {
  786. unsigned long flags;
  787. spin_lock_irqsave(vhost->host->host_lock, flags);
  788. __ibmvfc_reset_host(vhost);
  789. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  790. }
  791. /**
  792. * ibmvfc_retry_host_init - Retry host initialization if allowed
  793. * @vhost: ibmvfc host struct
  794. *
  795. * Returns: 1 if init will be retried / 0 if not
  796. *
  797. **/
  798. static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
  799. {
  800. int retry = 0;
  801. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
  802. vhost->delay_init = 1;
  803. if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
  804. dev_err(vhost->dev,
  805. "Host initialization retries exceeded. Taking adapter offline\n");
  806. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  807. } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
  808. __ibmvfc_reset_host(vhost);
  809. else {
  810. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  811. retry = 1;
  812. }
  813. }
  814. wake_up(&vhost->work_wait_q);
  815. return retry;
  816. }
  817. /**
  818. * __ibmvfc_get_target - Find the specified scsi_target (no locking)
  819. * @starget: scsi target struct
  820. *
  821. * Return value:
  822. * ibmvfc_target struct / NULL if not found
  823. **/
  824. static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
  825. {
  826. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  827. struct ibmvfc_host *vhost = shost_priv(shost);
  828. struct ibmvfc_target *tgt;
  829. list_for_each_entry(tgt, &vhost->targets, queue)
  830. if (tgt->target_id == starget->id) {
  831. kref_get(&tgt->kref);
  832. return tgt;
  833. }
  834. return NULL;
  835. }
  836. /**
  837. * ibmvfc_get_target - Find the specified scsi_target
  838. * @starget: scsi target struct
  839. *
  840. * Return value:
  841. * ibmvfc_target struct / NULL if not found
  842. **/
  843. static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
  844. {
  845. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  846. struct ibmvfc_target *tgt;
  847. unsigned long flags;
  848. spin_lock_irqsave(shost->host_lock, flags);
  849. tgt = __ibmvfc_get_target(starget);
  850. spin_unlock_irqrestore(shost->host_lock, flags);
  851. return tgt;
  852. }
  853. /**
  854. * ibmvfc_get_host_speed - Get host port speed
  855. * @shost: scsi host struct
  856. *
  857. * Return value:
  858. * none
  859. **/
  860. static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
  861. {
  862. struct ibmvfc_host *vhost = shost_priv(shost);
  863. unsigned long flags;
  864. spin_lock_irqsave(shost->host_lock, flags);
  865. if (vhost->state == IBMVFC_ACTIVE) {
  866. switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) {
  867. case 1:
  868. fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
  869. break;
  870. case 2:
  871. fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
  872. break;
  873. case 4:
  874. fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
  875. break;
  876. case 8:
  877. fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
  878. break;
  879. case 10:
  880. fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
  881. break;
  882. case 16:
  883. fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
  884. break;
  885. default:
  886. ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
  887. be64_to_cpu(vhost->login_buf->resp.link_speed) / 100);
  888. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  889. break;
  890. }
  891. } else
  892. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  893. spin_unlock_irqrestore(shost->host_lock, flags);
  894. }
  895. /**
  896. * ibmvfc_get_host_port_state - Get host port state
  897. * @shost: scsi host struct
  898. *
  899. * Return value:
  900. * none
  901. **/
  902. static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
  903. {
  904. struct ibmvfc_host *vhost = shost_priv(shost);
  905. unsigned long flags;
  906. spin_lock_irqsave(shost->host_lock, flags);
  907. switch (vhost->state) {
  908. case IBMVFC_INITIALIZING:
  909. case IBMVFC_ACTIVE:
  910. fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
  911. break;
  912. case IBMVFC_LINK_DOWN:
  913. fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
  914. break;
  915. case IBMVFC_LINK_DEAD:
  916. case IBMVFC_HOST_OFFLINE:
  917. fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
  918. break;
  919. case IBMVFC_HALTED:
  920. fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
  921. break;
  922. case IBMVFC_NO_CRQ:
  923. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  924. break;
  925. default:
  926. ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
  927. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  928. break;
  929. }
  930. spin_unlock_irqrestore(shost->host_lock, flags);
  931. }
  932. /**
  933. * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
  934. * @rport: rport struct
  935. * @timeout: timeout value
  936. *
  937. * Return value:
  938. * none
  939. **/
  940. static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  941. {
  942. if (timeout)
  943. rport->dev_loss_tmo = timeout;
  944. else
  945. rport->dev_loss_tmo = 1;
  946. }
  947. /**
  948. * ibmvfc_release_tgt - Free memory allocated for a target
  949. * @kref: kref struct
  950. *
  951. **/
  952. static void ibmvfc_release_tgt(struct kref *kref)
  953. {
  954. struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
  955. kfree(tgt);
  956. }
  957. /**
  958. * ibmvfc_get_starget_node_name - Get SCSI target's node name
  959. * @starget: scsi target struct
  960. *
  961. * Return value:
  962. * none
  963. **/
  964. static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
  965. {
  966. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  967. fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
  968. if (tgt)
  969. kref_put(&tgt->kref, ibmvfc_release_tgt);
  970. }
  971. /**
  972. * ibmvfc_get_starget_port_name - Get SCSI target's port name
  973. * @starget: scsi target struct
  974. *
  975. * Return value:
  976. * none
  977. **/
  978. static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
  979. {
  980. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  981. fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
  982. if (tgt)
  983. kref_put(&tgt->kref, ibmvfc_release_tgt);
  984. }
  985. /**
  986. * ibmvfc_get_starget_port_id - Get SCSI target's port ID
  987. * @starget: scsi target struct
  988. *
  989. * Return value:
  990. * none
  991. **/
  992. static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
  993. {
  994. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  995. fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
  996. if (tgt)
  997. kref_put(&tgt->kref, ibmvfc_release_tgt);
  998. }
  999. /**
  1000. * ibmvfc_wait_while_resetting - Wait while the host resets
  1001. * @vhost: ibmvfc host struct
  1002. *
  1003. * Return value:
  1004. * 0 on success / other on failure
  1005. **/
  1006. static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
  1007. {
  1008. long timeout = wait_event_timeout(vhost->init_wait_q,
  1009. ((vhost->state == IBMVFC_ACTIVE ||
  1010. vhost->state == IBMVFC_HOST_OFFLINE ||
  1011. vhost->state == IBMVFC_LINK_DEAD) &&
  1012. vhost->action == IBMVFC_HOST_ACTION_NONE),
  1013. (init_timeout * HZ));
  1014. return timeout ? 0 : -EIO;
  1015. }
  1016. /**
  1017. * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
  1018. * @shost: scsi host struct
  1019. *
  1020. * Return value:
  1021. * 0 on success / other on failure
  1022. **/
  1023. static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
  1024. {
  1025. struct ibmvfc_host *vhost = shost_priv(shost);
  1026. dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
  1027. ibmvfc_reset_host(vhost);
  1028. return ibmvfc_wait_while_resetting(vhost);
  1029. }
  1030. /**
  1031. * ibmvfc_gather_partition_info - Gather info about the LPAR
  1032. *
  1033. * Return value:
  1034. * none
  1035. **/
  1036. static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
  1037. {
  1038. struct device_node *rootdn;
  1039. const char *name;
  1040. const unsigned int *num;
  1041. rootdn = of_find_node_by_path("/");
  1042. if (!rootdn)
  1043. return;
  1044. name = of_get_property(rootdn, "ibm,partition-name", NULL);
  1045. if (name)
  1046. strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
  1047. num = of_get_property(rootdn, "ibm,partition-no", NULL);
  1048. if (num)
  1049. vhost->partition_number = *num;
  1050. of_node_put(rootdn);
  1051. }
  1052. /**
  1053. * ibmvfc_set_login_info - Setup info for NPIV login
  1054. * @vhost: ibmvfc host struct
  1055. *
  1056. * Return value:
  1057. * none
  1058. **/
  1059. static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
  1060. {
  1061. struct ibmvfc_npiv_login *login_info = &vhost->login_info;
  1062. struct device_node *of_node = vhost->dev->of_node;
  1063. const char *location;
  1064. memset(login_info, 0, sizeof(*login_info));
  1065. login_info->ostype = cpu_to_be32(IBMVFC_OS_LINUX);
  1066. login_info->max_dma_len = cpu_to_be64(IBMVFC_MAX_SECTORS << 9);
  1067. login_info->max_payload = cpu_to_be32(sizeof(struct ibmvfc_fcp_cmd_iu));
  1068. login_info->max_response = cpu_to_be32(sizeof(struct ibmvfc_fcp_rsp));
  1069. login_info->partition_num = cpu_to_be32(vhost->partition_number);
  1070. login_info->vfc_frame_version = cpu_to_be32(1);
  1071. login_info->fcp_version = cpu_to_be16(3);
  1072. login_info->flags = cpu_to_be16(IBMVFC_FLUSH_ON_HALT);
  1073. if (vhost->client_migrated)
  1074. login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED);
  1075. login_info->max_cmds = cpu_to_be32(max_requests + IBMVFC_NUM_INTERNAL_REQ);
  1076. login_info->capabilities = cpu_to_be64(IBMVFC_CAN_MIGRATE);
  1077. login_info->async.va = cpu_to_be64(vhost->async_crq.msg_token);
  1078. login_info->async.len = cpu_to_be32(vhost->async_crq.size * sizeof(*vhost->async_crq.msgs));
  1079. strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
  1080. strncpy(login_info->device_name,
  1081. dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
  1082. location = of_get_property(of_node, "ibm,loc-code", NULL);
  1083. location = location ? location : dev_name(vhost->dev);
  1084. strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
  1085. }
  1086. /**
  1087. * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
  1088. * @vhost: ibmvfc host who owns the event pool
  1089. *
  1090. * Returns zero on success.
  1091. **/
  1092. static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
  1093. {
  1094. int i;
  1095. struct ibmvfc_event_pool *pool = &vhost->pool;
  1096. ENTER;
  1097. pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
  1098. pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
  1099. if (!pool->events)
  1100. return -ENOMEM;
  1101. pool->iu_storage = dma_alloc_coherent(vhost->dev,
  1102. pool->size * sizeof(*pool->iu_storage),
  1103. &pool->iu_token, 0);
  1104. if (!pool->iu_storage) {
  1105. kfree(pool->events);
  1106. return -ENOMEM;
  1107. }
  1108. for (i = 0; i < pool->size; ++i) {
  1109. struct ibmvfc_event *evt = &pool->events[i];
  1110. atomic_set(&evt->free, 1);
  1111. evt->crq.valid = 0x80;
  1112. evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
  1113. evt->xfer_iu = pool->iu_storage + i;
  1114. evt->vhost = vhost;
  1115. evt->ext_list = NULL;
  1116. list_add_tail(&evt->queue, &vhost->free);
  1117. }
  1118. LEAVE;
  1119. return 0;
  1120. }
  1121. /**
  1122. * ibmvfc_free_event_pool - Frees memory of the event pool of a host
  1123. * @vhost: ibmvfc host who owns the event pool
  1124. *
  1125. **/
  1126. static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
  1127. {
  1128. int i;
  1129. struct ibmvfc_event_pool *pool = &vhost->pool;
  1130. ENTER;
  1131. for (i = 0; i < pool->size; ++i) {
  1132. list_del(&pool->events[i].queue);
  1133. BUG_ON(atomic_read(&pool->events[i].free) != 1);
  1134. if (pool->events[i].ext_list)
  1135. dma_pool_free(vhost->sg_pool,
  1136. pool->events[i].ext_list,
  1137. pool->events[i].ext_list_token);
  1138. }
  1139. kfree(pool->events);
  1140. dma_free_coherent(vhost->dev,
  1141. pool->size * sizeof(*pool->iu_storage),
  1142. pool->iu_storage, pool->iu_token);
  1143. LEAVE;
  1144. }
  1145. /**
  1146. * ibmvfc_get_event - Gets the next free event in pool
  1147. * @vhost: ibmvfc host struct
  1148. *
  1149. * Returns a free event from the pool.
  1150. **/
  1151. static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
  1152. {
  1153. struct ibmvfc_event *evt;
  1154. BUG_ON(list_empty(&vhost->free));
  1155. evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
  1156. atomic_set(&evt->free, 0);
  1157. list_del(&evt->queue);
  1158. return evt;
  1159. }
  1160. /**
  1161. * ibmvfc_init_event - Initialize fields in an event struct that are always
  1162. * required.
  1163. * @evt: The event
  1164. * @done: Routine to call when the event is responded to
  1165. * @format: SRP or MAD format
  1166. **/
  1167. static void ibmvfc_init_event(struct ibmvfc_event *evt,
  1168. void (*done) (struct ibmvfc_event *), u8 format)
  1169. {
  1170. evt->cmnd = NULL;
  1171. evt->sync_iu = NULL;
  1172. evt->crq.format = format;
  1173. evt->done = done;
  1174. evt->eh_comp = NULL;
  1175. }
  1176. /**
  1177. * ibmvfc_map_sg_list - Initialize scatterlist
  1178. * @scmd: scsi command struct
  1179. * @nseg: number of scatterlist segments
  1180. * @md: memory descriptor list to initialize
  1181. **/
  1182. static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
  1183. struct srp_direct_buf *md)
  1184. {
  1185. int i;
  1186. struct scatterlist *sg;
  1187. scsi_for_each_sg(scmd, sg, nseg, i) {
  1188. md[i].va = cpu_to_be64(sg_dma_address(sg));
  1189. md[i].len = cpu_to_be32(sg_dma_len(sg));
  1190. md[i].key = 0;
  1191. }
  1192. }
  1193. /**
  1194. * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
  1195. * @scmd: Scsi_Cmnd with the scatterlist
  1196. * @evt: ibmvfc event struct
  1197. * @vfc_cmd: vfc_cmd that contains the memory descriptor
  1198. * @dev: device for which to map dma memory
  1199. *
  1200. * Returns:
  1201. * 0 on success / non-zero on failure
  1202. **/
  1203. static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
  1204. struct ibmvfc_event *evt,
  1205. struct ibmvfc_cmd *vfc_cmd, struct device *dev)
  1206. {
  1207. int sg_mapped;
  1208. struct srp_direct_buf *data = &vfc_cmd->ioba;
  1209. struct ibmvfc_host *vhost = dev_get_drvdata(dev);
  1210. if (cls3_error)
  1211. vfc_cmd->flags |= cpu_to_be16(IBMVFC_CLASS_3_ERR);
  1212. sg_mapped = scsi_dma_map(scmd);
  1213. if (!sg_mapped) {
  1214. vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
  1215. return 0;
  1216. } else if (unlikely(sg_mapped < 0)) {
  1217. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1218. scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
  1219. return sg_mapped;
  1220. }
  1221. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  1222. vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
  1223. vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
  1224. } else {
  1225. vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
  1226. vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
  1227. }
  1228. if (sg_mapped == 1) {
  1229. ibmvfc_map_sg_list(scmd, sg_mapped, data);
  1230. return 0;
  1231. }
  1232. vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
  1233. if (!evt->ext_list) {
  1234. evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
  1235. &evt->ext_list_token);
  1236. if (!evt->ext_list) {
  1237. scsi_dma_unmap(scmd);
  1238. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1239. scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
  1240. return -ENOMEM;
  1241. }
  1242. }
  1243. ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
  1244. data->va = cpu_to_be64(evt->ext_list_token);
  1245. data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
  1246. data->key = 0;
  1247. return 0;
  1248. }
  1249. /**
  1250. * ibmvfc_timeout - Internal command timeout handler
  1251. * @evt: struct ibmvfc_event that timed out
  1252. *
  1253. * Called when an internally generated command times out
  1254. **/
  1255. static void ibmvfc_timeout(struct ibmvfc_event *evt)
  1256. {
  1257. struct ibmvfc_host *vhost = evt->vhost;
  1258. dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
  1259. ibmvfc_reset_host(vhost);
  1260. }
  1261. /**
  1262. * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
  1263. * @evt: event to be sent
  1264. * @vhost: ibmvfc host struct
  1265. * @timeout: timeout in seconds - 0 means do not time command
  1266. *
  1267. * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
  1268. **/
  1269. static int ibmvfc_send_event(struct ibmvfc_event *evt,
  1270. struct ibmvfc_host *vhost, unsigned long timeout)
  1271. {
  1272. __be64 *crq_as_u64 = (__be64 *) &evt->crq;
  1273. int rc;
  1274. /* Copy the IU into the transfer area */
  1275. *evt->xfer_iu = evt->iu;
  1276. if (evt->crq.format == IBMVFC_CMD_FORMAT)
  1277. evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
  1278. else if (evt->crq.format == IBMVFC_MAD_FORMAT)
  1279. evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
  1280. else
  1281. BUG();
  1282. list_add_tail(&evt->queue, &vhost->sent);
  1283. init_timer(&evt->timer);
  1284. if (timeout) {
  1285. evt->timer.data = (unsigned long) evt;
  1286. evt->timer.expires = jiffies + (timeout * HZ);
  1287. evt->timer.function = (void (*)(unsigned long))ibmvfc_timeout;
  1288. add_timer(&evt->timer);
  1289. }
  1290. mb();
  1291. if ((rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
  1292. be64_to_cpu(crq_as_u64[1])))) {
  1293. list_del(&evt->queue);
  1294. del_timer(&evt->timer);
  1295. /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
  1296. * Firmware will send a CRQ with a transport event (0xFF) to
  1297. * tell this client what has happened to the transport. This
  1298. * will be handled in ibmvfc_handle_crq()
  1299. */
  1300. if (rc == H_CLOSED) {
  1301. if (printk_ratelimit())
  1302. dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
  1303. if (evt->cmnd)
  1304. scsi_dma_unmap(evt->cmnd);
  1305. ibmvfc_free_event(evt);
  1306. return SCSI_MLQUEUE_HOST_BUSY;
  1307. }
  1308. dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
  1309. if (evt->cmnd) {
  1310. evt->cmnd->result = DID_ERROR << 16;
  1311. evt->done = ibmvfc_scsi_eh_done;
  1312. } else
  1313. evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
  1314. evt->done(evt);
  1315. } else
  1316. ibmvfc_trc_start(evt);
  1317. return 0;
  1318. }
  1319. /**
  1320. * ibmvfc_log_error - Log an error for the failed command if appropriate
  1321. * @evt: ibmvfc event to log
  1322. *
  1323. **/
  1324. static void ibmvfc_log_error(struct ibmvfc_event *evt)
  1325. {
  1326. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  1327. struct ibmvfc_host *vhost = evt->vhost;
  1328. struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
  1329. struct scsi_cmnd *cmnd = evt->cmnd;
  1330. const char *err = unknown_error;
  1331. int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
  1332. int logerr = 0;
  1333. int rsp_code = 0;
  1334. if (index >= 0) {
  1335. logerr = cmd_status[index].log;
  1336. err = cmd_status[index].name;
  1337. }
  1338. if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
  1339. return;
  1340. if (rsp->flags & FCP_RSP_LEN_VALID)
  1341. rsp_code = rsp->data.info.rsp_code;
  1342. scmd_printk(KERN_ERR, cmnd, "Command (%02X) failed: %s (%x:%x) "
  1343. "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
  1344. cmnd->cmnd[0], err, vfc_cmd->status, vfc_cmd->error,
  1345. rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
  1346. }
  1347. /**
  1348. * ibmvfc_relogin - Log back into the specified device
  1349. * @sdev: scsi device struct
  1350. *
  1351. **/
  1352. static void ibmvfc_relogin(struct scsi_device *sdev)
  1353. {
  1354. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  1355. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1356. struct ibmvfc_target *tgt;
  1357. list_for_each_entry(tgt, &vhost->targets, queue) {
  1358. if (rport == tgt->rport) {
  1359. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  1360. break;
  1361. }
  1362. }
  1363. ibmvfc_reinit_host(vhost);
  1364. }
  1365. /**
  1366. * ibmvfc_scsi_done - Handle responses from commands
  1367. * @evt: ibmvfc event to be handled
  1368. *
  1369. * Used as a callback when sending scsi cmds.
  1370. **/
  1371. static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
  1372. {
  1373. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  1374. struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
  1375. struct scsi_cmnd *cmnd = evt->cmnd;
  1376. u32 rsp_len = 0;
  1377. u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
  1378. if (cmnd) {
  1379. if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
  1380. scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
  1381. else if (rsp->flags & FCP_RESID_UNDER)
  1382. scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
  1383. else
  1384. scsi_set_resid(cmnd, 0);
  1385. if (vfc_cmd->status) {
  1386. cmnd->result = ibmvfc_get_err_result(vfc_cmd);
  1387. if (rsp->flags & FCP_RSP_LEN_VALID)
  1388. rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
  1389. if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
  1390. sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
  1391. if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
  1392. memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
  1393. if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
  1394. (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
  1395. ibmvfc_relogin(cmnd->device);
  1396. if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
  1397. cmnd->result = (DID_ERROR << 16);
  1398. ibmvfc_log_error(evt);
  1399. }
  1400. if (!cmnd->result &&
  1401. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
  1402. cmnd->result = (DID_ERROR << 16);
  1403. scsi_dma_unmap(cmnd);
  1404. cmnd->scsi_done(cmnd);
  1405. }
  1406. if (evt->eh_comp)
  1407. complete(evt->eh_comp);
  1408. ibmvfc_free_event(evt);
  1409. }
  1410. /**
  1411. * ibmvfc_host_chkready - Check if the host can accept commands
  1412. * @vhost: struct ibmvfc host
  1413. *
  1414. * Returns:
  1415. * 1 if host can accept command / 0 if not
  1416. **/
  1417. static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
  1418. {
  1419. int result = 0;
  1420. switch (vhost->state) {
  1421. case IBMVFC_LINK_DEAD:
  1422. case IBMVFC_HOST_OFFLINE:
  1423. result = DID_NO_CONNECT << 16;
  1424. break;
  1425. case IBMVFC_NO_CRQ:
  1426. case IBMVFC_INITIALIZING:
  1427. case IBMVFC_HALTED:
  1428. case IBMVFC_LINK_DOWN:
  1429. result = DID_REQUEUE << 16;
  1430. break;
  1431. case IBMVFC_ACTIVE:
  1432. result = 0;
  1433. break;
  1434. };
  1435. return result;
  1436. }
  1437. /**
  1438. * ibmvfc_queuecommand - The queuecommand function of the scsi template
  1439. * @cmnd: struct scsi_cmnd to be executed
  1440. * @done: Callback function to be called when cmnd is completed
  1441. *
  1442. * Returns:
  1443. * 0 on success / other on failure
  1444. **/
  1445. static int ibmvfc_queuecommand_lck(struct scsi_cmnd *cmnd,
  1446. void (*done) (struct scsi_cmnd *))
  1447. {
  1448. struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
  1449. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  1450. struct ibmvfc_cmd *vfc_cmd;
  1451. struct ibmvfc_event *evt;
  1452. int rc;
  1453. if (unlikely((rc = fc_remote_port_chkready(rport))) ||
  1454. unlikely((rc = ibmvfc_host_chkready(vhost)))) {
  1455. cmnd->result = rc;
  1456. done(cmnd);
  1457. return 0;
  1458. }
  1459. cmnd->result = (DID_OK << 16);
  1460. evt = ibmvfc_get_event(vhost);
  1461. ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
  1462. evt->cmnd = cmnd;
  1463. cmnd->scsi_done = done;
  1464. vfc_cmd = &evt->iu.cmd;
  1465. memset(vfc_cmd, 0, sizeof(*vfc_cmd));
  1466. vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
  1467. vfc_cmd->resp.len = cpu_to_be32(sizeof(vfc_cmd->rsp));
  1468. vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
  1469. vfc_cmd->payload_len = cpu_to_be32(sizeof(vfc_cmd->iu));
  1470. vfc_cmd->resp_len = cpu_to_be32(sizeof(vfc_cmd->rsp));
  1471. vfc_cmd->cancel_key = cpu_to_be32((unsigned long)cmnd->device->hostdata);
  1472. vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
  1473. vfc_cmd->iu.xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
  1474. int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
  1475. memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
  1476. if (cmnd->flags & SCMD_TAGGED) {
  1477. vfc_cmd->task_tag = cpu_to_be64(cmnd->tag);
  1478. vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
  1479. }
  1480. if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
  1481. return ibmvfc_send_event(evt, vhost, 0);
  1482. ibmvfc_free_event(evt);
  1483. if (rc == -ENOMEM)
  1484. return SCSI_MLQUEUE_HOST_BUSY;
  1485. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1486. scmd_printk(KERN_ERR, cmnd,
  1487. "Failed to map DMA buffer for command. rc=%d\n", rc);
  1488. cmnd->result = DID_ERROR << 16;
  1489. done(cmnd);
  1490. return 0;
  1491. }
  1492. static DEF_SCSI_QCMD(ibmvfc_queuecommand)
  1493. /**
  1494. * ibmvfc_sync_completion - Signal that a synchronous command has completed
  1495. * @evt: ibmvfc event struct
  1496. *
  1497. **/
  1498. static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
  1499. {
  1500. /* copy the response back */
  1501. if (evt->sync_iu)
  1502. *evt->sync_iu = *evt->xfer_iu;
  1503. complete(&evt->comp);
  1504. }
  1505. /**
  1506. * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
  1507. * @evt: struct ibmvfc_event
  1508. *
  1509. **/
  1510. static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
  1511. {
  1512. struct ibmvfc_host *vhost = evt->vhost;
  1513. ibmvfc_free_event(evt);
  1514. vhost->aborting_passthru = 0;
  1515. dev_info(vhost->dev, "Passthru command cancelled\n");
  1516. }
  1517. /**
  1518. * ibmvfc_bsg_timeout - Handle a BSG timeout
  1519. * @job: struct fc_bsg_job that timed out
  1520. *
  1521. * Returns:
  1522. * 0 on success / other on failure
  1523. **/
  1524. static int ibmvfc_bsg_timeout(struct fc_bsg_job *job)
  1525. {
  1526. struct ibmvfc_host *vhost = shost_priv(job->shost);
  1527. unsigned long port_id = (unsigned long)job->dd_data;
  1528. struct ibmvfc_event *evt;
  1529. struct ibmvfc_tmf *tmf;
  1530. unsigned long flags;
  1531. int rc;
  1532. ENTER;
  1533. spin_lock_irqsave(vhost->host->host_lock, flags);
  1534. if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
  1535. __ibmvfc_reset_host(vhost);
  1536. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1537. return 0;
  1538. }
  1539. vhost->aborting_passthru = 1;
  1540. evt = ibmvfc_get_event(vhost);
  1541. ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
  1542. tmf = &evt->iu.tmf;
  1543. memset(tmf, 0, sizeof(*tmf));
  1544. tmf->common.version = cpu_to_be32(1);
  1545. tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
  1546. tmf->common.length = cpu_to_be16(sizeof(*tmf));
  1547. tmf->scsi_id = cpu_to_be64(port_id);
  1548. tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
  1549. tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
  1550. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1551. if (rc != 0) {
  1552. vhost->aborting_passthru = 0;
  1553. dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
  1554. rc = -EIO;
  1555. } else
  1556. dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
  1557. port_id);
  1558. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1559. LEAVE;
  1560. return rc;
  1561. }
  1562. /**
  1563. * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
  1564. * @vhost: struct ibmvfc_host to send command
  1565. * @port_id: port ID to send command
  1566. *
  1567. * Returns:
  1568. * 0 on success / other on failure
  1569. **/
  1570. static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
  1571. {
  1572. struct ibmvfc_port_login *plogi;
  1573. struct ibmvfc_target *tgt;
  1574. struct ibmvfc_event *evt;
  1575. union ibmvfc_iu rsp_iu;
  1576. unsigned long flags;
  1577. int rc = 0, issue_login = 1;
  1578. ENTER;
  1579. spin_lock_irqsave(vhost->host->host_lock, flags);
  1580. list_for_each_entry(tgt, &vhost->targets, queue) {
  1581. if (tgt->scsi_id == port_id) {
  1582. issue_login = 0;
  1583. break;
  1584. }
  1585. }
  1586. if (!issue_login)
  1587. goto unlock_out;
  1588. if (unlikely((rc = ibmvfc_host_chkready(vhost))))
  1589. goto unlock_out;
  1590. evt = ibmvfc_get_event(vhost);
  1591. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  1592. plogi = &evt->iu.plogi;
  1593. memset(plogi, 0, sizeof(*plogi));
  1594. plogi->common.version = cpu_to_be32(1);
  1595. plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
  1596. plogi->common.length = cpu_to_be16(sizeof(*plogi));
  1597. plogi->scsi_id = cpu_to_be64(port_id);
  1598. evt->sync_iu = &rsp_iu;
  1599. init_completion(&evt->comp);
  1600. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1601. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1602. if (rc)
  1603. return -EIO;
  1604. wait_for_completion(&evt->comp);
  1605. if (rsp_iu.plogi.common.status)
  1606. rc = -EIO;
  1607. spin_lock_irqsave(vhost->host->host_lock, flags);
  1608. ibmvfc_free_event(evt);
  1609. unlock_out:
  1610. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1611. LEAVE;
  1612. return rc;
  1613. }
  1614. /**
  1615. * ibmvfc_bsg_request - Handle a BSG request
  1616. * @job: struct fc_bsg_job to be executed
  1617. *
  1618. * Returns:
  1619. * 0 on success / other on failure
  1620. **/
  1621. static int ibmvfc_bsg_request(struct fc_bsg_job *job)
  1622. {
  1623. struct ibmvfc_host *vhost = shost_priv(job->shost);
  1624. struct fc_rport *rport = job->rport;
  1625. struct ibmvfc_passthru_mad *mad;
  1626. struct ibmvfc_event *evt;
  1627. union ibmvfc_iu rsp_iu;
  1628. unsigned long flags, port_id = -1;
  1629. unsigned int code = job->request->msgcode;
  1630. int rc = 0, req_seg, rsp_seg, issue_login = 0;
  1631. u32 fc_flags, rsp_len;
  1632. ENTER;
  1633. job->reply->reply_payload_rcv_len = 0;
  1634. if (rport)
  1635. port_id = rport->port_id;
  1636. switch (code) {
  1637. case FC_BSG_HST_ELS_NOLOGIN:
  1638. port_id = (job->request->rqst_data.h_els.port_id[0] << 16) |
  1639. (job->request->rqst_data.h_els.port_id[1] << 8) |
  1640. job->request->rqst_data.h_els.port_id[2];
  1641. case FC_BSG_RPT_ELS:
  1642. fc_flags = IBMVFC_FC_ELS;
  1643. break;
  1644. case FC_BSG_HST_CT:
  1645. issue_login = 1;
  1646. port_id = (job->request->rqst_data.h_ct.port_id[0] << 16) |
  1647. (job->request->rqst_data.h_ct.port_id[1] << 8) |
  1648. job->request->rqst_data.h_ct.port_id[2];
  1649. case FC_BSG_RPT_CT:
  1650. fc_flags = IBMVFC_FC_CT_IU;
  1651. break;
  1652. default:
  1653. return -ENOTSUPP;
  1654. };
  1655. if (port_id == -1)
  1656. return -EINVAL;
  1657. if (!mutex_trylock(&vhost->passthru_mutex))
  1658. return -EBUSY;
  1659. job->dd_data = (void *)port_id;
  1660. req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
  1661. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1662. if (!req_seg) {
  1663. mutex_unlock(&vhost->passthru_mutex);
  1664. return -ENOMEM;
  1665. }
  1666. rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
  1667. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1668. if (!rsp_seg) {
  1669. dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
  1670. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1671. mutex_unlock(&vhost->passthru_mutex);
  1672. return -ENOMEM;
  1673. }
  1674. if (req_seg > 1 || rsp_seg > 1) {
  1675. rc = -EINVAL;
  1676. goto out;
  1677. }
  1678. if (issue_login)
  1679. rc = ibmvfc_bsg_plogi(vhost, port_id);
  1680. spin_lock_irqsave(vhost->host->host_lock, flags);
  1681. if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
  1682. unlikely((rc = ibmvfc_host_chkready(vhost)))) {
  1683. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1684. goto out;
  1685. }
  1686. evt = ibmvfc_get_event(vhost);
  1687. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  1688. mad = &evt->iu.passthru;
  1689. memset(mad, 0, sizeof(*mad));
  1690. mad->common.version = cpu_to_be32(1);
  1691. mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
  1692. mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
  1693. mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
  1694. offsetof(struct ibmvfc_passthru_mad, iu));
  1695. mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
  1696. mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
  1697. mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
  1698. mad->iu.flags = cpu_to_be32(fc_flags);
  1699. mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
  1700. mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
  1701. mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
  1702. mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
  1703. mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
  1704. mad->iu.scsi_id = cpu_to_be64(port_id);
  1705. mad->iu.tag = cpu_to_be64((u64)evt);
  1706. rsp_len = be32_to_cpu(mad->iu.rsp.len);
  1707. evt->sync_iu = &rsp_iu;
  1708. init_completion(&evt->comp);
  1709. rc = ibmvfc_send_event(evt, vhost, 0);
  1710. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1711. if (rc) {
  1712. rc = -EIO;
  1713. goto out;
  1714. }
  1715. wait_for_completion(&evt->comp);
  1716. if (rsp_iu.passthru.common.status)
  1717. rc = -EIO;
  1718. else
  1719. job->reply->reply_payload_rcv_len = rsp_len;
  1720. spin_lock_irqsave(vhost->host->host_lock, flags);
  1721. ibmvfc_free_event(evt);
  1722. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1723. job->reply->result = rc;
  1724. job->job_done(job);
  1725. rc = 0;
  1726. out:
  1727. dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
  1728. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1729. dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
  1730. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1731. mutex_unlock(&vhost->passthru_mutex);
  1732. LEAVE;
  1733. return rc;
  1734. }
  1735. /**
  1736. * ibmvfc_reset_device - Reset the device with the specified reset type
  1737. * @sdev: scsi device to reset
  1738. * @type: reset type
  1739. * @desc: reset type description for log messages
  1740. *
  1741. * Returns:
  1742. * 0 on success / other on failure
  1743. **/
  1744. static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
  1745. {
  1746. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  1747. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1748. struct ibmvfc_cmd *tmf;
  1749. struct ibmvfc_event *evt = NULL;
  1750. union ibmvfc_iu rsp_iu;
  1751. struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
  1752. int rsp_rc = -EBUSY;
  1753. unsigned long flags;
  1754. int rsp_code = 0;
  1755. spin_lock_irqsave(vhost->host->host_lock, flags);
  1756. if (vhost->state == IBMVFC_ACTIVE) {
  1757. evt = ibmvfc_get_event(vhost);
  1758. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
  1759. tmf = &evt->iu.cmd;
  1760. memset(tmf, 0, sizeof(*tmf));
  1761. tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
  1762. tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
  1763. tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
  1764. tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
  1765. tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
  1766. tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
  1767. tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
  1768. int_to_scsilun(sdev->lun, &tmf->iu.lun);
  1769. tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
  1770. tmf->iu.tmf_flags = type;
  1771. evt->sync_iu = &rsp_iu;
  1772. init_completion(&evt->comp);
  1773. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1774. }
  1775. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1776. if (rsp_rc != 0) {
  1777. sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
  1778. desc, rsp_rc);
  1779. return -EIO;
  1780. }
  1781. sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
  1782. wait_for_completion(&evt->comp);
  1783. if (rsp_iu.cmd.status)
  1784. rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
  1785. if (rsp_code) {
  1786. if (fc_rsp->flags & FCP_RSP_LEN_VALID)
  1787. rsp_code = fc_rsp->data.info.rsp_code;
  1788. sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
  1789. "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
  1790. ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
  1791. rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
  1792. fc_rsp->scsi_status);
  1793. rsp_rc = -EIO;
  1794. } else
  1795. sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
  1796. spin_lock_irqsave(vhost->host->host_lock, flags);
  1797. ibmvfc_free_event(evt);
  1798. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1799. return rsp_rc;
  1800. }
  1801. /**
  1802. * ibmvfc_match_rport - Match function for specified remote port
  1803. * @evt: ibmvfc event struct
  1804. * @device: device to match (rport)
  1805. *
  1806. * Returns:
  1807. * 1 if event matches rport / 0 if event does not match rport
  1808. **/
  1809. static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
  1810. {
  1811. struct fc_rport *cmd_rport;
  1812. if (evt->cmnd) {
  1813. cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
  1814. if (cmd_rport == rport)
  1815. return 1;
  1816. }
  1817. return 0;
  1818. }
  1819. /**
  1820. * ibmvfc_match_target - Match function for specified target
  1821. * @evt: ibmvfc event struct
  1822. * @device: device to match (starget)
  1823. *
  1824. * Returns:
  1825. * 1 if event matches starget / 0 if event does not match starget
  1826. **/
  1827. static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
  1828. {
  1829. if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
  1830. return 1;
  1831. return 0;
  1832. }
  1833. /**
  1834. * ibmvfc_match_lun - Match function for specified LUN
  1835. * @evt: ibmvfc event struct
  1836. * @device: device to match (sdev)
  1837. *
  1838. * Returns:
  1839. * 1 if event matches sdev / 0 if event does not match sdev
  1840. **/
  1841. static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
  1842. {
  1843. if (evt->cmnd && evt->cmnd->device == device)
  1844. return 1;
  1845. return 0;
  1846. }
  1847. /**
  1848. * ibmvfc_wait_for_ops - Wait for ops to complete
  1849. * @vhost: ibmvfc host struct
  1850. * @device: device to match (starget or sdev)
  1851. * @match: match function
  1852. *
  1853. * Returns:
  1854. * SUCCESS / FAILED
  1855. **/
  1856. static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
  1857. int (*match) (struct ibmvfc_event *, void *))
  1858. {
  1859. struct ibmvfc_event *evt;
  1860. DECLARE_COMPLETION_ONSTACK(comp);
  1861. int wait;
  1862. unsigned long flags;
  1863. signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
  1864. ENTER;
  1865. do {
  1866. wait = 0;
  1867. spin_lock_irqsave(vhost->host->host_lock, flags);
  1868. list_for_each_entry(evt, &vhost->sent, queue) {
  1869. if (match(evt, device)) {
  1870. evt->eh_comp = &comp;
  1871. wait++;
  1872. }
  1873. }
  1874. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1875. if (wait) {
  1876. timeout = wait_for_completion_timeout(&comp, timeout);
  1877. if (!timeout) {
  1878. wait = 0;
  1879. spin_lock_irqsave(vhost->host->host_lock, flags);
  1880. list_for_each_entry(evt, &vhost->sent, queue) {
  1881. if (match(evt, device)) {
  1882. evt->eh_comp = NULL;
  1883. wait++;
  1884. }
  1885. }
  1886. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1887. if (wait)
  1888. dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
  1889. LEAVE;
  1890. return wait ? FAILED : SUCCESS;
  1891. }
  1892. }
  1893. } while (wait);
  1894. LEAVE;
  1895. return SUCCESS;
  1896. }
  1897. /**
  1898. * ibmvfc_cancel_all - Cancel all outstanding commands to the device
  1899. * @sdev: scsi device to cancel commands
  1900. * @type: type of error recovery being performed
  1901. *
  1902. * This sends a cancel to the VIOS for the specified device. This does
  1903. * NOT send any abort to the actual device. That must be done separately.
  1904. *
  1905. * Returns:
  1906. * 0 on success / other on failure
  1907. **/
  1908. static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
  1909. {
  1910. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  1911. struct scsi_target *starget = scsi_target(sdev);
  1912. struct fc_rport *rport = starget_to_rport(starget);
  1913. struct ibmvfc_tmf *tmf;
  1914. struct ibmvfc_event *evt, *found_evt;
  1915. union ibmvfc_iu rsp;
  1916. int rsp_rc = -EBUSY;
  1917. unsigned long flags;
  1918. u16 status;
  1919. ENTER;
  1920. spin_lock_irqsave(vhost->host->host_lock, flags);
  1921. found_evt = NULL;
  1922. list_for_each_entry(evt, &vhost->sent, queue) {
  1923. if (evt->cmnd && evt->cmnd->device == sdev) {
  1924. found_evt = evt;
  1925. break;
  1926. }
  1927. }
  1928. if (!found_evt) {
  1929. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1930. sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
  1931. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1932. return 0;
  1933. }
  1934. if (vhost->logged_in) {
  1935. evt = ibmvfc_get_event(vhost);
  1936. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  1937. tmf = &evt->iu.tmf;
  1938. memset(tmf, 0, sizeof(*tmf));
  1939. tmf->common.version = cpu_to_be32(1);
  1940. tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
  1941. tmf->common.length = cpu_to_be16(sizeof(*tmf));
  1942. tmf->scsi_id = cpu_to_be64(rport->port_id);
  1943. int_to_scsilun(sdev->lun, &tmf->lun);
  1944. if (!(be64_to_cpu(vhost->login_buf->resp.capabilities) & IBMVFC_CAN_SUPPRESS_ABTS))
  1945. type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
  1946. if (vhost->state == IBMVFC_ACTIVE)
  1947. tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
  1948. else
  1949. tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
  1950. tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
  1951. tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
  1952. evt->sync_iu = &rsp;
  1953. init_completion(&evt->comp);
  1954. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1955. }
  1956. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1957. if (rsp_rc != 0) {
  1958. sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
  1959. /* If failure is received, the host adapter is most likely going
  1960. through reset, return success so the caller will wait for the command
  1961. being cancelled to get returned */
  1962. return 0;
  1963. }
  1964. sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
  1965. wait_for_completion(&evt->comp);
  1966. status = be16_to_cpu(rsp.mad_common.status);
  1967. spin_lock_irqsave(vhost->host->host_lock, flags);
  1968. ibmvfc_free_event(evt);
  1969. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1970. if (status != IBMVFC_MAD_SUCCESS) {
  1971. sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
  1972. switch (status) {
  1973. case IBMVFC_MAD_DRIVER_FAILED:
  1974. case IBMVFC_MAD_CRQ_ERROR:
  1975. /* Host adapter most likely going through reset, return success to
  1976. the caller will wait for the command being cancelled to get returned */
  1977. return 0;
  1978. default:
  1979. return -EIO;
  1980. };
  1981. }
  1982. sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
  1983. return 0;
  1984. }
  1985. /**
  1986. * ibmvfc_match_key - Match function for specified cancel key
  1987. * @evt: ibmvfc event struct
  1988. * @key: cancel key to match
  1989. *
  1990. * Returns:
  1991. * 1 if event matches key / 0 if event does not match key
  1992. **/
  1993. static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
  1994. {
  1995. unsigned long cancel_key = (unsigned long)key;
  1996. if (evt->crq.format == IBMVFC_CMD_FORMAT &&
  1997. be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
  1998. return 1;
  1999. return 0;
  2000. }
  2001. /**
  2002. * ibmvfc_match_evt - Match function for specified event
  2003. * @evt: ibmvfc event struct
  2004. * @match: event to match
  2005. *
  2006. * Returns:
  2007. * 1 if event matches key / 0 if event does not match key
  2008. **/
  2009. static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
  2010. {
  2011. if (evt == match)
  2012. return 1;
  2013. return 0;
  2014. }
  2015. /**
  2016. * ibmvfc_abort_task_set - Abort outstanding commands to the device
  2017. * @sdev: scsi device to abort commands
  2018. *
  2019. * This sends an Abort Task Set to the VIOS for the specified device. This does
  2020. * NOT send any cancel to the VIOS. That must be done separately.
  2021. *
  2022. * Returns:
  2023. * 0 on success / other on failure
  2024. **/
  2025. static int ibmvfc_abort_task_set(struct scsi_device *sdev)
  2026. {
  2027. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2028. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  2029. struct ibmvfc_cmd *tmf;
  2030. struct ibmvfc_event *evt, *found_evt;
  2031. union ibmvfc_iu rsp_iu;
  2032. struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
  2033. int rc, rsp_rc = -EBUSY;
  2034. unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
  2035. int rsp_code = 0;
  2036. spin_lock_irqsave(vhost->host->host_lock, flags);
  2037. found_evt = NULL;
  2038. list_for_each_entry(evt, &vhost->sent, queue) {
  2039. if (evt->cmnd && evt->cmnd->device == sdev) {
  2040. found_evt = evt;
  2041. break;
  2042. }
  2043. }
  2044. if (!found_evt) {
  2045. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  2046. sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
  2047. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2048. return 0;
  2049. }
  2050. if (vhost->state == IBMVFC_ACTIVE) {
  2051. evt = ibmvfc_get_event(vhost);
  2052. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
  2053. tmf = &evt->iu.cmd;
  2054. memset(tmf, 0, sizeof(*tmf));
  2055. tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
  2056. tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
  2057. tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
  2058. tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
  2059. tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
  2060. tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
  2061. tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
  2062. int_to_scsilun(sdev->lun, &tmf->iu.lun);
  2063. tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
  2064. tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
  2065. evt->sync_iu = &rsp_iu;
  2066. init_completion(&evt->comp);
  2067. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  2068. }
  2069. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2070. if (rsp_rc != 0) {
  2071. sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
  2072. return -EIO;
  2073. }
  2074. sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
  2075. timeout = wait_for_completion_timeout(&evt->comp, timeout);
  2076. if (!timeout) {
  2077. rc = ibmvfc_cancel_all(sdev, 0);
  2078. if (!rc) {
  2079. rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
  2080. if (rc == SUCCESS)
  2081. rc = 0;
  2082. }
  2083. if (rc) {
  2084. sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
  2085. ibmvfc_reset_host(vhost);
  2086. rsp_rc = -EIO;
  2087. rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
  2088. if (rc == SUCCESS)
  2089. rsp_rc = 0;
  2090. rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
  2091. if (rc != SUCCESS) {
  2092. spin_lock_irqsave(vhost->host->host_lock, flags);
  2093. ibmvfc_hard_reset_host(vhost);
  2094. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2095. rsp_rc = 0;
  2096. }
  2097. goto out;
  2098. }
  2099. }
  2100. if (rsp_iu.cmd.status)
  2101. rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
  2102. if (rsp_code) {
  2103. if (fc_rsp->flags & FCP_RSP_LEN_VALID)
  2104. rsp_code = fc_rsp->data.info.rsp_code;
  2105. sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
  2106. "flags: %x fcp_rsp: %x, scsi_status: %x\n",
  2107. ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
  2108. rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
  2109. fc_rsp->scsi_status);
  2110. rsp_rc = -EIO;
  2111. } else
  2112. sdev_printk(KERN_INFO, sdev, "Abort successful\n");
  2113. out:
  2114. spin_lock_irqsave(vhost->host->host_lock, flags);
  2115. ibmvfc_free_event(evt);
  2116. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2117. return rsp_rc;
  2118. }
  2119. /**
  2120. * ibmvfc_eh_abort_handler - Abort a command
  2121. * @cmd: scsi command to abort
  2122. *
  2123. * Returns:
  2124. * SUCCESS / FAST_IO_FAIL / FAILED
  2125. **/
  2126. static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
  2127. {
  2128. struct scsi_device *sdev = cmd->device;
  2129. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2130. int cancel_rc, block_rc;
  2131. int rc = FAILED;
  2132. ENTER;
  2133. block_rc = fc_block_scsi_eh(cmd);
  2134. ibmvfc_wait_while_resetting(vhost);
  2135. if (block_rc != FAST_IO_FAIL) {
  2136. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
  2137. ibmvfc_abort_task_set(sdev);
  2138. } else
  2139. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2140. if (!cancel_rc)
  2141. rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
  2142. if (block_rc == FAST_IO_FAIL && rc != FAILED)
  2143. rc = FAST_IO_FAIL;
  2144. LEAVE;
  2145. return rc;
  2146. }
  2147. /**
  2148. * ibmvfc_eh_device_reset_handler - Reset a single LUN
  2149. * @cmd: scsi command struct
  2150. *
  2151. * Returns:
  2152. * SUCCESS / FAST_IO_FAIL / FAILED
  2153. **/
  2154. static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
  2155. {
  2156. struct scsi_device *sdev = cmd->device;
  2157. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2158. int cancel_rc, block_rc, reset_rc = 0;
  2159. int rc = FAILED;
  2160. ENTER;
  2161. block_rc = fc_block_scsi_eh(cmd);
  2162. ibmvfc_wait_while_resetting(vhost);
  2163. if (block_rc != FAST_IO_FAIL) {
  2164. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
  2165. reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
  2166. } else
  2167. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2168. if (!cancel_rc && !reset_rc)
  2169. rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
  2170. if (block_rc == FAST_IO_FAIL && rc != FAILED)
  2171. rc = FAST_IO_FAIL;
  2172. LEAVE;
  2173. return rc;
  2174. }
  2175. /**
  2176. * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
  2177. * @sdev: scsi device struct
  2178. * @data: return code
  2179. *
  2180. **/
  2181. static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
  2182. {
  2183. unsigned long *rc = data;
  2184. *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2185. }
  2186. /**
  2187. * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
  2188. * @sdev: scsi device struct
  2189. * @data: return code
  2190. *
  2191. **/
  2192. static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
  2193. {
  2194. unsigned long *rc = data;
  2195. *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
  2196. }
  2197. /**
  2198. * ibmvfc_eh_target_reset_handler - Reset the target
  2199. * @cmd: scsi command struct
  2200. *
  2201. * Returns:
  2202. * SUCCESS / FAST_IO_FAIL / FAILED
  2203. **/
  2204. static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
  2205. {
  2206. struct scsi_device *sdev = cmd->device;
  2207. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2208. struct scsi_target *starget = scsi_target(sdev);
  2209. int block_rc;
  2210. int reset_rc = 0;
  2211. int rc = FAILED;
  2212. unsigned long cancel_rc = 0;
  2213. ENTER;
  2214. block_rc = fc_block_scsi_eh(cmd);
  2215. ibmvfc_wait_while_resetting(vhost);
  2216. if (block_rc != FAST_IO_FAIL) {
  2217. starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
  2218. reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
  2219. } else
  2220. starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_noreset);
  2221. if (!cancel_rc && !reset_rc)
  2222. rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
  2223. if (block_rc == FAST_IO_FAIL && rc != FAILED)
  2224. rc = FAST_IO_FAIL;
  2225. LEAVE;
  2226. return rc;
  2227. }
  2228. /**
  2229. * ibmvfc_eh_host_reset_handler - Reset the connection to the server
  2230. * @cmd: struct scsi_cmnd having problems
  2231. *
  2232. **/
  2233. static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
  2234. {
  2235. int rc, block_rc;
  2236. struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
  2237. block_rc = fc_block_scsi_eh(cmd);
  2238. dev_err(vhost->dev, "Resetting connection due to error recovery\n");
  2239. rc = ibmvfc_issue_fc_host_lip(vhost->host);
  2240. if (block_rc == FAST_IO_FAIL)
  2241. return FAST_IO_FAIL;
  2242. return rc ? FAILED : SUCCESS;
  2243. }
  2244. /**
  2245. * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
  2246. * @rport: rport struct
  2247. *
  2248. * Return value:
  2249. * none
  2250. **/
  2251. static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
  2252. {
  2253. struct Scsi_Host *shost = rport_to_shost(rport);
  2254. struct ibmvfc_host *vhost = shost_priv(shost);
  2255. struct fc_rport *dev_rport;
  2256. struct scsi_device *sdev;
  2257. unsigned long rc;
  2258. ENTER;
  2259. shost_for_each_device(sdev, shost) {
  2260. dev_rport = starget_to_rport(scsi_target(sdev));
  2261. if (dev_rport != rport)
  2262. continue;
  2263. ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2264. }
  2265. rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
  2266. if (rc == FAILED)
  2267. ibmvfc_issue_fc_host_lip(shost);
  2268. LEAVE;
  2269. }
  2270. static const struct ibmvfc_async_desc ae_desc [] = {
  2271. { "PLOGI", IBMVFC_AE_ELS_PLOGI, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2272. { "LOGO", IBMVFC_AE_ELS_LOGO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2273. { "PRLO", IBMVFC_AE_ELS_PRLO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2274. { "N-Port SCN", IBMVFC_AE_SCN_NPORT, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2275. { "Group SCN", IBMVFC_AE_SCN_GROUP, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2276. { "Domain SCN", IBMVFC_AE_SCN_DOMAIN, IBMVFC_DEFAULT_LOG_LEVEL },
  2277. { "Fabric SCN", IBMVFC_AE_SCN_FABRIC, IBMVFC_DEFAULT_LOG_LEVEL },
  2278. { "Link Up", IBMVFC_AE_LINK_UP, IBMVFC_DEFAULT_LOG_LEVEL },
  2279. { "Link Down", IBMVFC_AE_LINK_DOWN, IBMVFC_DEFAULT_LOG_LEVEL },
  2280. { "Link Dead", IBMVFC_AE_LINK_DEAD, IBMVFC_DEFAULT_LOG_LEVEL },
  2281. { "Halt", IBMVFC_AE_HALT, IBMVFC_DEFAULT_LOG_LEVEL },
  2282. { "Resume", IBMVFC_AE_RESUME, IBMVFC_DEFAULT_LOG_LEVEL },
  2283. { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
  2284. };
  2285. static const struct ibmvfc_async_desc unknown_ae = {
  2286. "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
  2287. };
  2288. /**
  2289. * ibmvfc_get_ae_desc - Get text description for async event
  2290. * @ae: async event
  2291. *
  2292. **/
  2293. static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
  2294. {
  2295. int i;
  2296. for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
  2297. if (ae_desc[i].ae == ae)
  2298. return &ae_desc[i];
  2299. return &unknown_ae;
  2300. }
  2301. static const struct {
  2302. enum ibmvfc_ae_link_state state;
  2303. const char *desc;
  2304. } link_desc [] = {
  2305. { IBMVFC_AE_LS_LINK_UP, " link up" },
  2306. { IBMVFC_AE_LS_LINK_BOUNCED, " link bounced" },
  2307. { IBMVFC_AE_LS_LINK_DOWN, " link down" },
  2308. { IBMVFC_AE_LS_LINK_DEAD, " link dead" },
  2309. };
  2310. /**
  2311. * ibmvfc_get_link_state - Get text description for link state
  2312. * @state: link state
  2313. *
  2314. **/
  2315. static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
  2316. {
  2317. int i;
  2318. for (i = 0; i < ARRAY_SIZE(link_desc); i++)
  2319. if (link_desc[i].state == state)
  2320. return link_desc[i].desc;
  2321. return "";
  2322. }
  2323. /**
  2324. * ibmvfc_handle_async - Handle an async event from the adapter
  2325. * @crq: crq to process
  2326. * @vhost: ibmvfc host struct
  2327. *
  2328. **/
  2329. static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
  2330. struct ibmvfc_host *vhost)
  2331. {
  2332. const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
  2333. struct ibmvfc_target *tgt;
  2334. ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
  2335. " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
  2336. be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
  2337. ibmvfc_get_link_state(crq->link_state));
  2338. switch (be64_to_cpu(crq->event)) {
  2339. case IBMVFC_AE_RESUME:
  2340. switch (crq->link_state) {
  2341. case IBMVFC_AE_LS_LINK_DOWN:
  2342. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2343. break;
  2344. case IBMVFC_AE_LS_LINK_DEAD:
  2345. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2346. break;
  2347. case IBMVFC_AE_LS_LINK_UP:
  2348. case IBMVFC_AE_LS_LINK_BOUNCED:
  2349. default:
  2350. vhost->events_to_log |= IBMVFC_AE_LINKUP;
  2351. vhost->delay_init = 1;
  2352. __ibmvfc_reset_host(vhost);
  2353. break;
  2354. };
  2355. break;
  2356. case IBMVFC_AE_LINK_UP:
  2357. vhost->events_to_log |= IBMVFC_AE_LINKUP;
  2358. vhost->delay_init = 1;
  2359. __ibmvfc_reset_host(vhost);
  2360. break;
  2361. case IBMVFC_AE_SCN_FABRIC:
  2362. case IBMVFC_AE_SCN_DOMAIN:
  2363. vhost->events_to_log |= IBMVFC_AE_RSCN;
  2364. if (vhost->state < IBMVFC_HALTED) {
  2365. vhost->delay_init = 1;
  2366. __ibmvfc_reset_host(vhost);
  2367. }
  2368. break;
  2369. case IBMVFC_AE_SCN_NPORT:
  2370. case IBMVFC_AE_SCN_GROUP:
  2371. vhost->events_to_log |= IBMVFC_AE_RSCN;
  2372. ibmvfc_reinit_host(vhost);
  2373. break;
  2374. case IBMVFC_AE_ELS_LOGO:
  2375. case IBMVFC_AE_ELS_PRLO:
  2376. case IBMVFC_AE_ELS_PLOGI:
  2377. list_for_each_entry(tgt, &vhost->targets, queue) {
  2378. if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
  2379. break;
  2380. if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
  2381. continue;
  2382. if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
  2383. continue;
  2384. if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
  2385. continue;
  2386. if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
  2387. tgt->logo_rcvd = 1;
  2388. if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
  2389. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2390. ibmvfc_reinit_host(vhost);
  2391. }
  2392. }
  2393. break;
  2394. case IBMVFC_AE_LINK_DOWN:
  2395. case IBMVFC_AE_ADAPTER_FAILED:
  2396. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2397. break;
  2398. case IBMVFC_AE_LINK_DEAD:
  2399. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2400. break;
  2401. case IBMVFC_AE_HALT:
  2402. ibmvfc_link_down(vhost, IBMVFC_HALTED);
  2403. break;
  2404. default:
  2405. dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
  2406. break;
  2407. };
  2408. }
  2409. /**
  2410. * ibmvfc_handle_crq - Handles and frees received events in the CRQ
  2411. * @crq: Command/Response queue
  2412. * @vhost: ibmvfc host struct
  2413. *
  2414. **/
  2415. static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
  2416. {
  2417. long rc;
  2418. struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
  2419. switch (crq->valid) {
  2420. case IBMVFC_CRQ_INIT_RSP:
  2421. switch (crq->format) {
  2422. case IBMVFC_CRQ_INIT:
  2423. dev_info(vhost->dev, "Partner initialized\n");
  2424. /* Send back a response */
  2425. rc = ibmvfc_send_crq_init_complete(vhost);
  2426. if (rc == 0)
  2427. ibmvfc_init_host(vhost);
  2428. else
  2429. dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
  2430. break;
  2431. case IBMVFC_CRQ_INIT_COMPLETE:
  2432. dev_info(vhost->dev, "Partner initialization complete\n");
  2433. ibmvfc_init_host(vhost);
  2434. break;
  2435. default:
  2436. dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
  2437. }
  2438. return;
  2439. case IBMVFC_CRQ_XPORT_EVENT:
  2440. vhost->state = IBMVFC_NO_CRQ;
  2441. vhost->logged_in = 0;
  2442. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  2443. if (crq->format == IBMVFC_PARTITION_MIGRATED) {
  2444. /* We need to re-setup the interpartition connection */
  2445. dev_info(vhost->dev, "Re-enabling adapter\n");
  2446. vhost->client_migrated = 1;
  2447. ibmvfc_purge_requests(vhost, DID_REQUEUE);
  2448. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2449. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
  2450. } else {
  2451. dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format);
  2452. ibmvfc_purge_requests(vhost, DID_ERROR);
  2453. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2454. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
  2455. }
  2456. return;
  2457. case IBMVFC_CRQ_CMD_RSP:
  2458. break;
  2459. default:
  2460. dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
  2461. return;
  2462. }
  2463. if (crq->format == IBMVFC_ASYNC_EVENT)
  2464. return;
  2465. /* The only kind of payload CRQs we should get are responses to
  2466. * things we send. Make sure this response is to something we
  2467. * actually sent
  2468. */
  2469. if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
  2470. dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
  2471. crq->ioba);
  2472. return;
  2473. }
  2474. if (unlikely(atomic_read(&evt->free))) {
  2475. dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
  2476. crq->ioba);
  2477. return;
  2478. }
  2479. del_timer(&evt->timer);
  2480. list_del(&evt->queue);
  2481. ibmvfc_trc_end(evt);
  2482. evt->done(evt);
  2483. }
  2484. /**
  2485. * ibmvfc_scan_finished - Check if the device scan is done.
  2486. * @shost: scsi host struct
  2487. * @time: current elapsed time
  2488. *
  2489. * Returns:
  2490. * 0 if scan is not done / 1 if scan is done
  2491. **/
  2492. static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
  2493. {
  2494. unsigned long flags;
  2495. struct ibmvfc_host *vhost = shost_priv(shost);
  2496. int done = 0;
  2497. spin_lock_irqsave(shost->host_lock, flags);
  2498. if (time >= (init_timeout * HZ)) {
  2499. dev_info(vhost->dev, "Scan taking longer than %d seconds, "
  2500. "continuing initialization\n", init_timeout);
  2501. done = 1;
  2502. }
  2503. if (vhost->scan_complete)
  2504. done = 1;
  2505. spin_unlock_irqrestore(shost->host_lock, flags);
  2506. return done;
  2507. }
  2508. /**
  2509. * ibmvfc_slave_alloc - Setup the device's task set value
  2510. * @sdev: struct scsi_device device to configure
  2511. *
  2512. * Set the device's task set value so that error handling works as
  2513. * expected.
  2514. *
  2515. * Returns:
  2516. * 0 on success / -ENXIO if device does not exist
  2517. **/
  2518. static int ibmvfc_slave_alloc(struct scsi_device *sdev)
  2519. {
  2520. struct Scsi_Host *shost = sdev->host;
  2521. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  2522. struct ibmvfc_host *vhost = shost_priv(shost);
  2523. unsigned long flags = 0;
  2524. if (!rport || fc_remote_port_chkready(rport))
  2525. return -ENXIO;
  2526. spin_lock_irqsave(shost->host_lock, flags);
  2527. sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
  2528. spin_unlock_irqrestore(shost->host_lock, flags);
  2529. return 0;
  2530. }
  2531. /**
  2532. * ibmvfc_target_alloc - Setup the target's task set value
  2533. * @starget: struct scsi_target
  2534. *
  2535. * Set the target's task set value so that error handling works as
  2536. * expected.
  2537. *
  2538. * Returns:
  2539. * 0 on success / -ENXIO if device does not exist
  2540. **/
  2541. static int ibmvfc_target_alloc(struct scsi_target *starget)
  2542. {
  2543. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2544. struct ibmvfc_host *vhost = shost_priv(shost);
  2545. unsigned long flags = 0;
  2546. spin_lock_irqsave(shost->host_lock, flags);
  2547. starget->hostdata = (void *)(unsigned long)vhost->task_set++;
  2548. spin_unlock_irqrestore(shost->host_lock, flags);
  2549. return 0;
  2550. }
  2551. /**
  2552. * ibmvfc_slave_configure - Configure the device
  2553. * @sdev: struct scsi_device device to configure
  2554. *
  2555. * Enable allow_restart for a device if it is a disk. Adjust the
  2556. * queue_depth here also.
  2557. *
  2558. * Returns:
  2559. * 0
  2560. **/
  2561. static int ibmvfc_slave_configure(struct scsi_device *sdev)
  2562. {
  2563. struct Scsi_Host *shost = sdev->host;
  2564. unsigned long flags = 0;
  2565. spin_lock_irqsave(shost->host_lock, flags);
  2566. if (sdev->type == TYPE_DISK)
  2567. sdev->allow_restart = 1;
  2568. spin_unlock_irqrestore(shost->host_lock, flags);
  2569. return 0;
  2570. }
  2571. /**
  2572. * ibmvfc_change_queue_depth - Change the device's queue depth
  2573. * @sdev: scsi device struct
  2574. * @qdepth: depth to set
  2575. * @reason: calling context
  2576. *
  2577. * Return value:
  2578. * actual depth set
  2579. **/
  2580. static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
  2581. {
  2582. if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
  2583. qdepth = IBMVFC_MAX_CMDS_PER_LUN;
  2584. return scsi_change_queue_depth(sdev, qdepth);
  2585. }
  2586. static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
  2587. struct device_attribute *attr, char *buf)
  2588. {
  2589. struct Scsi_Host *shost = class_to_shost(dev);
  2590. struct ibmvfc_host *vhost = shost_priv(shost);
  2591. return snprintf(buf, PAGE_SIZE, "%s\n",
  2592. vhost->login_buf->resp.partition_name);
  2593. }
  2594. static ssize_t ibmvfc_show_host_device_name(struct device *dev,
  2595. struct device_attribute *attr, char *buf)
  2596. {
  2597. struct Scsi_Host *shost = class_to_shost(dev);
  2598. struct ibmvfc_host *vhost = shost_priv(shost);
  2599. return snprintf(buf, PAGE_SIZE, "%s\n",
  2600. vhost->login_buf->resp.device_name);
  2601. }
  2602. static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
  2603. struct device_attribute *attr, char *buf)
  2604. {
  2605. struct Scsi_Host *shost = class_to_shost(dev);
  2606. struct ibmvfc_host *vhost = shost_priv(shost);
  2607. return snprintf(buf, PAGE_SIZE, "%s\n",
  2608. vhost->login_buf->resp.port_loc_code);
  2609. }
  2610. static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
  2611. struct device_attribute *attr, char *buf)
  2612. {
  2613. struct Scsi_Host *shost = class_to_shost(dev);
  2614. struct ibmvfc_host *vhost = shost_priv(shost);
  2615. return snprintf(buf, PAGE_SIZE, "%s\n",
  2616. vhost->login_buf->resp.drc_name);
  2617. }
  2618. static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
  2619. struct device_attribute *attr, char *buf)
  2620. {
  2621. struct Scsi_Host *shost = class_to_shost(dev);
  2622. struct ibmvfc_host *vhost = shost_priv(shost);
  2623. return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
  2624. }
  2625. static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
  2626. struct device_attribute *attr, char *buf)
  2627. {
  2628. struct Scsi_Host *shost = class_to_shost(dev);
  2629. struct ibmvfc_host *vhost = shost_priv(shost);
  2630. return snprintf(buf, PAGE_SIZE, "%llx\n", vhost->login_buf->resp.capabilities);
  2631. }
  2632. /**
  2633. * ibmvfc_show_log_level - Show the adapter's error logging level
  2634. * @dev: class device struct
  2635. * @buf: buffer
  2636. *
  2637. * Return value:
  2638. * number of bytes printed to buffer
  2639. **/
  2640. static ssize_t ibmvfc_show_log_level(struct device *dev,
  2641. struct device_attribute *attr, char *buf)
  2642. {
  2643. struct Scsi_Host *shost = class_to_shost(dev);
  2644. struct ibmvfc_host *vhost = shost_priv(shost);
  2645. unsigned long flags = 0;
  2646. int len;
  2647. spin_lock_irqsave(shost->host_lock, flags);
  2648. len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
  2649. spin_unlock_irqrestore(shost->host_lock, flags);
  2650. return len;
  2651. }
  2652. /**
  2653. * ibmvfc_store_log_level - Change the adapter's error logging level
  2654. * @dev: class device struct
  2655. * @buf: buffer
  2656. *
  2657. * Return value:
  2658. * number of bytes printed to buffer
  2659. **/
  2660. static ssize_t ibmvfc_store_log_level(struct device *dev,
  2661. struct device_attribute *attr,
  2662. const char *buf, size_t count)
  2663. {
  2664. struct Scsi_Host *shost = class_to_shost(dev);
  2665. struct ibmvfc_host *vhost = shost_priv(shost);
  2666. unsigned long flags = 0;
  2667. spin_lock_irqsave(shost->host_lock, flags);
  2668. vhost->log_level = simple_strtoul(buf, NULL, 10);
  2669. spin_unlock_irqrestore(shost->host_lock, flags);
  2670. return strlen(buf);
  2671. }
  2672. static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
  2673. static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
  2674. static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
  2675. static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
  2676. static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
  2677. static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
  2678. static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
  2679. ibmvfc_show_log_level, ibmvfc_store_log_level);
  2680. #ifdef CONFIG_SCSI_IBMVFC_TRACE
  2681. /**
  2682. * ibmvfc_read_trace - Dump the adapter trace
  2683. * @filp: open sysfs file
  2684. * @kobj: kobject struct
  2685. * @bin_attr: bin_attribute struct
  2686. * @buf: buffer
  2687. * @off: offset
  2688. * @count: buffer size
  2689. *
  2690. * Return value:
  2691. * number of bytes printed to buffer
  2692. **/
  2693. static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
  2694. struct bin_attribute *bin_attr,
  2695. char *buf, loff_t off, size_t count)
  2696. {
  2697. struct device *dev = container_of(kobj, struct device, kobj);
  2698. struct Scsi_Host *shost = class_to_shost(dev);
  2699. struct ibmvfc_host *vhost = shost_priv(shost);
  2700. unsigned long flags = 0;
  2701. int size = IBMVFC_TRACE_SIZE;
  2702. char *src = (char *)vhost->trace;
  2703. if (off > size)
  2704. return 0;
  2705. if (off + count > size) {
  2706. size -= off;
  2707. count = size;
  2708. }
  2709. spin_lock_irqsave(shost->host_lock, flags);
  2710. memcpy(buf, &src[off], count);
  2711. spin_unlock_irqrestore(shost->host_lock, flags);
  2712. return count;
  2713. }
  2714. static struct bin_attribute ibmvfc_trace_attr = {
  2715. .attr = {
  2716. .name = "trace",
  2717. .mode = S_IRUGO,
  2718. },
  2719. .size = 0,
  2720. .read = ibmvfc_read_trace,
  2721. };
  2722. #endif
  2723. static struct device_attribute *ibmvfc_attrs[] = {
  2724. &dev_attr_partition_name,
  2725. &dev_attr_device_name,
  2726. &dev_attr_port_loc_code,
  2727. &dev_attr_drc_name,
  2728. &dev_attr_npiv_version,
  2729. &dev_attr_capabilities,
  2730. &dev_attr_log_level,
  2731. NULL
  2732. };
  2733. static struct scsi_host_template driver_template = {
  2734. .module = THIS_MODULE,
  2735. .name = "IBM POWER Virtual FC Adapter",
  2736. .proc_name = IBMVFC_NAME,
  2737. .queuecommand = ibmvfc_queuecommand,
  2738. .eh_abort_handler = ibmvfc_eh_abort_handler,
  2739. .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
  2740. .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
  2741. .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
  2742. .slave_alloc = ibmvfc_slave_alloc,
  2743. .slave_configure = ibmvfc_slave_configure,
  2744. .target_alloc = ibmvfc_target_alloc,
  2745. .scan_finished = ibmvfc_scan_finished,
  2746. .change_queue_depth = ibmvfc_change_queue_depth,
  2747. .cmd_per_lun = 16,
  2748. .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
  2749. .this_id = -1,
  2750. .sg_tablesize = SG_ALL,
  2751. .max_sectors = IBMVFC_MAX_SECTORS,
  2752. .use_clustering = ENABLE_CLUSTERING,
  2753. .shost_attrs = ibmvfc_attrs,
  2754. .track_queue_depth = 1,
  2755. };
  2756. /**
  2757. * ibmvfc_next_async_crq - Returns the next entry in async queue
  2758. * @vhost: ibmvfc host struct
  2759. *
  2760. * Returns:
  2761. * Pointer to next entry in queue / NULL if empty
  2762. **/
  2763. static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
  2764. {
  2765. struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
  2766. struct ibmvfc_async_crq *crq;
  2767. crq = &async_crq->msgs[async_crq->cur];
  2768. if (crq->valid & 0x80) {
  2769. if (++async_crq->cur == async_crq->size)
  2770. async_crq->cur = 0;
  2771. rmb();
  2772. } else
  2773. crq = NULL;
  2774. return crq;
  2775. }
  2776. /**
  2777. * ibmvfc_next_crq - Returns the next entry in message queue
  2778. * @vhost: ibmvfc host struct
  2779. *
  2780. * Returns:
  2781. * Pointer to next entry in queue / NULL if empty
  2782. **/
  2783. static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
  2784. {
  2785. struct ibmvfc_crq_queue *queue = &vhost->crq;
  2786. struct ibmvfc_crq *crq;
  2787. crq = &queue->msgs[queue->cur];
  2788. if (crq->valid & 0x80) {
  2789. if (++queue->cur == queue->size)
  2790. queue->cur = 0;
  2791. rmb();
  2792. } else
  2793. crq = NULL;
  2794. return crq;
  2795. }
  2796. /**
  2797. * ibmvfc_interrupt - Interrupt handler
  2798. * @irq: number of irq to handle, not used
  2799. * @dev_instance: ibmvfc_host that received interrupt
  2800. *
  2801. * Returns:
  2802. * IRQ_HANDLED
  2803. **/
  2804. static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
  2805. {
  2806. struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
  2807. unsigned long flags;
  2808. spin_lock_irqsave(vhost->host->host_lock, flags);
  2809. vio_disable_interrupts(to_vio_dev(vhost->dev));
  2810. tasklet_schedule(&vhost->tasklet);
  2811. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2812. return IRQ_HANDLED;
  2813. }
  2814. /**
  2815. * ibmvfc_tasklet - Interrupt handler tasklet
  2816. * @data: ibmvfc host struct
  2817. *
  2818. * Returns:
  2819. * Nothing
  2820. **/
  2821. static void ibmvfc_tasklet(void *data)
  2822. {
  2823. struct ibmvfc_host *vhost = data;
  2824. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  2825. struct ibmvfc_crq *crq;
  2826. struct ibmvfc_async_crq *async;
  2827. unsigned long flags;
  2828. int done = 0;
  2829. spin_lock_irqsave(vhost->host->host_lock, flags);
  2830. while (!done) {
  2831. /* Pull all the valid messages off the async CRQ */
  2832. while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
  2833. ibmvfc_handle_async(async, vhost);
  2834. async->valid = 0;
  2835. wmb();
  2836. }
  2837. /* Pull all the valid messages off the CRQ */
  2838. while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
  2839. ibmvfc_handle_crq(crq, vhost);
  2840. crq->valid = 0;
  2841. wmb();
  2842. }
  2843. vio_enable_interrupts(vdev);
  2844. if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
  2845. vio_disable_interrupts(vdev);
  2846. ibmvfc_handle_async(async, vhost);
  2847. async->valid = 0;
  2848. wmb();
  2849. } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
  2850. vio_disable_interrupts(vdev);
  2851. ibmvfc_handle_crq(crq, vhost);
  2852. crq->valid = 0;
  2853. wmb();
  2854. } else
  2855. done = 1;
  2856. }
  2857. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2858. }
  2859. /**
  2860. * ibmvfc_init_tgt - Set the next init job step for the target
  2861. * @tgt: ibmvfc target struct
  2862. * @job_step: job step to perform
  2863. *
  2864. **/
  2865. static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
  2866. void (*job_step) (struct ibmvfc_target *))
  2867. {
  2868. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
  2869. tgt->job_step = job_step;
  2870. wake_up(&tgt->vhost->work_wait_q);
  2871. }
  2872. /**
  2873. * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
  2874. * @tgt: ibmvfc target struct
  2875. * @job_step: initialization job step
  2876. *
  2877. * Returns: 1 if step will be retried / 0 if not
  2878. *
  2879. **/
  2880. static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
  2881. void (*job_step) (struct ibmvfc_target *))
  2882. {
  2883. if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
  2884. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2885. wake_up(&tgt->vhost->work_wait_q);
  2886. return 0;
  2887. } else
  2888. ibmvfc_init_tgt(tgt, job_step);
  2889. return 1;
  2890. }
  2891. /* Defined in FC-LS */
  2892. static const struct {
  2893. int code;
  2894. int retry;
  2895. int logged_in;
  2896. } prli_rsp [] = {
  2897. { 0, 1, 0 },
  2898. { 1, 0, 1 },
  2899. { 2, 1, 0 },
  2900. { 3, 1, 0 },
  2901. { 4, 0, 0 },
  2902. { 5, 0, 0 },
  2903. { 6, 0, 1 },
  2904. { 7, 0, 0 },
  2905. { 8, 1, 0 },
  2906. };
  2907. /**
  2908. * ibmvfc_get_prli_rsp - Find PRLI response index
  2909. * @flags: PRLI response flags
  2910. *
  2911. **/
  2912. static int ibmvfc_get_prli_rsp(u16 flags)
  2913. {
  2914. int i;
  2915. int code = (flags & 0x0f00) >> 8;
  2916. for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
  2917. if (prli_rsp[i].code == code)
  2918. return i;
  2919. return 0;
  2920. }
  2921. /**
  2922. * ibmvfc_tgt_prli_done - Completion handler for Process Login
  2923. * @evt: ibmvfc event struct
  2924. *
  2925. **/
  2926. static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
  2927. {
  2928. struct ibmvfc_target *tgt = evt->tgt;
  2929. struct ibmvfc_host *vhost = evt->vhost;
  2930. struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
  2931. struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
  2932. u32 status = be16_to_cpu(rsp->common.status);
  2933. int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
  2934. vhost->discovery_threads--;
  2935. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  2936. switch (status) {
  2937. case IBMVFC_MAD_SUCCESS:
  2938. tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
  2939. parms->type, parms->flags, parms->service_parms);
  2940. if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
  2941. index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
  2942. if (prli_rsp[index].logged_in) {
  2943. if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
  2944. tgt->need_login = 0;
  2945. tgt->ids.roles = 0;
  2946. if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
  2947. tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
  2948. if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
  2949. tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
  2950. tgt->add_rport = 1;
  2951. } else
  2952. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2953. } else if (prli_rsp[index].retry)
  2954. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  2955. else
  2956. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2957. } else
  2958. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2959. break;
  2960. case IBMVFC_MAD_DRIVER_FAILED:
  2961. break;
  2962. case IBMVFC_MAD_CRQ_ERROR:
  2963. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  2964. break;
  2965. case IBMVFC_MAD_FAILED:
  2966. default:
  2967. if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
  2968. be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
  2969. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  2970. else if (tgt->logo_rcvd)
  2971. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  2972. else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  2973. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  2974. else
  2975. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2976. tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
  2977. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  2978. rsp->status, rsp->error, status);
  2979. break;
  2980. };
  2981. kref_put(&tgt->kref, ibmvfc_release_tgt);
  2982. ibmvfc_free_event(evt);
  2983. wake_up(&vhost->work_wait_q);
  2984. }
  2985. /**
  2986. * ibmvfc_tgt_send_prli - Send a process login
  2987. * @tgt: ibmvfc target struct
  2988. *
  2989. **/
  2990. static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
  2991. {
  2992. struct ibmvfc_process_login *prli;
  2993. struct ibmvfc_host *vhost = tgt->vhost;
  2994. struct ibmvfc_event *evt;
  2995. if (vhost->discovery_threads >= disc_threads)
  2996. return;
  2997. kref_get(&tgt->kref);
  2998. evt = ibmvfc_get_event(vhost);
  2999. vhost->discovery_threads++;
  3000. ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
  3001. evt->tgt = tgt;
  3002. prli = &evt->iu.prli;
  3003. memset(prli, 0, sizeof(*prli));
  3004. prli->common.version = cpu_to_be32(1);
  3005. prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
  3006. prli->common.length = cpu_to_be16(sizeof(*prli));
  3007. prli->scsi_id = cpu_to_be64(tgt->scsi_id);
  3008. prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
  3009. prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
  3010. prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
  3011. prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
  3012. if (cls3_error)
  3013. prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
  3014. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3015. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3016. vhost->discovery_threads--;
  3017. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3018. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3019. } else
  3020. tgt_dbg(tgt, "Sent process login\n");
  3021. }
  3022. /**
  3023. * ibmvfc_tgt_plogi_done - Completion handler for Port Login
  3024. * @evt: ibmvfc event struct
  3025. *
  3026. **/
  3027. static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
  3028. {
  3029. struct ibmvfc_target *tgt = evt->tgt;
  3030. struct ibmvfc_host *vhost = evt->vhost;
  3031. struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
  3032. u32 status = be16_to_cpu(rsp->common.status);
  3033. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  3034. vhost->discovery_threads--;
  3035. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3036. switch (status) {
  3037. case IBMVFC_MAD_SUCCESS:
  3038. tgt_dbg(tgt, "Port Login succeeded\n");
  3039. if (tgt->ids.port_name &&
  3040. tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
  3041. vhost->reinit = 1;
  3042. tgt_dbg(tgt, "Port re-init required\n");
  3043. break;
  3044. }
  3045. tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
  3046. tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
  3047. tgt->ids.port_id = tgt->scsi_id;
  3048. memcpy(&tgt->service_parms, &rsp->service_parms,
  3049. sizeof(tgt->service_parms));
  3050. memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
  3051. sizeof(tgt->service_parms_change));
  3052. ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
  3053. break;
  3054. case IBMVFC_MAD_DRIVER_FAILED:
  3055. break;
  3056. case IBMVFC_MAD_CRQ_ERROR:
  3057. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3058. break;
  3059. case IBMVFC_MAD_FAILED:
  3060. default:
  3061. if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  3062. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3063. else
  3064. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3065. tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  3066. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)), rsp->status, rsp->error,
  3067. ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), rsp->fc_type,
  3068. ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), rsp->fc_explain, status);
  3069. break;
  3070. };
  3071. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3072. ibmvfc_free_event(evt);
  3073. wake_up(&vhost->work_wait_q);
  3074. }
  3075. /**
  3076. * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
  3077. * @tgt: ibmvfc target struct
  3078. *
  3079. **/
  3080. static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
  3081. {
  3082. struct ibmvfc_port_login *plogi;
  3083. struct ibmvfc_host *vhost = tgt->vhost;
  3084. struct ibmvfc_event *evt;
  3085. if (vhost->discovery_threads >= disc_threads)
  3086. return;
  3087. kref_get(&tgt->kref);
  3088. tgt->logo_rcvd = 0;
  3089. evt = ibmvfc_get_event(vhost);
  3090. vhost->discovery_threads++;
  3091. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3092. ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
  3093. evt->tgt = tgt;
  3094. plogi = &evt->iu.plogi;
  3095. memset(plogi, 0, sizeof(*plogi));
  3096. plogi->common.version = cpu_to_be32(1);
  3097. plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
  3098. plogi->common.length = cpu_to_be16(sizeof(*plogi));
  3099. plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
  3100. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3101. vhost->discovery_threads--;
  3102. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3103. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3104. } else
  3105. tgt_dbg(tgt, "Sent port login\n");
  3106. }
  3107. /**
  3108. * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
  3109. * @evt: ibmvfc event struct
  3110. *
  3111. **/
  3112. static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
  3113. {
  3114. struct ibmvfc_target *tgt = evt->tgt;
  3115. struct ibmvfc_host *vhost = evt->vhost;
  3116. struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
  3117. u32 status = be16_to_cpu(rsp->common.status);
  3118. vhost->discovery_threads--;
  3119. ibmvfc_free_event(evt);
  3120. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3121. switch (status) {
  3122. case IBMVFC_MAD_SUCCESS:
  3123. tgt_dbg(tgt, "Implicit Logout succeeded\n");
  3124. break;
  3125. case IBMVFC_MAD_DRIVER_FAILED:
  3126. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3127. wake_up(&vhost->work_wait_q);
  3128. return;
  3129. case IBMVFC_MAD_FAILED:
  3130. default:
  3131. tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
  3132. break;
  3133. };
  3134. if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
  3135. ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
  3136. else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
  3137. tgt->scsi_id != tgt->new_scsi_id)
  3138. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3139. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3140. wake_up(&vhost->work_wait_q);
  3141. }
  3142. /**
  3143. * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
  3144. * @tgt: ibmvfc target struct
  3145. *
  3146. **/
  3147. static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
  3148. {
  3149. struct ibmvfc_implicit_logout *mad;
  3150. struct ibmvfc_host *vhost = tgt->vhost;
  3151. struct ibmvfc_event *evt;
  3152. if (vhost->discovery_threads >= disc_threads)
  3153. return;
  3154. kref_get(&tgt->kref);
  3155. evt = ibmvfc_get_event(vhost);
  3156. vhost->discovery_threads++;
  3157. ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
  3158. evt->tgt = tgt;
  3159. mad = &evt->iu.implicit_logout;
  3160. memset(mad, 0, sizeof(*mad));
  3161. mad->common.version = cpu_to_be32(1);
  3162. mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
  3163. mad->common.length = cpu_to_be16(sizeof(*mad));
  3164. mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
  3165. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3166. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3167. vhost->discovery_threads--;
  3168. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3169. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3170. } else
  3171. tgt_dbg(tgt, "Sent Implicit Logout\n");
  3172. }
  3173. /**
  3174. * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
  3175. * @mad: ibmvfc passthru mad struct
  3176. * @tgt: ibmvfc target struct
  3177. *
  3178. * Returns:
  3179. * 1 if PLOGI needed / 0 if PLOGI not needed
  3180. **/
  3181. static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
  3182. struct ibmvfc_target *tgt)
  3183. {
  3184. if (memcmp(&mad->fc_iu.response[2], &tgt->ids.port_name,
  3185. sizeof(tgt->ids.port_name)))
  3186. return 1;
  3187. if (memcmp(&mad->fc_iu.response[4], &tgt->ids.node_name,
  3188. sizeof(tgt->ids.node_name)))
  3189. return 1;
  3190. if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
  3191. return 1;
  3192. return 0;
  3193. }
  3194. /**
  3195. * ibmvfc_tgt_adisc_done - Completion handler for ADISC
  3196. * @evt: ibmvfc event struct
  3197. *
  3198. **/
  3199. static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
  3200. {
  3201. struct ibmvfc_target *tgt = evt->tgt;
  3202. struct ibmvfc_host *vhost = evt->vhost;
  3203. struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
  3204. u32 status = be16_to_cpu(mad->common.status);
  3205. u8 fc_reason, fc_explain;
  3206. vhost->discovery_threads--;
  3207. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3208. del_timer(&tgt->timer);
  3209. switch (status) {
  3210. case IBMVFC_MAD_SUCCESS:
  3211. tgt_dbg(tgt, "ADISC succeeded\n");
  3212. if (ibmvfc_adisc_needs_plogi(mad, tgt))
  3213. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3214. break;
  3215. case IBMVFC_MAD_DRIVER_FAILED:
  3216. break;
  3217. case IBMVFC_MAD_FAILED:
  3218. default:
  3219. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3220. fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
  3221. fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
  3222. tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  3223. ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
  3224. mad->iu.status, mad->iu.error,
  3225. ibmvfc_get_fc_type(fc_reason), fc_reason,
  3226. ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
  3227. break;
  3228. };
  3229. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3230. ibmvfc_free_event(evt);
  3231. wake_up(&vhost->work_wait_q);
  3232. }
  3233. /**
  3234. * ibmvfc_init_passthru - Initialize an event struct for FC passthru
  3235. * @evt: ibmvfc event struct
  3236. *
  3237. **/
  3238. static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
  3239. {
  3240. struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
  3241. memset(mad, 0, sizeof(*mad));
  3242. mad->common.version = cpu_to_be32(1);
  3243. mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
  3244. mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
  3245. mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
  3246. offsetof(struct ibmvfc_passthru_mad, iu));
  3247. mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
  3248. mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
  3249. mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
  3250. mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
  3251. offsetof(struct ibmvfc_passthru_mad, fc_iu) +
  3252. offsetof(struct ibmvfc_passthru_fc_iu, payload));
  3253. mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
  3254. mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
  3255. offsetof(struct ibmvfc_passthru_mad, fc_iu) +
  3256. offsetof(struct ibmvfc_passthru_fc_iu, response));
  3257. mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
  3258. }
  3259. /**
  3260. * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
  3261. * @evt: ibmvfc event struct
  3262. *
  3263. * Just cleanup this event struct. Everything else is handled by
  3264. * the ADISC completion handler. If the ADISC never actually comes
  3265. * back, we still have the timer running on the ADISC event struct
  3266. * which will fire and cause the CRQ to get reset.
  3267. *
  3268. **/
  3269. static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
  3270. {
  3271. struct ibmvfc_host *vhost = evt->vhost;
  3272. struct ibmvfc_target *tgt = evt->tgt;
  3273. tgt_dbg(tgt, "ADISC cancel complete\n");
  3274. vhost->abort_threads--;
  3275. ibmvfc_free_event(evt);
  3276. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3277. wake_up(&vhost->work_wait_q);
  3278. }
  3279. /**
  3280. * ibmvfc_adisc_timeout - Handle an ADISC timeout
  3281. * @tgt: ibmvfc target struct
  3282. *
  3283. * If an ADISC times out, send a cancel. If the cancel times
  3284. * out, reset the CRQ. When the ADISC comes back as cancelled,
  3285. * log back into the target.
  3286. **/
  3287. static void ibmvfc_adisc_timeout(struct ibmvfc_target *tgt)
  3288. {
  3289. struct ibmvfc_host *vhost = tgt->vhost;
  3290. struct ibmvfc_event *evt;
  3291. struct ibmvfc_tmf *tmf;
  3292. unsigned long flags;
  3293. int rc;
  3294. tgt_dbg(tgt, "ADISC timeout\n");
  3295. spin_lock_irqsave(vhost->host->host_lock, flags);
  3296. if (vhost->abort_threads >= disc_threads ||
  3297. tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
  3298. vhost->state != IBMVFC_INITIALIZING ||
  3299. vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
  3300. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3301. return;
  3302. }
  3303. vhost->abort_threads++;
  3304. kref_get(&tgt->kref);
  3305. evt = ibmvfc_get_event(vhost);
  3306. ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
  3307. evt->tgt = tgt;
  3308. tmf = &evt->iu.tmf;
  3309. memset(tmf, 0, sizeof(*tmf));
  3310. tmf->common.version = cpu_to_be32(1);
  3311. tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
  3312. tmf->common.length = cpu_to_be16(sizeof(*tmf));
  3313. tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
  3314. tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
  3315. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  3316. if (rc) {
  3317. tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
  3318. vhost->abort_threads--;
  3319. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3320. __ibmvfc_reset_host(vhost);
  3321. } else
  3322. tgt_dbg(tgt, "Attempting to cancel ADISC\n");
  3323. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3324. }
  3325. /**
  3326. * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
  3327. * @tgt: ibmvfc target struct
  3328. *
  3329. * When sending an ADISC we end up with two timers running. The
  3330. * first timer is the timer in the ibmvfc target struct. If this
  3331. * fires, we send a cancel to the target. The second timer is the
  3332. * timer on the ibmvfc event for the ADISC, which is longer. If that
  3333. * fires, it means the ADISC timed out and our attempt to cancel it
  3334. * also failed, so we need to reset the CRQ.
  3335. **/
  3336. static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
  3337. {
  3338. struct ibmvfc_passthru_mad *mad;
  3339. struct ibmvfc_host *vhost = tgt->vhost;
  3340. struct ibmvfc_event *evt;
  3341. if (vhost->discovery_threads >= disc_threads)
  3342. return;
  3343. kref_get(&tgt->kref);
  3344. evt = ibmvfc_get_event(vhost);
  3345. vhost->discovery_threads++;
  3346. ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
  3347. evt->tgt = tgt;
  3348. ibmvfc_init_passthru(evt);
  3349. mad = &evt->iu.passthru;
  3350. mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
  3351. mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
  3352. mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
  3353. mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
  3354. memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
  3355. sizeof(vhost->login_buf->resp.port_name));
  3356. memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
  3357. sizeof(vhost->login_buf->resp.node_name));
  3358. mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
  3359. if (timer_pending(&tgt->timer))
  3360. mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
  3361. else {
  3362. tgt->timer.data = (unsigned long) tgt;
  3363. tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
  3364. tgt->timer.function = (void (*)(unsigned long))ibmvfc_adisc_timeout;
  3365. add_timer(&tgt->timer);
  3366. }
  3367. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3368. if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
  3369. vhost->discovery_threads--;
  3370. del_timer(&tgt->timer);
  3371. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3372. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3373. } else
  3374. tgt_dbg(tgt, "Sent ADISC\n");
  3375. }
  3376. /**
  3377. * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
  3378. * @evt: ibmvfc event struct
  3379. *
  3380. **/
  3381. static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
  3382. {
  3383. struct ibmvfc_target *tgt = evt->tgt;
  3384. struct ibmvfc_host *vhost = evt->vhost;
  3385. struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
  3386. u32 status = be16_to_cpu(rsp->common.status);
  3387. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  3388. vhost->discovery_threads--;
  3389. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3390. switch (status) {
  3391. case IBMVFC_MAD_SUCCESS:
  3392. tgt_dbg(tgt, "Query Target succeeded\n");
  3393. tgt->new_scsi_id = be64_to_cpu(rsp->scsi_id);
  3394. if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
  3395. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  3396. else
  3397. ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
  3398. break;
  3399. case IBMVFC_MAD_DRIVER_FAILED:
  3400. break;
  3401. case IBMVFC_MAD_CRQ_ERROR:
  3402. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
  3403. break;
  3404. case IBMVFC_MAD_FAILED:
  3405. default:
  3406. if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
  3407. be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
  3408. be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
  3409. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3410. else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  3411. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
  3412. else
  3413. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3414. tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  3415. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  3416. rsp->status, rsp->error, ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)),
  3417. rsp->fc_type, ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)),
  3418. rsp->fc_explain, status);
  3419. break;
  3420. };
  3421. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3422. ibmvfc_free_event(evt);
  3423. wake_up(&vhost->work_wait_q);
  3424. }
  3425. /**
  3426. * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
  3427. * @tgt: ibmvfc target struct
  3428. *
  3429. **/
  3430. static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
  3431. {
  3432. struct ibmvfc_query_tgt *query_tgt;
  3433. struct ibmvfc_host *vhost = tgt->vhost;
  3434. struct ibmvfc_event *evt;
  3435. if (vhost->discovery_threads >= disc_threads)
  3436. return;
  3437. kref_get(&tgt->kref);
  3438. evt = ibmvfc_get_event(vhost);
  3439. vhost->discovery_threads++;
  3440. evt->tgt = tgt;
  3441. ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
  3442. query_tgt = &evt->iu.query_tgt;
  3443. memset(query_tgt, 0, sizeof(*query_tgt));
  3444. query_tgt->common.version = cpu_to_be32(1);
  3445. query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
  3446. query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
  3447. query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
  3448. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3449. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3450. vhost->discovery_threads--;
  3451. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3452. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3453. } else
  3454. tgt_dbg(tgt, "Sent Query Target\n");
  3455. }
  3456. /**
  3457. * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
  3458. * @vhost: ibmvfc host struct
  3459. * @scsi_id: SCSI ID to allocate target for
  3460. *
  3461. * Returns:
  3462. * 0 on success / other on failure
  3463. **/
  3464. static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
  3465. {
  3466. struct ibmvfc_target *tgt;
  3467. unsigned long flags;
  3468. spin_lock_irqsave(vhost->host->host_lock, flags);
  3469. list_for_each_entry(tgt, &vhost->targets, queue) {
  3470. if (tgt->scsi_id == scsi_id) {
  3471. if (tgt->need_login)
  3472. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  3473. goto unlock_out;
  3474. }
  3475. }
  3476. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3477. tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
  3478. if (!tgt) {
  3479. dev_err(vhost->dev, "Target allocation failure for scsi id %08llx\n",
  3480. scsi_id);
  3481. return -ENOMEM;
  3482. }
  3483. memset(tgt, 0, sizeof(*tgt));
  3484. tgt->scsi_id = scsi_id;
  3485. tgt->new_scsi_id = scsi_id;
  3486. tgt->vhost = vhost;
  3487. tgt->need_login = 1;
  3488. tgt->cancel_key = vhost->task_set++;
  3489. init_timer(&tgt->timer);
  3490. kref_init(&tgt->kref);
  3491. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  3492. spin_lock_irqsave(vhost->host->host_lock, flags);
  3493. list_add_tail(&tgt->queue, &vhost->targets);
  3494. unlock_out:
  3495. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3496. return 0;
  3497. }
  3498. /**
  3499. * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
  3500. * @vhost: ibmvfc host struct
  3501. *
  3502. * Returns:
  3503. * 0 on success / other on failure
  3504. **/
  3505. static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
  3506. {
  3507. int i, rc;
  3508. for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
  3509. rc = ibmvfc_alloc_target(vhost,
  3510. be32_to_cpu(vhost->disc_buf->scsi_id[i]) &
  3511. IBMVFC_DISC_TGT_SCSI_ID_MASK);
  3512. return rc;
  3513. }
  3514. /**
  3515. * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
  3516. * @evt: ibmvfc event struct
  3517. *
  3518. **/
  3519. static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
  3520. {
  3521. struct ibmvfc_host *vhost = evt->vhost;
  3522. struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
  3523. u32 mad_status = be16_to_cpu(rsp->common.status);
  3524. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  3525. switch (mad_status) {
  3526. case IBMVFC_MAD_SUCCESS:
  3527. ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
  3528. vhost->num_targets = be32_to_cpu(rsp->num_written);
  3529. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
  3530. break;
  3531. case IBMVFC_MAD_FAILED:
  3532. level += ibmvfc_retry_host_init(vhost);
  3533. ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
  3534. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  3535. rsp->status, rsp->error);
  3536. break;
  3537. case IBMVFC_MAD_DRIVER_FAILED:
  3538. break;
  3539. default:
  3540. dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
  3541. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3542. break;
  3543. }
  3544. ibmvfc_free_event(evt);
  3545. wake_up(&vhost->work_wait_q);
  3546. }
  3547. /**
  3548. * ibmvfc_discover_targets - Send Discover Targets MAD
  3549. * @vhost: ibmvfc host struct
  3550. *
  3551. **/
  3552. static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
  3553. {
  3554. struct ibmvfc_discover_targets *mad;
  3555. struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
  3556. ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
  3557. mad = &evt->iu.discover_targets;
  3558. memset(mad, 0, sizeof(*mad));
  3559. mad->common.version = cpu_to_be32(1);
  3560. mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
  3561. mad->common.length = cpu_to_be16(sizeof(*mad));
  3562. mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
  3563. mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
  3564. mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
  3565. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  3566. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  3567. ibmvfc_dbg(vhost, "Sent discover targets\n");
  3568. else
  3569. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3570. }
  3571. /**
  3572. * ibmvfc_npiv_login_done - Completion handler for NPIV Login
  3573. * @evt: ibmvfc event struct
  3574. *
  3575. **/
  3576. static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
  3577. {
  3578. struct ibmvfc_host *vhost = evt->vhost;
  3579. u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
  3580. struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
  3581. unsigned int npiv_max_sectors;
  3582. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  3583. switch (mad_status) {
  3584. case IBMVFC_MAD_SUCCESS:
  3585. ibmvfc_free_event(evt);
  3586. break;
  3587. case IBMVFC_MAD_FAILED:
  3588. if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  3589. level += ibmvfc_retry_host_init(vhost);
  3590. else
  3591. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3592. ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
  3593. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  3594. rsp->status, rsp->error);
  3595. ibmvfc_free_event(evt);
  3596. return;
  3597. case IBMVFC_MAD_CRQ_ERROR:
  3598. ibmvfc_retry_host_init(vhost);
  3599. case IBMVFC_MAD_DRIVER_FAILED:
  3600. ibmvfc_free_event(evt);
  3601. return;
  3602. default:
  3603. dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
  3604. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3605. ibmvfc_free_event(evt);
  3606. return;
  3607. }
  3608. vhost->client_migrated = 0;
  3609. if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
  3610. dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
  3611. rsp->flags);
  3612. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3613. wake_up(&vhost->work_wait_q);
  3614. return;
  3615. }
  3616. if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
  3617. dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
  3618. rsp->max_cmds);
  3619. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3620. wake_up(&vhost->work_wait_q);
  3621. return;
  3622. }
  3623. vhost->logged_in = 1;
  3624. npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
  3625. dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
  3626. rsp->partition_name, rsp->device_name, rsp->port_loc_code,
  3627. rsp->drc_name, npiv_max_sectors);
  3628. fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
  3629. fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
  3630. fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
  3631. fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
  3632. fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
  3633. fc_host_supported_classes(vhost->host) = 0;
  3634. if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
  3635. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
  3636. if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
  3637. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
  3638. if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
  3639. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
  3640. fc_host_maxframe_size(vhost->host) =
  3641. be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
  3642. vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
  3643. vhost->host->max_sectors = npiv_max_sectors;
  3644. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  3645. wake_up(&vhost->work_wait_q);
  3646. }
  3647. /**
  3648. * ibmvfc_npiv_login - Sends NPIV login
  3649. * @vhost: ibmvfc host struct
  3650. *
  3651. **/
  3652. static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
  3653. {
  3654. struct ibmvfc_npiv_login_mad *mad;
  3655. struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
  3656. ibmvfc_gather_partition_info(vhost);
  3657. ibmvfc_set_login_info(vhost);
  3658. ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
  3659. memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
  3660. mad = &evt->iu.npiv_login;
  3661. memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
  3662. mad->common.version = cpu_to_be32(1);
  3663. mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
  3664. mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
  3665. mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
  3666. mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
  3667. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  3668. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  3669. ibmvfc_dbg(vhost, "Sent NPIV login\n");
  3670. else
  3671. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3672. };
  3673. /**
  3674. * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
  3675. * @vhost: ibmvfc host struct
  3676. *
  3677. **/
  3678. static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
  3679. {
  3680. struct ibmvfc_host *vhost = evt->vhost;
  3681. u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
  3682. ibmvfc_free_event(evt);
  3683. switch (mad_status) {
  3684. case IBMVFC_MAD_SUCCESS:
  3685. if (list_empty(&vhost->sent) &&
  3686. vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
  3687. ibmvfc_init_host(vhost);
  3688. return;
  3689. }
  3690. break;
  3691. case IBMVFC_MAD_FAILED:
  3692. case IBMVFC_MAD_NOT_SUPPORTED:
  3693. case IBMVFC_MAD_CRQ_ERROR:
  3694. case IBMVFC_MAD_DRIVER_FAILED:
  3695. default:
  3696. ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
  3697. break;
  3698. }
  3699. ibmvfc_hard_reset_host(vhost);
  3700. }
  3701. /**
  3702. * ibmvfc_npiv_logout - Issue an NPIV Logout
  3703. * @vhost: ibmvfc host struct
  3704. *
  3705. **/
  3706. static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
  3707. {
  3708. struct ibmvfc_npiv_logout_mad *mad;
  3709. struct ibmvfc_event *evt;
  3710. evt = ibmvfc_get_event(vhost);
  3711. ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
  3712. mad = &evt->iu.npiv_logout;
  3713. memset(mad, 0, sizeof(*mad));
  3714. mad->common.version = cpu_to_be32(1);
  3715. mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
  3716. mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
  3717. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
  3718. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  3719. ibmvfc_dbg(vhost, "Sent NPIV logout\n");
  3720. else
  3721. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3722. }
  3723. /**
  3724. * ibmvfc_dev_init_to_do - Is there target initialization work to do?
  3725. * @vhost: ibmvfc host struct
  3726. *
  3727. * Returns:
  3728. * 1 if work to do / 0 if not
  3729. **/
  3730. static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
  3731. {
  3732. struct ibmvfc_target *tgt;
  3733. list_for_each_entry(tgt, &vhost->targets, queue) {
  3734. if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
  3735. tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
  3736. return 1;
  3737. }
  3738. return 0;
  3739. }
  3740. /**
  3741. * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
  3742. * @vhost: ibmvfc host struct
  3743. *
  3744. * Returns:
  3745. * 1 if work to do / 0 if not
  3746. **/
  3747. static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
  3748. {
  3749. struct ibmvfc_target *tgt;
  3750. if (kthread_should_stop())
  3751. return 1;
  3752. switch (vhost->action) {
  3753. case IBMVFC_HOST_ACTION_NONE:
  3754. case IBMVFC_HOST_ACTION_INIT_WAIT:
  3755. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  3756. return 0;
  3757. case IBMVFC_HOST_ACTION_TGT_INIT:
  3758. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  3759. if (vhost->discovery_threads == disc_threads)
  3760. return 0;
  3761. list_for_each_entry(tgt, &vhost->targets, queue)
  3762. if (tgt->action == IBMVFC_TGT_ACTION_INIT)
  3763. return 1;
  3764. list_for_each_entry(tgt, &vhost->targets, queue)
  3765. if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
  3766. return 0;
  3767. return 1;
  3768. case IBMVFC_HOST_ACTION_LOGO:
  3769. case IBMVFC_HOST_ACTION_INIT:
  3770. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  3771. case IBMVFC_HOST_ACTION_TGT_DEL:
  3772. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  3773. case IBMVFC_HOST_ACTION_QUERY:
  3774. case IBMVFC_HOST_ACTION_RESET:
  3775. case IBMVFC_HOST_ACTION_REENABLE:
  3776. default:
  3777. break;
  3778. };
  3779. return 1;
  3780. }
  3781. /**
  3782. * ibmvfc_work_to_do - Is there task level work to do?
  3783. * @vhost: ibmvfc host struct
  3784. *
  3785. * Returns:
  3786. * 1 if work to do / 0 if not
  3787. **/
  3788. static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
  3789. {
  3790. unsigned long flags;
  3791. int rc;
  3792. spin_lock_irqsave(vhost->host->host_lock, flags);
  3793. rc = __ibmvfc_work_to_do(vhost);
  3794. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3795. return rc;
  3796. }
  3797. /**
  3798. * ibmvfc_log_ae - Log async events if necessary
  3799. * @vhost: ibmvfc host struct
  3800. * @events: events to log
  3801. *
  3802. **/
  3803. static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
  3804. {
  3805. if (events & IBMVFC_AE_RSCN)
  3806. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
  3807. if ((events & IBMVFC_AE_LINKDOWN) &&
  3808. vhost->state >= IBMVFC_HALTED)
  3809. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
  3810. if ((events & IBMVFC_AE_LINKUP) &&
  3811. vhost->state == IBMVFC_INITIALIZING)
  3812. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
  3813. }
  3814. /**
  3815. * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
  3816. * @tgt: ibmvfc target struct
  3817. *
  3818. **/
  3819. static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
  3820. {
  3821. struct ibmvfc_host *vhost = tgt->vhost;
  3822. struct fc_rport *rport;
  3823. unsigned long flags;
  3824. tgt_dbg(tgt, "Adding rport\n");
  3825. rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
  3826. spin_lock_irqsave(vhost->host->host_lock, flags);
  3827. if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
  3828. tgt_dbg(tgt, "Deleting rport\n");
  3829. list_del(&tgt->queue);
  3830. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
  3831. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3832. fc_remote_port_delete(rport);
  3833. del_timer_sync(&tgt->timer);
  3834. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3835. return;
  3836. } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
  3837. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3838. return;
  3839. }
  3840. if (rport) {
  3841. tgt_dbg(tgt, "rport add succeeded\n");
  3842. tgt->rport = rport;
  3843. rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
  3844. rport->supported_classes = 0;
  3845. tgt->target_id = rport->scsi_target_id;
  3846. if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
  3847. rport->supported_classes |= FC_COS_CLASS1;
  3848. if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
  3849. rport->supported_classes |= FC_COS_CLASS2;
  3850. if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
  3851. rport->supported_classes |= FC_COS_CLASS3;
  3852. if (rport->rqst_q)
  3853. blk_queue_max_segments(rport->rqst_q, 1);
  3854. } else
  3855. tgt_dbg(tgt, "rport add failed\n");
  3856. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3857. }
  3858. /**
  3859. * ibmvfc_do_work - Do task level work
  3860. * @vhost: ibmvfc host struct
  3861. *
  3862. **/
  3863. static void ibmvfc_do_work(struct ibmvfc_host *vhost)
  3864. {
  3865. struct ibmvfc_target *tgt;
  3866. unsigned long flags;
  3867. struct fc_rport *rport;
  3868. int rc;
  3869. ibmvfc_log_ae(vhost, vhost->events_to_log);
  3870. spin_lock_irqsave(vhost->host->host_lock, flags);
  3871. vhost->events_to_log = 0;
  3872. switch (vhost->action) {
  3873. case IBMVFC_HOST_ACTION_NONE:
  3874. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  3875. case IBMVFC_HOST_ACTION_INIT_WAIT:
  3876. break;
  3877. case IBMVFC_HOST_ACTION_RESET:
  3878. vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
  3879. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3880. rc = ibmvfc_reset_crq(vhost);
  3881. spin_lock_irqsave(vhost->host->host_lock, flags);
  3882. if (rc == H_CLOSED)
  3883. vio_enable_interrupts(to_vio_dev(vhost->dev));
  3884. if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
  3885. (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
  3886. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3887. dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
  3888. }
  3889. break;
  3890. case IBMVFC_HOST_ACTION_REENABLE:
  3891. vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
  3892. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3893. rc = ibmvfc_reenable_crq_queue(vhost);
  3894. spin_lock_irqsave(vhost->host->host_lock, flags);
  3895. if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
  3896. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3897. dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
  3898. }
  3899. break;
  3900. case IBMVFC_HOST_ACTION_LOGO:
  3901. vhost->job_step(vhost);
  3902. break;
  3903. case IBMVFC_HOST_ACTION_INIT:
  3904. BUG_ON(vhost->state != IBMVFC_INITIALIZING);
  3905. if (vhost->delay_init) {
  3906. vhost->delay_init = 0;
  3907. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3908. ssleep(15);
  3909. return;
  3910. } else
  3911. vhost->job_step(vhost);
  3912. break;
  3913. case IBMVFC_HOST_ACTION_QUERY:
  3914. list_for_each_entry(tgt, &vhost->targets, queue)
  3915. ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
  3916. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
  3917. break;
  3918. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  3919. list_for_each_entry(tgt, &vhost->targets, queue) {
  3920. if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
  3921. tgt->job_step(tgt);
  3922. break;
  3923. }
  3924. }
  3925. if (!ibmvfc_dev_init_to_do(vhost))
  3926. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
  3927. break;
  3928. case IBMVFC_HOST_ACTION_TGT_DEL:
  3929. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  3930. list_for_each_entry(tgt, &vhost->targets, queue) {
  3931. if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
  3932. tgt_dbg(tgt, "Deleting rport\n");
  3933. rport = tgt->rport;
  3934. tgt->rport = NULL;
  3935. list_del(&tgt->queue);
  3936. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
  3937. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3938. if (rport)
  3939. fc_remote_port_delete(rport);
  3940. del_timer_sync(&tgt->timer);
  3941. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3942. return;
  3943. }
  3944. }
  3945. if (vhost->state == IBMVFC_INITIALIZING) {
  3946. if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
  3947. if (vhost->reinit) {
  3948. vhost->reinit = 0;
  3949. scsi_block_requests(vhost->host);
  3950. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  3951. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3952. } else {
  3953. ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
  3954. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  3955. wake_up(&vhost->init_wait_q);
  3956. schedule_work(&vhost->rport_add_work_q);
  3957. vhost->init_retries = 0;
  3958. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3959. scsi_unblock_requests(vhost->host);
  3960. }
  3961. return;
  3962. } else {
  3963. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  3964. vhost->job_step = ibmvfc_discover_targets;
  3965. }
  3966. } else {
  3967. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  3968. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3969. scsi_unblock_requests(vhost->host);
  3970. wake_up(&vhost->init_wait_q);
  3971. return;
  3972. }
  3973. break;
  3974. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  3975. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
  3976. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3977. ibmvfc_alloc_targets(vhost);
  3978. spin_lock_irqsave(vhost->host->host_lock, flags);
  3979. break;
  3980. case IBMVFC_HOST_ACTION_TGT_INIT:
  3981. list_for_each_entry(tgt, &vhost->targets, queue) {
  3982. if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
  3983. tgt->job_step(tgt);
  3984. break;
  3985. }
  3986. }
  3987. if (!ibmvfc_dev_init_to_do(vhost))
  3988. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
  3989. break;
  3990. default:
  3991. break;
  3992. };
  3993. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3994. }
  3995. /**
  3996. * ibmvfc_work - Do task level work
  3997. * @data: ibmvfc host struct
  3998. *
  3999. * Returns:
  4000. * zero
  4001. **/
  4002. static int ibmvfc_work(void *data)
  4003. {
  4004. struct ibmvfc_host *vhost = data;
  4005. int rc;
  4006. set_user_nice(current, MIN_NICE);
  4007. while (1) {
  4008. rc = wait_event_interruptible(vhost->work_wait_q,
  4009. ibmvfc_work_to_do(vhost));
  4010. BUG_ON(rc);
  4011. if (kthread_should_stop())
  4012. break;
  4013. ibmvfc_do_work(vhost);
  4014. }
  4015. ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
  4016. return 0;
  4017. }
  4018. /**
  4019. * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
  4020. * @vhost: ibmvfc host struct
  4021. *
  4022. * Allocates a page for messages, maps it for dma, and registers
  4023. * the crq with the hypervisor.
  4024. *
  4025. * Return value:
  4026. * zero on success / other on failure
  4027. **/
  4028. static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
  4029. {
  4030. int rc, retrc = -ENOMEM;
  4031. struct device *dev = vhost->dev;
  4032. struct vio_dev *vdev = to_vio_dev(dev);
  4033. struct ibmvfc_crq_queue *crq = &vhost->crq;
  4034. ENTER;
  4035. crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
  4036. if (!crq->msgs)
  4037. return -ENOMEM;
  4038. crq->size = PAGE_SIZE / sizeof(*crq->msgs);
  4039. crq->msg_token = dma_map_single(dev, crq->msgs,
  4040. PAGE_SIZE, DMA_BIDIRECTIONAL);
  4041. if (dma_mapping_error(dev, crq->msg_token))
  4042. goto map_failed;
  4043. retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
  4044. crq->msg_token, PAGE_SIZE);
  4045. if (rc == H_RESOURCE)
  4046. /* maybe kexecing and resource is busy. try a reset */
  4047. retrc = rc = ibmvfc_reset_crq(vhost);
  4048. if (rc == H_CLOSED)
  4049. dev_warn(dev, "Partner adapter not ready\n");
  4050. else if (rc) {
  4051. dev_warn(dev, "Error %d opening adapter\n", rc);
  4052. goto reg_crq_failed;
  4053. }
  4054. retrc = 0;
  4055. tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
  4056. if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
  4057. dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
  4058. goto req_irq_failed;
  4059. }
  4060. if ((rc = vio_enable_interrupts(vdev))) {
  4061. dev_err(dev, "Error %d enabling interrupts\n", rc);
  4062. goto req_irq_failed;
  4063. }
  4064. crq->cur = 0;
  4065. LEAVE;
  4066. return retrc;
  4067. req_irq_failed:
  4068. tasklet_kill(&vhost->tasklet);
  4069. do {
  4070. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  4071. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  4072. reg_crq_failed:
  4073. dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
  4074. map_failed:
  4075. free_page((unsigned long)crq->msgs);
  4076. return retrc;
  4077. }
  4078. /**
  4079. * ibmvfc_free_mem - Free memory for vhost
  4080. * @vhost: ibmvfc host struct
  4081. *
  4082. * Return value:
  4083. * none
  4084. **/
  4085. static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
  4086. {
  4087. struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
  4088. ENTER;
  4089. mempool_destroy(vhost->tgt_pool);
  4090. kfree(vhost->trace);
  4091. dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
  4092. vhost->disc_buf_dma);
  4093. dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
  4094. vhost->login_buf, vhost->login_buf_dma);
  4095. dma_pool_destroy(vhost->sg_pool);
  4096. dma_unmap_single(vhost->dev, async_q->msg_token,
  4097. async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
  4098. free_page((unsigned long)async_q->msgs);
  4099. LEAVE;
  4100. }
  4101. /**
  4102. * ibmvfc_alloc_mem - Allocate memory for vhost
  4103. * @vhost: ibmvfc host struct
  4104. *
  4105. * Return value:
  4106. * 0 on success / non-zero on failure
  4107. **/
  4108. static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
  4109. {
  4110. struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
  4111. struct device *dev = vhost->dev;
  4112. ENTER;
  4113. async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
  4114. if (!async_q->msgs) {
  4115. dev_err(dev, "Couldn't allocate async queue.\n");
  4116. goto nomem;
  4117. }
  4118. async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
  4119. async_q->msg_token = dma_map_single(dev, async_q->msgs,
  4120. async_q->size * sizeof(*async_q->msgs),
  4121. DMA_BIDIRECTIONAL);
  4122. if (dma_mapping_error(dev, async_q->msg_token)) {
  4123. dev_err(dev, "Failed to map async queue\n");
  4124. goto free_async_crq;
  4125. }
  4126. vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
  4127. SG_ALL * sizeof(struct srp_direct_buf),
  4128. sizeof(struct srp_direct_buf), 0);
  4129. if (!vhost->sg_pool) {
  4130. dev_err(dev, "Failed to allocate sg pool\n");
  4131. goto unmap_async_crq;
  4132. }
  4133. vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
  4134. &vhost->login_buf_dma, GFP_KERNEL);
  4135. if (!vhost->login_buf) {
  4136. dev_err(dev, "Couldn't allocate NPIV login buffer\n");
  4137. goto free_sg_pool;
  4138. }
  4139. vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
  4140. vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
  4141. &vhost->disc_buf_dma, GFP_KERNEL);
  4142. if (!vhost->disc_buf) {
  4143. dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
  4144. goto free_login_buffer;
  4145. }
  4146. vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
  4147. sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
  4148. if (!vhost->trace)
  4149. goto free_disc_buffer;
  4150. vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
  4151. sizeof(struct ibmvfc_target));
  4152. if (!vhost->tgt_pool) {
  4153. dev_err(dev, "Couldn't allocate target memory pool\n");
  4154. goto free_trace;
  4155. }
  4156. LEAVE;
  4157. return 0;
  4158. free_trace:
  4159. kfree(vhost->trace);
  4160. free_disc_buffer:
  4161. dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
  4162. vhost->disc_buf_dma);
  4163. free_login_buffer:
  4164. dma_free_coherent(dev, sizeof(*vhost->login_buf),
  4165. vhost->login_buf, vhost->login_buf_dma);
  4166. free_sg_pool:
  4167. dma_pool_destroy(vhost->sg_pool);
  4168. unmap_async_crq:
  4169. dma_unmap_single(dev, async_q->msg_token,
  4170. async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
  4171. free_async_crq:
  4172. free_page((unsigned long)async_q->msgs);
  4173. nomem:
  4174. LEAVE;
  4175. return -ENOMEM;
  4176. }
  4177. /**
  4178. * ibmvfc_rport_add_thread - Worker thread for rport adds
  4179. * @work: work struct
  4180. *
  4181. **/
  4182. static void ibmvfc_rport_add_thread(struct work_struct *work)
  4183. {
  4184. struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
  4185. rport_add_work_q);
  4186. struct ibmvfc_target *tgt;
  4187. struct fc_rport *rport;
  4188. unsigned long flags;
  4189. int did_work;
  4190. ENTER;
  4191. spin_lock_irqsave(vhost->host->host_lock, flags);
  4192. do {
  4193. did_work = 0;
  4194. if (vhost->state != IBMVFC_ACTIVE)
  4195. break;
  4196. list_for_each_entry(tgt, &vhost->targets, queue) {
  4197. if (tgt->add_rport) {
  4198. did_work = 1;
  4199. tgt->add_rport = 0;
  4200. kref_get(&tgt->kref);
  4201. rport = tgt->rport;
  4202. if (!rport) {
  4203. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4204. ibmvfc_tgt_add_rport(tgt);
  4205. } else if (get_device(&rport->dev)) {
  4206. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4207. tgt_dbg(tgt, "Setting rport roles\n");
  4208. fc_remote_port_rolechg(rport, tgt->ids.roles);
  4209. put_device(&rport->dev);
  4210. } else {
  4211. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4212. }
  4213. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4214. spin_lock_irqsave(vhost->host->host_lock, flags);
  4215. break;
  4216. }
  4217. }
  4218. } while(did_work);
  4219. if (vhost->state == IBMVFC_ACTIVE)
  4220. vhost->scan_complete = 1;
  4221. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4222. LEAVE;
  4223. }
  4224. /**
  4225. * ibmvfc_probe - Adapter hot plug add entry point
  4226. * @vdev: vio device struct
  4227. * @id: vio device id struct
  4228. *
  4229. * Return value:
  4230. * 0 on success / non-zero on failure
  4231. **/
  4232. static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  4233. {
  4234. struct ibmvfc_host *vhost;
  4235. struct Scsi_Host *shost;
  4236. struct device *dev = &vdev->dev;
  4237. int rc = -ENOMEM;
  4238. ENTER;
  4239. shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
  4240. if (!shost) {
  4241. dev_err(dev, "Couldn't allocate host data\n");
  4242. goto out;
  4243. }
  4244. shost->transportt = ibmvfc_transport_template;
  4245. shost->can_queue = max_requests;
  4246. shost->max_lun = max_lun;
  4247. shost->max_id = max_targets;
  4248. shost->max_sectors = IBMVFC_MAX_SECTORS;
  4249. shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
  4250. shost->unique_id = shost->host_no;
  4251. vhost = shost_priv(shost);
  4252. INIT_LIST_HEAD(&vhost->sent);
  4253. INIT_LIST_HEAD(&vhost->free);
  4254. INIT_LIST_HEAD(&vhost->targets);
  4255. sprintf(vhost->name, IBMVFC_NAME);
  4256. vhost->host = shost;
  4257. vhost->dev = dev;
  4258. vhost->partition_number = -1;
  4259. vhost->log_level = log_level;
  4260. vhost->task_set = 1;
  4261. strcpy(vhost->partition_name, "UNKNOWN");
  4262. init_waitqueue_head(&vhost->work_wait_q);
  4263. init_waitqueue_head(&vhost->init_wait_q);
  4264. INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
  4265. mutex_init(&vhost->passthru_mutex);
  4266. if ((rc = ibmvfc_alloc_mem(vhost)))
  4267. goto free_scsi_host;
  4268. vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
  4269. shost->host_no);
  4270. if (IS_ERR(vhost->work_thread)) {
  4271. dev_err(dev, "Couldn't create kernel thread: %ld\n",
  4272. PTR_ERR(vhost->work_thread));
  4273. goto free_host_mem;
  4274. }
  4275. if ((rc = ibmvfc_init_crq(vhost))) {
  4276. dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
  4277. goto kill_kthread;
  4278. }
  4279. if ((rc = ibmvfc_init_event_pool(vhost))) {
  4280. dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
  4281. goto release_crq;
  4282. }
  4283. if ((rc = scsi_add_host(shost, dev)))
  4284. goto release_event_pool;
  4285. fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
  4286. if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
  4287. &ibmvfc_trace_attr))) {
  4288. dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
  4289. goto remove_shost;
  4290. }
  4291. if (shost_to_fc_host(shost)->rqst_q)
  4292. blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
  4293. dev_set_drvdata(dev, vhost);
  4294. spin_lock(&ibmvfc_driver_lock);
  4295. list_add_tail(&vhost->queue, &ibmvfc_head);
  4296. spin_unlock(&ibmvfc_driver_lock);
  4297. ibmvfc_send_crq_init(vhost);
  4298. scsi_scan_host(shost);
  4299. return 0;
  4300. remove_shost:
  4301. scsi_remove_host(shost);
  4302. release_event_pool:
  4303. ibmvfc_free_event_pool(vhost);
  4304. release_crq:
  4305. ibmvfc_release_crq_queue(vhost);
  4306. kill_kthread:
  4307. kthread_stop(vhost->work_thread);
  4308. free_host_mem:
  4309. ibmvfc_free_mem(vhost);
  4310. free_scsi_host:
  4311. scsi_host_put(shost);
  4312. out:
  4313. LEAVE;
  4314. return rc;
  4315. }
  4316. /**
  4317. * ibmvfc_remove - Adapter hot plug remove entry point
  4318. * @vdev: vio device struct
  4319. *
  4320. * Return value:
  4321. * 0
  4322. **/
  4323. static int ibmvfc_remove(struct vio_dev *vdev)
  4324. {
  4325. struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
  4326. unsigned long flags;
  4327. ENTER;
  4328. ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
  4329. spin_lock_irqsave(vhost->host->host_lock, flags);
  4330. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  4331. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4332. ibmvfc_wait_while_resetting(vhost);
  4333. ibmvfc_release_crq_queue(vhost);
  4334. kthread_stop(vhost->work_thread);
  4335. fc_remove_host(vhost->host);
  4336. scsi_remove_host(vhost->host);
  4337. spin_lock_irqsave(vhost->host->host_lock, flags);
  4338. ibmvfc_purge_requests(vhost, DID_ERROR);
  4339. ibmvfc_free_event_pool(vhost);
  4340. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4341. ibmvfc_free_mem(vhost);
  4342. spin_lock(&ibmvfc_driver_lock);
  4343. list_del(&vhost->queue);
  4344. spin_unlock(&ibmvfc_driver_lock);
  4345. scsi_host_put(vhost->host);
  4346. LEAVE;
  4347. return 0;
  4348. }
  4349. /**
  4350. * ibmvfc_resume - Resume from suspend
  4351. * @dev: device struct
  4352. *
  4353. * We may have lost an interrupt across suspend/resume, so kick the
  4354. * interrupt handler
  4355. *
  4356. */
  4357. static int ibmvfc_resume(struct device *dev)
  4358. {
  4359. unsigned long flags;
  4360. struct ibmvfc_host *vhost = dev_get_drvdata(dev);
  4361. struct vio_dev *vdev = to_vio_dev(dev);
  4362. spin_lock_irqsave(vhost->host->host_lock, flags);
  4363. vio_disable_interrupts(vdev);
  4364. tasklet_schedule(&vhost->tasklet);
  4365. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4366. return 0;
  4367. }
  4368. /**
  4369. * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
  4370. * @vdev: vio device struct
  4371. *
  4372. * Return value:
  4373. * Number of bytes the driver will need to DMA map at the same time in
  4374. * order to perform well.
  4375. */
  4376. static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
  4377. {
  4378. unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
  4379. return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
  4380. }
  4381. static struct vio_device_id ibmvfc_device_table[] = {
  4382. {"fcp", "IBM,vfc-client"},
  4383. { "", "" }
  4384. };
  4385. MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
  4386. static struct dev_pm_ops ibmvfc_pm_ops = {
  4387. .resume = ibmvfc_resume
  4388. };
  4389. static struct vio_driver ibmvfc_driver = {
  4390. .id_table = ibmvfc_device_table,
  4391. .probe = ibmvfc_probe,
  4392. .remove = ibmvfc_remove,
  4393. .get_desired_dma = ibmvfc_get_desired_dma,
  4394. .name = IBMVFC_NAME,
  4395. .pm = &ibmvfc_pm_ops,
  4396. };
  4397. static struct fc_function_template ibmvfc_transport_functions = {
  4398. .show_host_fabric_name = 1,
  4399. .show_host_node_name = 1,
  4400. .show_host_port_name = 1,
  4401. .show_host_supported_classes = 1,
  4402. .show_host_port_type = 1,
  4403. .show_host_port_id = 1,
  4404. .show_host_maxframe_size = 1,
  4405. .get_host_port_state = ibmvfc_get_host_port_state,
  4406. .show_host_port_state = 1,
  4407. .get_host_speed = ibmvfc_get_host_speed,
  4408. .show_host_speed = 1,
  4409. .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
  4410. .terminate_rport_io = ibmvfc_terminate_rport_io,
  4411. .show_rport_maxframe_size = 1,
  4412. .show_rport_supported_classes = 1,
  4413. .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
  4414. .show_rport_dev_loss_tmo = 1,
  4415. .get_starget_node_name = ibmvfc_get_starget_node_name,
  4416. .show_starget_node_name = 1,
  4417. .get_starget_port_name = ibmvfc_get_starget_port_name,
  4418. .show_starget_port_name = 1,
  4419. .get_starget_port_id = ibmvfc_get_starget_port_id,
  4420. .show_starget_port_id = 1,
  4421. .bsg_request = ibmvfc_bsg_request,
  4422. .bsg_timeout = ibmvfc_bsg_timeout,
  4423. };
  4424. /**
  4425. * ibmvfc_module_init - Initialize the ibmvfc module
  4426. *
  4427. * Return value:
  4428. * 0 on success / other on failure
  4429. **/
  4430. static int __init ibmvfc_module_init(void)
  4431. {
  4432. int rc;
  4433. if (!firmware_has_feature(FW_FEATURE_VIO))
  4434. return -ENODEV;
  4435. printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
  4436. IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
  4437. ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
  4438. if (!ibmvfc_transport_template)
  4439. return -ENOMEM;
  4440. rc = vio_register_driver(&ibmvfc_driver);
  4441. if (rc)
  4442. fc_release_transport(ibmvfc_transport_template);
  4443. return rc;
  4444. }
  4445. /**
  4446. * ibmvfc_module_exit - Teardown the ibmvfc module
  4447. *
  4448. * Return value:
  4449. * nothing
  4450. **/
  4451. static void __exit ibmvfc_module_exit(void)
  4452. {
  4453. vio_unregister_driver(&ibmvfc_driver);
  4454. fc_release_transport(ibmvfc_transport_template);
  4455. }
  4456. module_init(ibmvfc_module_init);
  4457. module_exit(ibmvfc_module_exit);