ibmvfc.c 106 KB

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