ibmvfc.c 140 KB

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