ibmvfc.c 133 KB

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