ibmvfc.c 140 KB

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