ibmvfc.c 111 KB

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