ibmvfc.c 107 KB

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