ibmvfc.c 135 KB

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