ibmvfc.c 114 KB

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