ibmvscsi_tgt.c 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036
  1. /*******************************************************************************
  2. * IBM Virtual SCSI Target Driver
  3. * Copyright (C) 2003-2005 Dave Boutcher (boutcher@us.ibm.com) IBM Corp.
  4. * Santiago Leon (santil@us.ibm.com) IBM Corp.
  5. * Linda Xie (lxie@us.ibm.com) IBM Corp.
  6. *
  7. * Copyright (C) 2005-2011 FUJITA Tomonori <tomof@acm.org>
  8. * Copyright (C) 2010 Nicholas A. Bellinger <nab@kernel.org>
  9. *
  10. * Authors: Bryant G. Ly <bryantly@linux.vnet.ibm.com>
  11. * Authors: Michael Cyr <mikecyr@linux.vnet.ibm.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. ****************************************************************************/
  24. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/types.h>
  29. #include <linux/list.h>
  30. #include <linux/string.h>
  31. #include <target/target_core_base.h>
  32. #include <target/target_core_fabric.h>
  33. #include <asm/hvcall.h>
  34. #include <asm/vio.h>
  35. #include <scsi/viosrp.h>
  36. #include "ibmvscsi_tgt.h"
  37. #define IBMVSCSIS_VERSION "v0.2"
  38. #define INITIAL_SRP_LIMIT 800
  39. #define DEFAULT_MAX_SECTORS 256
  40. #define MAX_TXU 1024 * 1024
  41. static uint max_vdma_size = MAX_H_COPY_RDMA;
  42. static char system_id[SYS_ID_NAME_LEN] = "";
  43. static char partition_name[PARTITION_NAMELEN] = "UNKNOWN";
  44. static uint partition_number = -1;
  45. /* Adapter list and lock to control it */
  46. static DEFINE_SPINLOCK(ibmvscsis_dev_lock);
  47. static LIST_HEAD(ibmvscsis_dev_list);
  48. static long ibmvscsis_parse_command(struct scsi_info *vscsi,
  49. struct viosrp_crq *crq);
  50. static void ibmvscsis_adapter_idle(struct scsi_info *vscsi);
  51. static void ibmvscsis_determine_resid(struct se_cmd *se_cmd,
  52. struct srp_rsp *rsp)
  53. {
  54. u32 residual_count = se_cmd->residual_count;
  55. if (!residual_count)
  56. return;
  57. if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
  58. if (se_cmd->data_direction == DMA_TO_DEVICE) {
  59. /* residual data from an underflow write */
  60. rsp->flags = SRP_RSP_FLAG_DOUNDER;
  61. rsp->data_out_res_cnt = cpu_to_be32(residual_count);
  62. } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
  63. /* residual data from an underflow read */
  64. rsp->flags = SRP_RSP_FLAG_DIUNDER;
  65. rsp->data_in_res_cnt = cpu_to_be32(residual_count);
  66. }
  67. } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  68. if (se_cmd->data_direction == DMA_TO_DEVICE) {
  69. /* residual data from an overflow write */
  70. rsp->flags = SRP_RSP_FLAG_DOOVER;
  71. rsp->data_out_res_cnt = cpu_to_be32(residual_count);
  72. } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
  73. /* residual data from an overflow read */
  74. rsp->flags = SRP_RSP_FLAG_DIOVER;
  75. rsp->data_in_res_cnt = cpu_to_be32(residual_count);
  76. }
  77. }
  78. }
  79. /**
  80. * connection_broken() - Determine if the connection to the client is good
  81. * @vscsi: Pointer to our adapter structure
  82. *
  83. * This function attempts to send a ping MAD to the client. If the call to
  84. * queue the request returns H_CLOSED then the connection has been broken
  85. * and the function returns TRUE.
  86. *
  87. * EXECUTION ENVIRONMENT:
  88. * Interrupt or Process environment
  89. */
  90. static bool connection_broken(struct scsi_info *vscsi)
  91. {
  92. struct viosrp_crq *crq;
  93. u64 buffer[2] = { 0, 0 };
  94. long h_return_code;
  95. bool rc = false;
  96. /* create a PING crq */
  97. crq = (struct viosrp_crq *)&buffer;
  98. crq->valid = VALID_CMD_RESP_EL;
  99. crq->format = MESSAGE_IN_CRQ;
  100. crq->status = PING;
  101. h_return_code = h_send_crq(vscsi->dds.unit_id,
  102. cpu_to_be64(buffer[MSG_HI]),
  103. cpu_to_be64(buffer[MSG_LOW]));
  104. pr_debug("connection_broken: rc %ld\n", h_return_code);
  105. if (h_return_code == H_CLOSED)
  106. rc = true;
  107. return rc;
  108. }
  109. /**
  110. * ibmvscsis_unregister_command_q() - Helper Function-Unregister Command Queue
  111. * @vscsi: Pointer to our adapter structure
  112. *
  113. * This function calls h_free_q then frees the interrupt bit etc.
  114. * It must release the lock before doing so because of the time it can take
  115. * for h_free_crq in PHYP
  116. * NOTE: the caller must make sure that state and or flags will prevent
  117. * interrupt handler from scheduling work.
  118. * NOTE: anyone calling this function may need to set the CRQ_CLOSED flag
  119. * we can't do it here, because we don't have the lock
  120. *
  121. * EXECUTION ENVIRONMENT:
  122. * Process level
  123. */
  124. static long ibmvscsis_unregister_command_q(struct scsi_info *vscsi)
  125. {
  126. long qrc;
  127. long rc = ADAPT_SUCCESS;
  128. int ticks = 0;
  129. do {
  130. qrc = h_free_crq(vscsi->dds.unit_id);
  131. switch (qrc) {
  132. case H_SUCCESS:
  133. break;
  134. case H_HARDWARE:
  135. case H_PARAMETER:
  136. dev_err(&vscsi->dev, "unregister_command_q: error from h_free_crq %ld\n",
  137. qrc);
  138. rc = ERROR;
  139. break;
  140. case H_BUSY:
  141. case H_LONG_BUSY_ORDER_1_MSEC:
  142. /* msleep not good for small values */
  143. usleep_range(1000, 2000);
  144. ticks += 1;
  145. break;
  146. case H_LONG_BUSY_ORDER_10_MSEC:
  147. usleep_range(10000, 20000);
  148. ticks += 10;
  149. break;
  150. case H_LONG_BUSY_ORDER_100_MSEC:
  151. msleep(100);
  152. ticks += 100;
  153. break;
  154. case H_LONG_BUSY_ORDER_1_SEC:
  155. ssleep(1);
  156. ticks += 1000;
  157. break;
  158. case H_LONG_BUSY_ORDER_10_SEC:
  159. ssleep(10);
  160. ticks += 10000;
  161. break;
  162. case H_LONG_BUSY_ORDER_100_SEC:
  163. ssleep(100);
  164. ticks += 100000;
  165. break;
  166. default:
  167. dev_err(&vscsi->dev, "unregister_command_q: unknown error %ld from h_free_crq\n",
  168. qrc);
  169. rc = ERROR;
  170. break;
  171. }
  172. /*
  173. * dont wait more then 300 seconds
  174. * ticks are in milliseconds more or less
  175. */
  176. if (ticks > 300000 && qrc != H_SUCCESS) {
  177. rc = ERROR;
  178. dev_err(&vscsi->dev, "Excessive wait for h_free_crq\n");
  179. }
  180. } while (qrc != H_SUCCESS && rc == ADAPT_SUCCESS);
  181. pr_debug("Freeing CRQ: phyp rc %ld, rc %ld\n", qrc, rc);
  182. return rc;
  183. }
  184. /**
  185. * ibmvscsis_delete_client_info() - Helper function to Delete Client Info
  186. * @vscsi: Pointer to our adapter structure
  187. * @client_closed: True if client closed its queue
  188. *
  189. * Deletes information specific to the client when the client goes away
  190. *
  191. * EXECUTION ENVIRONMENT:
  192. * Interrupt or Process
  193. */
  194. static void ibmvscsis_delete_client_info(struct scsi_info *vscsi,
  195. bool client_closed)
  196. {
  197. vscsi->client_cap = 0;
  198. /*
  199. * Some things we don't want to clear if we're closing the queue,
  200. * because some clients don't resend the host handshake when they
  201. * get a transport event.
  202. */
  203. if (client_closed)
  204. vscsi->client_data.os_type = 0;
  205. }
  206. /**
  207. * ibmvscsis_free_command_q() - Free Command Queue
  208. * @vscsi: Pointer to our adapter structure
  209. *
  210. * This function calls unregister_command_q, then clears interrupts and
  211. * any pending interrupt acknowledgments associated with the command q.
  212. * It also clears memory if there is no error.
  213. *
  214. * PHYP did not meet the PAPR architecture so that we must give up the
  215. * lock. This causes a timing hole regarding state change. To close the
  216. * hole this routine does accounting on any change that occurred during
  217. * the time the lock is not held.
  218. * NOTE: must give up and then acquire the interrupt lock, the caller must
  219. * make sure that state and or flags will prevent interrupt handler from
  220. * scheduling work.
  221. *
  222. * EXECUTION ENVIRONMENT:
  223. * Process level, interrupt lock is held
  224. */
  225. static long ibmvscsis_free_command_q(struct scsi_info *vscsi)
  226. {
  227. int bytes;
  228. u32 flags_under_lock;
  229. u16 state_under_lock;
  230. long rc = ADAPT_SUCCESS;
  231. if (!(vscsi->flags & CRQ_CLOSED)) {
  232. vio_disable_interrupts(vscsi->dma_dev);
  233. state_under_lock = vscsi->new_state;
  234. flags_under_lock = vscsi->flags;
  235. vscsi->phyp_acr_state = 0;
  236. vscsi->phyp_acr_flags = 0;
  237. spin_unlock_bh(&vscsi->intr_lock);
  238. rc = ibmvscsis_unregister_command_q(vscsi);
  239. spin_lock_bh(&vscsi->intr_lock);
  240. if (state_under_lock != vscsi->new_state)
  241. vscsi->phyp_acr_state = vscsi->new_state;
  242. vscsi->phyp_acr_flags = ((~flags_under_lock) & vscsi->flags);
  243. if (rc == ADAPT_SUCCESS) {
  244. bytes = vscsi->cmd_q.size * PAGE_SIZE;
  245. memset(vscsi->cmd_q.base_addr, 0, bytes);
  246. vscsi->cmd_q.index = 0;
  247. vscsi->flags |= CRQ_CLOSED;
  248. ibmvscsis_delete_client_info(vscsi, false);
  249. }
  250. pr_debug("free_command_q: flags 0x%x, state 0x%hx, acr_flags 0x%x, acr_state 0x%hx\n",
  251. vscsi->flags, vscsi->state, vscsi->phyp_acr_flags,
  252. vscsi->phyp_acr_state);
  253. }
  254. return rc;
  255. }
  256. /**
  257. * ibmvscsis_cmd_q_dequeue() - Get valid Command element
  258. * @mask: Mask to use in case index wraps
  259. * @current_index: Current index into command queue
  260. * @base_addr: Pointer to start of command queue
  261. *
  262. * Returns a pointer to a valid command element or NULL, if the command
  263. * queue is empty
  264. *
  265. * EXECUTION ENVIRONMENT:
  266. * Interrupt environment, interrupt lock held
  267. */
  268. static struct viosrp_crq *ibmvscsis_cmd_q_dequeue(uint mask,
  269. uint *current_index,
  270. struct viosrp_crq *base_addr)
  271. {
  272. struct viosrp_crq *ptr;
  273. ptr = base_addr + *current_index;
  274. if (ptr->valid) {
  275. *current_index = (*current_index + 1) & mask;
  276. dma_rmb();
  277. } else {
  278. ptr = NULL;
  279. }
  280. return ptr;
  281. }
  282. /**
  283. * ibmvscsis_send_init_message() - send initialize message to the client
  284. * @vscsi: Pointer to our adapter structure
  285. * @format: Which Init Message format to send
  286. *
  287. * EXECUTION ENVIRONMENT:
  288. * Interrupt environment interrupt lock held
  289. */
  290. static long ibmvscsis_send_init_message(struct scsi_info *vscsi, u8 format)
  291. {
  292. struct viosrp_crq *crq;
  293. u64 buffer[2] = { 0, 0 };
  294. long rc;
  295. crq = (struct viosrp_crq *)&buffer;
  296. crq->valid = VALID_INIT_MSG;
  297. crq->format = format;
  298. rc = h_send_crq(vscsi->dds.unit_id, cpu_to_be64(buffer[MSG_HI]),
  299. cpu_to_be64(buffer[MSG_LOW]));
  300. return rc;
  301. }
  302. /**
  303. * ibmvscsis_check_init_msg() - Check init message valid
  304. * @vscsi: Pointer to our adapter structure
  305. * @format: Pointer to return format of Init Message, if any.
  306. * Set to UNUSED_FORMAT if no Init Message in queue.
  307. *
  308. * Checks if an initialize message was queued by the initiatior
  309. * after the queue was created and before the interrupt was enabled.
  310. *
  311. * EXECUTION ENVIRONMENT:
  312. * Process level only, interrupt lock held
  313. */
  314. static long ibmvscsis_check_init_msg(struct scsi_info *vscsi, uint *format)
  315. {
  316. struct viosrp_crq *crq;
  317. long rc = ADAPT_SUCCESS;
  318. crq = ibmvscsis_cmd_q_dequeue(vscsi->cmd_q.mask, &vscsi->cmd_q.index,
  319. vscsi->cmd_q.base_addr);
  320. if (!crq) {
  321. *format = (uint)UNUSED_FORMAT;
  322. } else if (crq->valid == VALID_INIT_MSG && crq->format == INIT_MSG) {
  323. *format = (uint)INIT_MSG;
  324. crq->valid = INVALIDATE_CMD_RESP_EL;
  325. dma_rmb();
  326. /*
  327. * the caller has ensured no initialize message was
  328. * sent after the queue was
  329. * created so there should be no other message on the queue.
  330. */
  331. crq = ibmvscsis_cmd_q_dequeue(vscsi->cmd_q.mask,
  332. &vscsi->cmd_q.index,
  333. vscsi->cmd_q.base_addr);
  334. if (crq) {
  335. *format = (uint)(crq->format);
  336. rc = ERROR;
  337. crq->valid = INVALIDATE_CMD_RESP_EL;
  338. dma_rmb();
  339. }
  340. } else {
  341. *format = (uint)(crq->format);
  342. rc = ERROR;
  343. crq->valid = INVALIDATE_CMD_RESP_EL;
  344. dma_rmb();
  345. }
  346. return rc;
  347. }
  348. /**
  349. * ibmvscsis_disconnect() - Helper function to disconnect
  350. * @work: Pointer to work_struct, gives access to our adapter structure
  351. *
  352. * An error has occurred or the driver received a Transport event,
  353. * and the driver is requesting that the command queue be de-registered
  354. * in a safe manner. If there is no outstanding I/O then we can stop the
  355. * queue. If we are restarting the queue it will be reflected in the
  356. * the state of the adapter.
  357. *
  358. * EXECUTION ENVIRONMENT:
  359. * Process environment
  360. */
  361. static void ibmvscsis_disconnect(struct work_struct *work)
  362. {
  363. struct scsi_info *vscsi = container_of(work, struct scsi_info,
  364. proc_work);
  365. u16 new_state;
  366. bool wait_idle = false;
  367. spin_lock_bh(&vscsi->intr_lock);
  368. new_state = vscsi->new_state;
  369. vscsi->new_state = 0;
  370. pr_debug("disconnect: flags 0x%x, state 0x%hx\n", vscsi->flags,
  371. vscsi->state);
  372. /*
  373. * check which state we are in and see if we
  374. * should transitition to the new state
  375. */
  376. switch (vscsi->state) {
  377. /* Should never be called while in this state. */
  378. case NO_QUEUE:
  379. /*
  380. * Can never transition from this state;
  381. * igonore errors and logout.
  382. */
  383. case UNCONFIGURING:
  384. break;
  385. /* can transition from this state to UNCONFIGURING */
  386. case ERR_DISCONNECT:
  387. if (new_state == UNCONFIGURING)
  388. vscsi->state = new_state;
  389. break;
  390. /*
  391. * Can transition from this state to to unconfiguring
  392. * or err disconnect.
  393. */
  394. case ERR_DISCONNECT_RECONNECT:
  395. switch (new_state) {
  396. case UNCONFIGURING:
  397. case ERR_DISCONNECT:
  398. vscsi->state = new_state;
  399. break;
  400. case WAIT_IDLE:
  401. break;
  402. default:
  403. break;
  404. }
  405. break;
  406. /* can transition from this state to UNCONFIGURING */
  407. case ERR_DISCONNECTED:
  408. if (new_state == UNCONFIGURING)
  409. vscsi->state = new_state;
  410. break;
  411. case WAIT_ENABLED:
  412. switch (new_state) {
  413. case UNCONFIGURING:
  414. vscsi->state = new_state;
  415. vscsi->flags |= RESPONSE_Q_DOWN;
  416. vscsi->flags &= ~(SCHEDULE_DISCONNECT |
  417. DISCONNECT_SCHEDULED);
  418. dma_rmb();
  419. if (vscsi->flags & CFG_SLEEPING) {
  420. vscsi->flags &= ~CFG_SLEEPING;
  421. complete(&vscsi->unconfig);
  422. }
  423. break;
  424. /* should never happen */
  425. case ERR_DISCONNECT:
  426. case ERR_DISCONNECT_RECONNECT:
  427. case WAIT_IDLE:
  428. dev_err(&vscsi->dev, "disconnect: invalid state %d for WAIT_IDLE\n",
  429. vscsi->state);
  430. break;
  431. }
  432. break;
  433. case WAIT_IDLE:
  434. switch (new_state) {
  435. case UNCONFIGURING:
  436. vscsi->flags |= RESPONSE_Q_DOWN;
  437. vscsi->state = new_state;
  438. vscsi->flags &= ~(SCHEDULE_DISCONNECT |
  439. DISCONNECT_SCHEDULED);
  440. ibmvscsis_free_command_q(vscsi);
  441. break;
  442. case ERR_DISCONNECT:
  443. case ERR_DISCONNECT_RECONNECT:
  444. vscsi->state = new_state;
  445. break;
  446. }
  447. break;
  448. /*
  449. * Initiator has not done a successful srp login
  450. * or has done a successful srp logout ( adapter was not
  451. * busy). In the first case there can be responses queued
  452. * waiting for space on the initiators response queue (MAD)
  453. * The second case the adapter is idle. Assume the worse case,
  454. * i.e. the second case.
  455. */
  456. case WAIT_CONNECTION:
  457. case CONNECTED:
  458. case SRP_PROCESSING:
  459. wait_idle = true;
  460. vscsi->state = new_state;
  461. break;
  462. /* can transition from this state to UNCONFIGURING */
  463. case UNDEFINED:
  464. if (new_state == UNCONFIGURING)
  465. vscsi->state = new_state;
  466. break;
  467. default:
  468. break;
  469. }
  470. if (wait_idle) {
  471. pr_debug("disconnect start wait, active %d, sched %d\n",
  472. (int)list_empty(&vscsi->active_q),
  473. (int)list_empty(&vscsi->schedule_q));
  474. if (!list_empty(&vscsi->active_q) ||
  475. !list_empty(&vscsi->schedule_q)) {
  476. vscsi->flags |= WAIT_FOR_IDLE;
  477. pr_debug("disconnect flags 0x%x\n", vscsi->flags);
  478. /*
  479. * This routine is can not be called with the interrupt
  480. * lock held.
  481. */
  482. spin_unlock_bh(&vscsi->intr_lock);
  483. wait_for_completion(&vscsi->wait_idle);
  484. spin_lock_bh(&vscsi->intr_lock);
  485. }
  486. pr_debug("disconnect stop wait\n");
  487. ibmvscsis_adapter_idle(vscsi);
  488. }
  489. spin_unlock_bh(&vscsi->intr_lock);
  490. }
  491. /**
  492. * ibmvscsis_post_disconnect() - Schedule the disconnect
  493. * @vscsi: Pointer to our adapter structure
  494. * @new_state: State to move to after disconnecting
  495. * @flag_bits: Flags to turn on in adapter structure
  496. *
  497. * If it's already been scheduled, then see if we need to "upgrade"
  498. * the new state (if the one passed in is more "severe" than the
  499. * previous one).
  500. *
  501. * PRECONDITION:
  502. * interrupt lock is held
  503. */
  504. static void ibmvscsis_post_disconnect(struct scsi_info *vscsi, uint new_state,
  505. uint flag_bits)
  506. {
  507. uint state;
  508. /* check the validity of the new state */
  509. switch (new_state) {
  510. case UNCONFIGURING:
  511. case ERR_DISCONNECT:
  512. case ERR_DISCONNECT_RECONNECT:
  513. case WAIT_IDLE:
  514. break;
  515. default:
  516. dev_err(&vscsi->dev, "post_disconnect: Invalid new state %d\n",
  517. new_state);
  518. return;
  519. }
  520. vscsi->flags |= flag_bits;
  521. pr_debug("post_disconnect: new_state 0x%x, flag_bits 0x%x, vscsi->flags 0x%x, state %hx\n",
  522. new_state, flag_bits, vscsi->flags, vscsi->state);
  523. if (!(vscsi->flags & (DISCONNECT_SCHEDULED | SCHEDULE_DISCONNECT))) {
  524. vscsi->flags |= SCHEDULE_DISCONNECT;
  525. vscsi->new_state = new_state;
  526. INIT_WORK(&vscsi->proc_work, ibmvscsis_disconnect);
  527. (void)queue_work(vscsi->work_q, &vscsi->proc_work);
  528. } else {
  529. if (vscsi->new_state)
  530. state = vscsi->new_state;
  531. else
  532. state = vscsi->state;
  533. switch (state) {
  534. case NO_QUEUE:
  535. case UNCONFIGURING:
  536. break;
  537. case ERR_DISCONNECTED:
  538. case ERR_DISCONNECT:
  539. case UNDEFINED:
  540. if (new_state == UNCONFIGURING)
  541. vscsi->new_state = new_state;
  542. break;
  543. case ERR_DISCONNECT_RECONNECT:
  544. switch (new_state) {
  545. case UNCONFIGURING:
  546. case ERR_DISCONNECT:
  547. vscsi->new_state = new_state;
  548. break;
  549. default:
  550. break;
  551. }
  552. break;
  553. case WAIT_ENABLED:
  554. case WAIT_IDLE:
  555. case WAIT_CONNECTION:
  556. case CONNECTED:
  557. case SRP_PROCESSING:
  558. vscsi->new_state = new_state;
  559. break;
  560. default:
  561. break;
  562. }
  563. }
  564. pr_debug("Leaving post_disconnect: flags 0x%x, new_state 0x%x\n",
  565. vscsi->flags, vscsi->new_state);
  566. }
  567. /**
  568. * ibmvscsis_handle_init_compl_msg() - Respond to an Init Complete Message
  569. * @vscsi: Pointer to our adapter structure
  570. *
  571. * Must be called with interrupt lock held.
  572. */
  573. static long ibmvscsis_handle_init_compl_msg(struct scsi_info *vscsi)
  574. {
  575. long rc = ADAPT_SUCCESS;
  576. switch (vscsi->state) {
  577. case NO_QUEUE:
  578. case ERR_DISCONNECT:
  579. case ERR_DISCONNECT_RECONNECT:
  580. case ERR_DISCONNECTED:
  581. case UNCONFIGURING:
  582. case UNDEFINED:
  583. rc = ERROR;
  584. break;
  585. case WAIT_CONNECTION:
  586. vscsi->state = CONNECTED;
  587. break;
  588. case WAIT_IDLE:
  589. case SRP_PROCESSING:
  590. case CONNECTED:
  591. case WAIT_ENABLED:
  592. default:
  593. rc = ERROR;
  594. dev_err(&vscsi->dev, "init_msg: invalid state %d to get init compl msg\n",
  595. vscsi->state);
  596. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  597. break;
  598. }
  599. return rc;
  600. }
  601. /**
  602. * ibmvscsis_handle_init_msg() - Respond to an Init Message
  603. * @vscsi: Pointer to our adapter structure
  604. *
  605. * Must be called with interrupt lock held.
  606. */
  607. static long ibmvscsis_handle_init_msg(struct scsi_info *vscsi)
  608. {
  609. long rc = ADAPT_SUCCESS;
  610. switch (vscsi->state) {
  611. case WAIT_CONNECTION:
  612. rc = ibmvscsis_send_init_message(vscsi, INIT_COMPLETE_MSG);
  613. switch (rc) {
  614. case H_SUCCESS:
  615. vscsi->state = CONNECTED;
  616. break;
  617. case H_PARAMETER:
  618. dev_err(&vscsi->dev, "init_msg: failed to send, rc %ld\n",
  619. rc);
  620. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
  621. break;
  622. case H_DROPPED:
  623. dev_err(&vscsi->dev, "init_msg: failed to send, rc %ld\n",
  624. rc);
  625. rc = ERROR;
  626. ibmvscsis_post_disconnect(vscsi,
  627. ERR_DISCONNECT_RECONNECT, 0);
  628. break;
  629. case H_CLOSED:
  630. pr_warn("init_msg: failed to send, rc %ld\n", rc);
  631. rc = 0;
  632. break;
  633. }
  634. break;
  635. case UNDEFINED:
  636. rc = ERROR;
  637. break;
  638. case UNCONFIGURING:
  639. break;
  640. case WAIT_ENABLED:
  641. case CONNECTED:
  642. case SRP_PROCESSING:
  643. case WAIT_IDLE:
  644. case NO_QUEUE:
  645. case ERR_DISCONNECT:
  646. case ERR_DISCONNECT_RECONNECT:
  647. case ERR_DISCONNECTED:
  648. default:
  649. rc = ERROR;
  650. dev_err(&vscsi->dev, "init_msg: invalid state %d to get init msg\n",
  651. vscsi->state);
  652. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  653. break;
  654. }
  655. return rc;
  656. }
  657. /**
  658. * ibmvscsis_init_msg() - Respond to an init message
  659. * @vscsi: Pointer to our adapter structure
  660. * @crq: Pointer to CRQ element containing the Init Message
  661. *
  662. * EXECUTION ENVIRONMENT:
  663. * Interrupt, interrupt lock held
  664. */
  665. static long ibmvscsis_init_msg(struct scsi_info *vscsi, struct viosrp_crq *crq)
  666. {
  667. long rc = ADAPT_SUCCESS;
  668. pr_debug("init_msg: state 0x%hx\n", vscsi->state);
  669. rc = h_vioctl(vscsi->dds.unit_id, H_GET_PARTNER_INFO,
  670. (u64)vscsi->map_ioba | ((u64)PAGE_SIZE << 32), 0, 0, 0,
  671. 0);
  672. if (rc == H_SUCCESS) {
  673. vscsi->client_data.partition_number =
  674. be64_to_cpu(*(u64 *)vscsi->map_buf);
  675. pr_debug("init_msg, part num %d\n",
  676. vscsi->client_data.partition_number);
  677. } else {
  678. pr_debug("init_msg h_vioctl rc %ld\n", rc);
  679. rc = ADAPT_SUCCESS;
  680. }
  681. if (crq->format == INIT_MSG) {
  682. rc = ibmvscsis_handle_init_msg(vscsi);
  683. } else if (crq->format == INIT_COMPLETE_MSG) {
  684. rc = ibmvscsis_handle_init_compl_msg(vscsi);
  685. } else {
  686. rc = ERROR;
  687. dev_err(&vscsi->dev, "init_msg: invalid format %d\n",
  688. (uint)crq->format);
  689. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  690. }
  691. return rc;
  692. }
  693. /**
  694. * ibmvscsis_establish_new_q() - Establish new CRQ queue
  695. * @vscsi: Pointer to our adapter structure
  696. *
  697. * Must be called with interrupt lock held.
  698. */
  699. static long ibmvscsis_establish_new_q(struct scsi_info *vscsi)
  700. {
  701. long rc = ADAPT_SUCCESS;
  702. uint format;
  703. vscsi->flags &= PRESERVE_FLAG_FIELDS;
  704. vscsi->rsp_q_timer.timer_pops = 0;
  705. vscsi->debit = 0;
  706. vscsi->credit = 0;
  707. rc = vio_enable_interrupts(vscsi->dma_dev);
  708. if (rc) {
  709. pr_warn("establish_new_q: failed to enable interrupts, rc %ld\n",
  710. rc);
  711. return rc;
  712. }
  713. rc = ibmvscsis_check_init_msg(vscsi, &format);
  714. if (rc) {
  715. dev_err(&vscsi->dev, "establish_new_q: check_init_msg failed, rc %ld\n",
  716. rc);
  717. return rc;
  718. }
  719. if (format == UNUSED_FORMAT) {
  720. rc = ibmvscsis_send_init_message(vscsi, INIT_MSG);
  721. switch (rc) {
  722. case H_SUCCESS:
  723. case H_DROPPED:
  724. case H_CLOSED:
  725. rc = ADAPT_SUCCESS;
  726. break;
  727. case H_PARAMETER:
  728. case H_HARDWARE:
  729. break;
  730. default:
  731. vscsi->state = UNDEFINED;
  732. rc = H_HARDWARE;
  733. break;
  734. }
  735. } else if (format == INIT_MSG) {
  736. rc = ibmvscsis_handle_init_msg(vscsi);
  737. }
  738. return rc;
  739. }
  740. /**
  741. * ibmvscsis_reset_queue() - Reset CRQ Queue
  742. * @vscsi: Pointer to our adapter structure
  743. *
  744. * This function calls h_free_q and then calls h_reg_q and does all
  745. * of the bookkeeping to get us back to where we can communicate.
  746. *
  747. * Actually, we don't always call h_free_crq. A problem was discovered
  748. * where one partition would close and reopen his queue, which would
  749. * cause his partner to get a transport event, which would cause him to
  750. * close and reopen his queue, which would cause the original partition
  751. * to get a transport event, etc., etc. To prevent this, we don't
  752. * actually close our queue if the client initiated the reset, (i.e.
  753. * either we got a transport event or we have detected that the client's
  754. * queue is gone)
  755. *
  756. * EXECUTION ENVIRONMENT:
  757. * Process environment, called with interrupt lock held
  758. */
  759. static void ibmvscsis_reset_queue(struct scsi_info *vscsi)
  760. {
  761. int bytes;
  762. long rc = ADAPT_SUCCESS;
  763. pr_debug("reset_queue: flags 0x%x\n", vscsi->flags);
  764. /* don't reset, the client did it for us */
  765. if (vscsi->flags & (CLIENT_FAILED | TRANS_EVENT)) {
  766. vscsi->flags &= PRESERVE_FLAG_FIELDS;
  767. vscsi->rsp_q_timer.timer_pops = 0;
  768. vscsi->debit = 0;
  769. vscsi->credit = 0;
  770. vscsi->state = WAIT_CONNECTION;
  771. vio_enable_interrupts(vscsi->dma_dev);
  772. } else {
  773. rc = ibmvscsis_free_command_q(vscsi);
  774. if (rc == ADAPT_SUCCESS) {
  775. vscsi->state = WAIT_CONNECTION;
  776. bytes = vscsi->cmd_q.size * PAGE_SIZE;
  777. rc = h_reg_crq(vscsi->dds.unit_id,
  778. vscsi->cmd_q.crq_token, bytes);
  779. if (rc == H_CLOSED || rc == H_SUCCESS) {
  780. rc = ibmvscsis_establish_new_q(vscsi);
  781. }
  782. if (rc != ADAPT_SUCCESS) {
  783. pr_debug("reset_queue: reg_crq rc %ld\n", rc);
  784. vscsi->state = ERR_DISCONNECTED;
  785. vscsi->flags |= RESPONSE_Q_DOWN;
  786. ibmvscsis_free_command_q(vscsi);
  787. }
  788. } else {
  789. vscsi->state = ERR_DISCONNECTED;
  790. vscsi->flags |= RESPONSE_Q_DOWN;
  791. }
  792. }
  793. }
  794. /**
  795. * ibmvscsis_free_cmd_resources() - Free command resources
  796. * @vscsi: Pointer to our adapter structure
  797. * @cmd: Command which is not longer in use
  798. *
  799. * Must be called with interrupt lock held.
  800. */
  801. static void ibmvscsis_free_cmd_resources(struct scsi_info *vscsi,
  802. struct ibmvscsis_cmd *cmd)
  803. {
  804. struct iu_entry *iue = cmd->iue;
  805. switch (cmd->type) {
  806. case TASK_MANAGEMENT:
  807. case SCSI_CDB:
  808. /*
  809. * When the queue goes down this value is cleared, so it
  810. * cannot be cleared in this general purpose function.
  811. */
  812. if (vscsi->debit)
  813. vscsi->debit -= 1;
  814. break;
  815. case ADAPTER_MAD:
  816. vscsi->flags &= ~PROCESSING_MAD;
  817. break;
  818. case UNSET_TYPE:
  819. break;
  820. default:
  821. dev_err(&vscsi->dev, "free_cmd_resources unknown type %d\n",
  822. cmd->type);
  823. break;
  824. }
  825. cmd->iue = NULL;
  826. list_add_tail(&cmd->list, &vscsi->free_cmd);
  827. srp_iu_put(iue);
  828. if (list_empty(&vscsi->active_q) && list_empty(&vscsi->schedule_q) &&
  829. list_empty(&vscsi->waiting_rsp) && (vscsi->flags & WAIT_FOR_IDLE)) {
  830. vscsi->flags &= ~WAIT_FOR_IDLE;
  831. complete(&vscsi->wait_idle);
  832. }
  833. }
  834. /**
  835. * ibmvscsis_trans_event() - Handle a Transport Event
  836. * @vscsi: Pointer to our adapter structure
  837. * @crq: Pointer to CRQ entry containing the Transport Event
  838. *
  839. * Do the logic to close the I_T nexus. This function may not
  840. * behave to specification.
  841. *
  842. * EXECUTION ENVIRONMENT:
  843. * Interrupt, interrupt lock held
  844. */
  845. static long ibmvscsis_trans_event(struct scsi_info *vscsi,
  846. struct viosrp_crq *crq)
  847. {
  848. long rc = ADAPT_SUCCESS;
  849. pr_debug("trans_event: format %d, flags 0x%x, state 0x%hx\n",
  850. (int)crq->format, vscsi->flags, vscsi->state);
  851. switch (crq->format) {
  852. case MIGRATED:
  853. case PARTNER_FAILED:
  854. case PARTNER_DEREGISTER:
  855. ibmvscsis_delete_client_info(vscsi, true);
  856. break;
  857. default:
  858. rc = ERROR;
  859. dev_err(&vscsi->dev, "trans_event: invalid format %d\n",
  860. (uint)crq->format);
  861. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT,
  862. RESPONSE_Q_DOWN);
  863. break;
  864. }
  865. if (rc == ADAPT_SUCCESS) {
  866. switch (vscsi->state) {
  867. case NO_QUEUE:
  868. case ERR_DISCONNECTED:
  869. case UNDEFINED:
  870. break;
  871. case UNCONFIGURING:
  872. vscsi->flags |= (RESPONSE_Q_DOWN | TRANS_EVENT);
  873. break;
  874. case WAIT_ENABLED:
  875. break;
  876. case WAIT_CONNECTION:
  877. break;
  878. case CONNECTED:
  879. ibmvscsis_post_disconnect(vscsi, WAIT_IDLE,
  880. (RESPONSE_Q_DOWN |
  881. TRANS_EVENT));
  882. break;
  883. case SRP_PROCESSING:
  884. if ((vscsi->debit > 0) ||
  885. !list_empty(&vscsi->schedule_q) ||
  886. !list_empty(&vscsi->waiting_rsp) ||
  887. !list_empty(&vscsi->active_q)) {
  888. pr_debug("debit %d, sched %d, wait %d, active %d\n",
  889. vscsi->debit,
  890. (int)list_empty(&vscsi->schedule_q),
  891. (int)list_empty(&vscsi->waiting_rsp),
  892. (int)list_empty(&vscsi->active_q));
  893. pr_warn("connection lost with outstanding work\n");
  894. } else {
  895. pr_debug("trans_event: SRP Processing, but no outstanding work\n");
  896. }
  897. ibmvscsis_post_disconnect(vscsi, WAIT_IDLE,
  898. (RESPONSE_Q_DOWN |
  899. TRANS_EVENT));
  900. break;
  901. case ERR_DISCONNECT:
  902. case ERR_DISCONNECT_RECONNECT:
  903. case WAIT_IDLE:
  904. vscsi->flags |= (RESPONSE_Q_DOWN | TRANS_EVENT);
  905. break;
  906. }
  907. }
  908. rc = vscsi->flags & SCHEDULE_DISCONNECT;
  909. pr_debug("Leaving trans_event: flags 0x%x, state 0x%hx, rc %ld\n",
  910. vscsi->flags, vscsi->state, rc);
  911. return rc;
  912. }
  913. /**
  914. * ibmvscsis_poll_cmd_q() - Poll Command Queue
  915. * @vscsi: Pointer to our adapter structure
  916. *
  917. * Called to handle command elements that may have arrived while
  918. * interrupts were disabled.
  919. *
  920. * EXECUTION ENVIRONMENT:
  921. * intr_lock must be held
  922. */
  923. static void ibmvscsis_poll_cmd_q(struct scsi_info *vscsi)
  924. {
  925. struct viosrp_crq *crq;
  926. long rc;
  927. bool ack = true;
  928. volatile u8 valid;
  929. pr_debug("poll_cmd_q: flags 0x%x, state 0x%hx, q index %ud\n",
  930. vscsi->flags, vscsi->state, vscsi->cmd_q.index);
  931. rc = vscsi->flags & SCHEDULE_DISCONNECT;
  932. crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
  933. valid = crq->valid;
  934. dma_rmb();
  935. while (valid) {
  936. poll_work:
  937. vscsi->cmd_q.index =
  938. (vscsi->cmd_q.index + 1) & vscsi->cmd_q.mask;
  939. if (!rc) {
  940. rc = ibmvscsis_parse_command(vscsi, crq);
  941. } else {
  942. if ((uint)crq->valid == VALID_TRANS_EVENT) {
  943. /*
  944. * must service the transport layer events even
  945. * in an error state, dont break out until all
  946. * the consecutive transport events have been
  947. * processed
  948. */
  949. rc = ibmvscsis_trans_event(vscsi, crq);
  950. } else if (vscsi->flags & TRANS_EVENT) {
  951. /*
  952. * if a tranport event has occurred leave
  953. * everything but transport events on the queue
  954. */
  955. pr_debug("poll_cmd_q, ignoring\n");
  956. /*
  957. * need to decrement the queue index so we can
  958. * look at the elment again
  959. */
  960. if (vscsi->cmd_q.index)
  961. vscsi->cmd_q.index -= 1;
  962. else
  963. /*
  964. * index is at 0 it just wrapped.
  965. * have it index last element in q
  966. */
  967. vscsi->cmd_q.index = vscsi->cmd_q.mask;
  968. break;
  969. }
  970. }
  971. crq->valid = INVALIDATE_CMD_RESP_EL;
  972. crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
  973. valid = crq->valid;
  974. dma_rmb();
  975. }
  976. if (!rc) {
  977. if (ack) {
  978. vio_enable_interrupts(vscsi->dma_dev);
  979. ack = false;
  980. pr_debug("poll_cmd_q, reenabling interrupts\n");
  981. }
  982. valid = crq->valid;
  983. dma_rmb();
  984. if (valid)
  985. goto poll_work;
  986. }
  987. pr_debug("Leaving poll_cmd_q: rc %ld\n", rc);
  988. }
  989. /**
  990. * ibmvscsis_free_cmd_qs() - Free elements in queue
  991. * @vscsi: Pointer to our adapter structure
  992. *
  993. * Free all of the elements on all queues that are waiting for
  994. * whatever reason.
  995. *
  996. * PRECONDITION:
  997. * Called with interrupt lock held
  998. */
  999. static void ibmvscsis_free_cmd_qs(struct scsi_info *vscsi)
  1000. {
  1001. struct ibmvscsis_cmd *cmd, *nxt;
  1002. pr_debug("free_cmd_qs: waiting_rsp empty %d, timer starter %d\n",
  1003. (int)list_empty(&vscsi->waiting_rsp),
  1004. vscsi->rsp_q_timer.started);
  1005. list_for_each_entry_safe(cmd, nxt, &vscsi->waiting_rsp, list) {
  1006. list_del(&cmd->list);
  1007. ibmvscsis_free_cmd_resources(vscsi, cmd);
  1008. }
  1009. }
  1010. /**
  1011. * ibmvscsis_get_free_cmd() - Get free command from list
  1012. * @vscsi: Pointer to our adapter structure
  1013. *
  1014. * Must be called with interrupt lock held.
  1015. */
  1016. static struct ibmvscsis_cmd *ibmvscsis_get_free_cmd(struct scsi_info *vscsi)
  1017. {
  1018. struct ibmvscsis_cmd *cmd = NULL;
  1019. struct iu_entry *iue;
  1020. iue = srp_iu_get(&vscsi->target);
  1021. if (iue) {
  1022. cmd = list_first_entry_or_null(&vscsi->free_cmd,
  1023. struct ibmvscsis_cmd, list);
  1024. if (cmd) {
  1025. if (cmd->abort_cmd)
  1026. cmd->abort_cmd = NULL;
  1027. cmd->flags &= ~(DELAY_SEND);
  1028. list_del(&cmd->list);
  1029. cmd->iue = iue;
  1030. cmd->type = UNSET_TYPE;
  1031. memset(&cmd->se_cmd, 0, sizeof(cmd->se_cmd));
  1032. } else {
  1033. srp_iu_put(iue);
  1034. }
  1035. }
  1036. return cmd;
  1037. }
  1038. /**
  1039. * ibmvscsis_adapter_idle() - Helper function to handle idle adapter
  1040. * @vscsi: Pointer to our adapter structure
  1041. *
  1042. * This function is called when the adapter is idle when the driver
  1043. * is attempting to clear an error condition.
  1044. * The adapter is considered busy if any of its cmd queues
  1045. * are non-empty. This function can be invoked
  1046. * from the off level disconnect function.
  1047. *
  1048. * EXECUTION ENVIRONMENT:
  1049. * Process environment called with interrupt lock held
  1050. */
  1051. static void ibmvscsis_adapter_idle(struct scsi_info *vscsi)
  1052. {
  1053. int free_qs = false;
  1054. pr_debug("adapter_idle: flags 0x%x, state 0x%hx\n", vscsi->flags,
  1055. vscsi->state);
  1056. /* Only need to free qs if we're disconnecting from client */
  1057. if (vscsi->state != WAIT_CONNECTION || vscsi->flags & TRANS_EVENT)
  1058. free_qs = true;
  1059. switch (vscsi->state) {
  1060. case UNCONFIGURING:
  1061. ibmvscsis_free_command_q(vscsi);
  1062. dma_rmb();
  1063. isync();
  1064. if (vscsi->flags & CFG_SLEEPING) {
  1065. vscsi->flags &= ~CFG_SLEEPING;
  1066. complete(&vscsi->unconfig);
  1067. }
  1068. break;
  1069. case ERR_DISCONNECT_RECONNECT:
  1070. ibmvscsis_reset_queue(vscsi);
  1071. pr_debug("adapter_idle, disc_rec: flags 0x%x\n", vscsi->flags);
  1072. break;
  1073. case ERR_DISCONNECT:
  1074. ibmvscsis_free_command_q(vscsi);
  1075. vscsi->flags &= ~(SCHEDULE_DISCONNECT | DISCONNECT_SCHEDULED);
  1076. vscsi->flags |= RESPONSE_Q_DOWN;
  1077. if (vscsi->tport.enabled)
  1078. vscsi->state = ERR_DISCONNECTED;
  1079. else
  1080. vscsi->state = WAIT_ENABLED;
  1081. pr_debug("adapter_idle, disc: flags 0x%x, state 0x%hx\n",
  1082. vscsi->flags, vscsi->state);
  1083. break;
  1084. case WAIT_IDLE:
  1085. vscsi->rsp_q_timer.timer_pops = 0;
  1086. vscsi->debit = 0;
  1087. vscsi->credit = 0;
  1088. if (vscsi->flags & TRANS_EVENT) {
  1089. vscsi->state = WAIT_CONNECTION;
  1090. vscsi->flags &= PRESERVE_FLAG_FIELDS;
  1091. } else {
  1092. vscsi->state = CONNECTED;
  1093. vscsi->flags &= ~DISCONNECT_SCHEDULED;
  1094. }
  1095. pr_debug("adapter_idle, wait: flags 0x%x, state 0x%hx\n",
  1096. vscsi->flags, vscsi->state);
  1097. ibmvscsis_poll_cmd_q(vscsi);
  1098. break;
  1099. case ERR_DISCONNECTED:
  1100. vscsi->flags &= ~DISCONNECT_SCHEDULED;
  1101. pr_debug("adapter_idle, disconnected: flags 0x%x, state 0x%hx\n",
  1102. vscsi->flags, vscsi->state);
  1103. break;
  1104. default:
  1105. dev_err(&vscsi->dev, "adapter_idle: in invalid state %d\n",
  1106. vscsi->state);
  1107. break;
  1108. }
  1109. if (free_qs)
  1110. ibmvscsis_free_cmd_qs(vscsi);
  1111. /*
  1112. * There is a timing window where we could lose a disconnect request.
  1113. * The known path to this window occurs during the DISCONNECT_RECONNECT
  1114. * case above: reset_queue calls free_command_q, which will release the
  1115. * interrupt lock. During that time, a new post_disconnect call can be
  1116. * made with a "more severe" state (DISCONNECT or UNCONFIGURING).
  1117. * Because the DISCONNECT_SCHEDULED flag is already set, post_disconnect
  1118. * will only set the new_state. Now free_command_q reacquires the intr
  1119. * lock and clears the DISCONNECT_SCHEDULED flag (using PRESERVE_FLAG_
  1120. * FIELDS), and the disconnect is lost. This is particularly bad when
  1121. * the new disconnect was for UNCONFIGURING, since the unconfigure hangs
  1122. * forever.
  1123. * Fix is that free command queue sets acr state and acr flags if there
  1124. * is a change under the lock
  1125. * note free command queue writes to this state it clears it
  1126. * before releasing the lock, different drivers call the free command
  1127. * queue different times so dont initialize above
  1128. */
  1129. if (vscsi->phyp_acr_state != 0) {
  1130. /*
  1131. * set any bits in flags that may have been cleared by
  1132. * a call to free command queue in switch statement
  1133. * or reset queue
  1134. */
  1135. vscsi->flags |= vscsi->phyp_acr_flags;
  1136. ibmvscsis_post_disconnect(vscsi, vscsi->phyp_acr_state, 0);
  1137. vscsi->phyp_acr_state = 0;
  1138. vscsi->phyp_acr_flags = 0;
  1139. pr_debug("adapter_idle: flags 0x%x, state 0x%hx, acr_flags 0x%x, acr_state 0x%hx\n",
  1140. vscsi->flags, vscsi->state, vscsi->phyp_acr_flags,
  1141. vscsi->phyp_acr_state);
  1142. }
  1143. pr_debug("Leaving adapter_idle: flags 0x%x, state 0x%hx, new_state 0x%x\n",
  1144. vscsi->flags, vscsi->state, vscsi->new_state);
  1145. }
  1146. /**
  1147. * ibmvscsis_copy_crq_packet() - Copy CRQ Packet
  1148. * @vscsi: Pointer to our adapter structure
  1149. * @cmd: Pointer to command element to use to process the request
  1150. * @crq: Pointer to CRQ entry containing the request
  1151. *
  1152. * Copy the srp information unit from the hosted
  1153. * partition using remote dma
  1154. *
  1155. * EXECUTION ENVIRONMENT:
  1156. * Interrupt, interrupt lock held
  1157. */
  1158. static long ibmvscsis_copy_crq_packet(struct scsi_info *vscsi,
  1159. struct ibmvscsis_cmd *cmd,
  1160. struct viosrp_crq *crq)
  1161. {
  1162. struct iu_entry *iue = cmd->iue;
  1163. long rc = 0;
  1164. u16 len;
  1165. len = be16_to_cpu(crq->IU_length);
  1166. if ((len > SRP_MAX_IU_LEN) || (len == 0)) {
  1167. dev_err(&vscsi->dev, "copy_crq: Invalid len %d passed", len);
  1168. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  1169. return SRP_VIOLATION;
  1170. }
  1171. rc = h_copy_rdma(len, vscsi->dds.window[REMOTE].liobn,
  1172. be64_to_cpu(crq->IU_data_ptr),
  1173. vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma);
  1174. switch (rc) {
  1175. case H_SUCCESS:
  1176. cmd->init_time = mftb();
  1177. iue->remote_token = crq->IU_data_ptr;
  1178. iue->iu_len = len;
  1179. pr_debug("copy_crq: ioba 0x%llx, init_time 0x%llx\n",
  1180. be64_to_cpu(crq->IU_data_ptr), cmd->init_time);
  1181. break;
  1182. case H_PERMISSION:
  1183. if (connection_broken(vscsi))
  1184. ibmvscsis_post_disconnect(vscsi,
  1185. ERR_DISCONNECT_RECONNECT,
  1186. (RESPONSE_Q_DOWN |
  1187. CLIENT_FAILED));
  1188. else
  1189. ibmvscsis_post_disconnect(vscsi,
  1190. ERR_DISCONNECT_RECONNECT, 0);
  1191. dev_err(&vscsi->dev, "copy_crq: h_copy_rdma failed, rc %ld\n",
  1192. rc);
  1193. break;
  1194. case H_DEST_PARM:
  1195. case H_SOURCE_PARM:
  1196. default:
  1197. dev_err(&vscsi->dev, "copy_crq: h_copy_rdma failed, rc %ld\n",
  1198. rc);
  1199. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  1200. break;
  1201. }
  1202. return rc;
  1203. }
  1204. /**
  1205. * ibmvscsis_adapter_info - Service an Adapter Info MAnagement Data gram
  1206. * @vscsi: Pointer to our adapter structure
  1207. * @iue: Information Unit containing the Adapter Info MAD request
  1208. *
  1209. * EXECUTION ENVIRONMENT:
  1210. * Interrupt adapter lock is held
  1211. */
  1212. static long ibmvscsis_adapter_info(struct scsi_info *vscsi,
  1213. struct iu_entry *iue)
  1214. {
  1215. struct viosrp_adapter_info *mad = &vio_iu(iue)->mad.adapter_info;
  1216. struct mad_adapter_info_data *info;
  1217. uint flag_bits = 0;
  1218. dma_addr_t token;
  1219. long rc;
  1220. mad->common.status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
  1221. if (be16_to_cpu(mad->common.length) > sizeof(*info)) {
  1222. mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
  1223. return 0;
  1224. }
  1225. info = dma_alloc_coherent(&vscsi->dma_dev->dev, sizeof(*info), &token,
  1226. GFP_ATOMIC);
  1227. if (!info) {
  1228. dev_err(&vscsi->dev, "bad dma_alloc_coherent %p\n",
  1229. iue->target);
  1230. mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
  1231. return 0;
  1232. }
  1233. /* Get remote info */
  1234. rc = h_copy_rdma(be16_to_cpu(mad->common.length),
  1235. vscsi->dds.window[REMOTE].liobn,
  1236. be64_to_cpu(mad->buffer),
  1237. vscsi->dds.window[LOCAL].liobn, token);
  1238. if (rc != H_SUCCESS) {
  1239. if (rc == H_PERMISSION) {
  1240. if (connection_broken(vscsi))
  1241. flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
  1242. }
  1243. pr_warn("adapter_info: h_copy_rdma from client failed, rc %ld\n",
  1244. rc);
  1245. pr_debug("adapter_info: ioba 0x%llx, flags 0x%x, flag_bits 0x%x\n",
  1246. be64_to_cpu(mad->buffer), vscsi->flags, flag_bits);
  1247. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
  1248. flag_bits);
  1249. goto free_dma;
  1250. }
  1251. /*
  1252. * Copy client info, but ignore partition number, which we
  1253. * already got from phyp - unless we failed to get it from
  1254. * phyp (e.g. if we're running on a p5 system).
  1255. */
  1256. if (vscsi->client_data.partition_number == 0)
  1257. vscsi->client_data.partition_number =
  1258. be32_to_cpu(info->partition_number);
  1259. strncpy(vscsi->client_data.srp_version, info->srp_version,
  1260. sizeof(vscsi->client_data.srp_version));
  1261. strncpy(vscsi->client_data.partition_name, info->partition_name,
  1262. sizeof(vscsi->client_data.partition_name));
  1263. vscsi->client_data.mad_version = be32_to_cpu(info->mad_version);
  1264. vscsi->client_data.os_type = be32_to_cpu(info->os_type);
  1265. /* Copy our info */
  1266. strncpy(info->srp_version, SRP_VERSION,
  1267. sizeof(info->srp_version));
  1268. strncpy(info->partition_name, vscsi->dds.partition_name,
  1269. sizeof(info->partition_name));
  1270. info->partition_number = cpu_to_be32(vscsi->dds.partition_num);
  1271. info->mad_version = cpu_to_be32(MAD_VERSION_1);
  1272. info->os_type = cpu_to_be32(LINUX);
  1273. memset(&info->port_max_txu[0], 0, sizeof(info->port_max_txu));
  1274. info->port_max_txu[0] = cpu_to_be32(MAX_TXU);
  1275. dma_wmb();
  1276. rc = h_copy_rdma(sizeof(*info), vscsi->dds.window[LOCAL].liobn,
  1277. token, vscsi->dds.window[REMOTE].liobn,
  1278. be64_to_cpu(mad->buffer));
  1279. switch (rc) {
  1280. case H_SUCCESS:
  1281. break;
  1282. case H_SOURCE_PARM:
  1283. case H_DEST_PARM:
  1284. case H_PERMISSION:
  1285. if (connection_broken(vscsi))
  1286. flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
  1287. default:
  1288. dev_err(&vscsi->dev, "adapter_info: h_copy_rdma to client failed, rc %ld\n",
  1289. rc);
  1290. ibmvscsis_post_disconnect(vscsi,
  1291. ERR_DISCONNECT_RECONNECT,
  1292. flag_bits);
  1293. break;
  1294. }
  1295. free_dma:
  1296. dma_free_coherent(&vscsi->dma_dev->dev, sizeof(*info), info, token);
  1297. pr_debug("Leaving adapter_info, rc %ld\n", rc);
  1298. return rc;
  1299. }
  1300. /**
  1301. * ibmvscsis_cap_mad() - Service a Capabilities MAnagement Data gram
  1302. * @vscsi: Pointer to our adapter structure
  1303. * @iue: Information Unit containing the Capabilities MAD request
  1304. *
  1305. * NOTE: if you return an error from this routine you must be
  1306. * disconnecting or you will cause a hang
  1307. *
  1308. * EXECUTION ENVIRONMENT:
  1309. * Interrupt called with adapter lock held
  1310. */
  1311. static int ibmvscsis_cap_mad(struct scsi_info *vscsi, struct iu_entry *iue)
  1312. {
  1313. struct viosrp_capabilities *mad = &vio_iu(iue)->mad.capabilities;
  1314. struct capabilities *cap;
  1315. struct mad_capability_common *common;
  1316. dma_addr_t token;
  1317. u16 olen, len, status, min_len, cap_len;
  1318. u32 flag;
  1319. uint flag_bits = 0;
  1320. long rc = 0;
  1321. olen = be16_to_cpu(mad->common.length);
  1322. /*
  1323. * struct capabilities hardcodes a couple capabilities after the
  1324. * header, but the capabilities can actually be in any order.
  1325. */
  1326. min_len = offsetof(struct capabilities, migration);
  1327. if ((olen < min_len) || (olen > PAGE_SIZE)) {
  1328. pr_warn("cap_mad: invalid len %d\n", olen);
  1329. mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
  1330. return 0;
  1331. }
  1332. cap = dma_alloc_coherent(&vscsi->dma_dev->dev, olen, &token,
  1333. GFP_ATOMIC);
  1334. if (!cap) {
  1335. dev_err(&vscsi->dev, "bad dma_alloc_coherent %p\n",
  1336. iue->target);
  1337. mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
  1338. return 0;
  1339. }
  1340. rc = h_copy_rdma(olen, vscsi->dds.window[REMOTE].liobn,
  1341. be64_to_cpu(mad->buffer),
  1342. vscsi->dds.window[LOCAL].liobn, token);
  1343. if (rc == H_SUCCESS) {
  1344. strncpy(cap->name, dev_name(&vscsi->dma_dev->dev),
  1345. SRP_MAX_LOC_LEN);
  1346. len = olen - min_len;
  1347. status = VIOSRP_MAD_SUCCESS;
  1348. common = (struct mad_capability_common *)&cap->migration;
  1349. while ((len > 0) && (status == VIOSRP_MAD_SUCCESS) && !rc) {
  1350. pr_debug("cap_mad: len left %hd, cap type %d, cap len %hd\n",
  1351. len, be32_to_cpu(common->cap_type),
  1352. be16_to_cpu(common->length));
  1353. cap_len = be16_to_cpu(common->length);
  1354. if (cap_len > len) {
  1355. dev_err(&vscsi->dev, "cap_mad: cap len mismatch with total len\n");
  1356. status = VIOSRP_MAD_FAILED;
  1357. break;
  1358. }
  1359. if (cap_len == 0) {
  1360. dev_err(&vscsi->dev, "cap_mad: cap len is 0\n");
  1361. status = VIOSRP_MAD_FAILED;
  1362. break;
  1363. }
  1364. switch (common->cap_type) {
  1365. default:
  1366. pr_debug("cap_mad: unsupported capability\n");
  1367. common->server_support = 0;
  1368. flag = cpu_to_be32((u32)CAP_LIST_SUPPORTED);
  1369. cap->flags &= ~flag;
  1370. break;
  1371. }
  1372. len = len - cap_len;
  1373. common = (struct mad_capability_common *)
  1374. ((char *)common + cap_len);
  1375. }
  1376. mad->common.status = cpu_to_be16(status);
  1377. dma_wmb();
  1378. rc = h_copy_rdma(olen, vscsi->dds.window[LOCAL].liobn, token,
  1379. vscsi->dds.window[REMOTE].liobn,
  1380. be64_to_cpu(mad->buffer));
  1381. if (rc != H_SUCCESS) {
  1382. pr_debug("cap_mad: failed to copy to client, rc %ld\n",
  1383. rc);
  1384. if (rc == H_PERMISSION) {
  1385. if (connection_broken(vscsi))
  1386. flag_bits = (RESPONSE_Q_DOWN |
  1387. CLIENT_FAILED);
  1388. }
  1389. pr_warn("cap_mad: error copying data to client, rc %ld\n",
  1390. rc);
  1391. ibmvscsis_post_disconnect(vscsi,
  1392. ERR_DISCONNECT_RECONNECT,
  1393. flag_bits);
  1394. }
  1395. }
  1396. dma_free_coherent(&vscsi->dma_dev->dev, olen, cap, token);
  1397. pr_debug("Leaving cap_mad, rc %ld, client_cap 0x%x\n",
  1398. rc, vscsi->client_cap);
  1399. return rc;
  1400. }
  1401. /**
  1402. * ibmvscsis_process_mad() - Service a MAnagement Data gram
  1403. * @vscsi: Pointer to our adapter structure
  1404. * @iue: Information Unit containing the MAD request
  1405. *
  1406. * Must be called with interrupt lock held.
  1407. */
  1408. static long ibmvscsis_process_mad(struct scsi_info *vscsi, struct iu_entry *iue)
  1409. {
  1410. struct mad_common *mad = (struct mad_common *)&vio_iu(iue)->mad;
  1411. struct viosrp_empty_iu *empty;
  1412. long rc = ADAPT_SUCCESS;
  1413. switch (be32_to_cpu(mad->type)) {
  1414. case VIOSRP_EMPTY_IU_TYPE:
  1415. empty = &vio_iu(iue)->mad.empty_iu;
  1416. vscsi->empty_iu_id = be64_to_cpu(empty->buffer);
  1417. vscsi->empty_iu_tag = be64_to_cpu(empty->common.tag);
  1418. mad->status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
  1419. break;
  1420. case VIOSRP_ADAPTER_INFO_TYPE:
  1421. rc = ibmvscsis_adapter_info(vscsi, iue);
  1422. break;
  1423. case VIOSRP_CAPABILITIES_TYPE:
  1424. rc = ibmvscsis_cap_mad(vscsi, iue);
  1425. break;
  1426. case VIOSRP_ENABLE_FAST_FAIL:
  1427. if (vscsi->state == CONNECTED) {
  1428. vscsi->fast_fail = true;
  1429. mad->status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
  1430. } else {
  1431. pr_warn("fast fail mad sent after login\n");
  1432. mad->status = cpu_to_be16(VIOSRP_MAD_FAILED);
  1433. }
  1434. break;
  1435. default:
  1436. mad->status = cpu_to_be16(VIOSRP_MAD_NOT_SUPPORTED);
  1437. break;
  1438. }
  1439. return rc;
  1440. }
  1441. /**
  1442. * srp_snd_msg_failed() - Handle an error when sending a response
  1443. * @vscsi: Pointer to our adapter structure
  1444. * @rc: The return code from the h_send_crq command
  1445. *
  1446. * Must be called with interrupt lock held.
  1447. */
  1448. static void srp_snd_msg_failed(struct scsi_info *vscsi, long rc)
  1449. {
  1450. ktime_t kt;
  1451. if (rc != H_DROPPED) {
  1452. ibmvscsis_free_cmd_qs(vscsi);
  1453. if (rc == H_CLOSED)
  1454. vscsi->flags |= CLIENT_FAILED;
  1455. /* don't flag the same problem multiple times */
  1456. if (!(vscsi->flags & RESPONSE_Q_DOWN)) {
  1457. vscsi->flags |= RESPONSE_Q_DOWN;
  1458. if (!(vscsi->state & (ERR_DISCONNECT |
  1459. ERR_DISCONNECT_RECONNECT |
  1460. ERR_DISCONNECTED | UNDEFINED))) {
  1461. dev_err(&vscsi->dev, "snd_msg_failed: setting RESPONSE_Q_DOWN, state 0x%hx, flags 0x%x, rc %ld\n",
  1462. vscsi->state, vscsi->flags, rc);
  1463. }
  1464. ibmvscsis_post_disconnect(vscsi,
  1465. ERR_DISCONNECT_RECONNECT, 0);
  1466. }
  1467. return;
  1468. }
  1469. /*
  1470. * The response queue is full.
  1471. * If the server is processing SRP requests, i.e.
  1472. * the client has successfully done an
  1473. * SRP_LOGIN, then it will wait forever for room in
  1474. * the queue. However if the system admin
  1475. * is attempting to unconfigure the server then one
  1476. * or more children will be in a state where
  1477. * they are being removed. So if there is even one
  1478. * child being removed then the driver assumes
  1479. * the system admin is attempting to break the
  1480. * connection with the client and MAX_TIMER_POPS
  1481. * is honored.
  1482. */
  1483. if ((vscsi->rsp_q_timer.timer_pops < MAX_TIMER_POPS) ||
  1484. (vscsi->state == SRP_PROCESSING)) {
  1485. pr_debug("snd_msg_failed: response queue full, flags 0x%x, timer started %d, pops %d\n",
  1486. vscsi->flags, (int)vscsi->rsp_q_timer.started,
  1487. vscsi->rsp_q_timer.timer_pops);
  1488. /*
  1489. * Check if the timer is running; if it
  1490. * is not then start it up.
  1491. */
  1492. if (!vscsi->rsp_q_timer.started) {
  1493. if (vscsi->rsp_q_timer.timer_pops <
  1494. MAX_TIMER_POPS) {
  1495. kt = ktime_set(0, WAIT_NANO_SECONDS);
  1496. } else {
  1497. /*
  1498. * slide the timeslice if the maximum
  1499. * timer pops have already happened
  1500. */
  1501. kt = ktime_set(WAIT_SECONDS, 0);
  1502. }
  1503. vscsi->rsp_q_timer.started = true;
  1504. hrtimer_start(&vscsi->rsp_q_timer.timer, kt,
  1505. HRTIMER_MODE_REL);
  1506. }
  1507. } else {
  1508. /*
  1509. * TBD: Do we need to worry about this? Need to get
  1510. * remove working.
  1511. */
  1512. /*
  1513. * waited a long time and it appears the system admin
  1514. * is bring this driver down
  1515. */
  1516. vscsi->flags |= RESPONSE_Q_DOWN;
  1517. ibmvscsis_free_cmd_qs(vscsi);
  1518. /*
  1519. * if the driver is already attempting to disconnect
  1520. * from the client and has already logged an error
  1521. * trace this event but don't put it in the error log
  1522. */
  1523. if (!(vscsi->state & (ERR_DISCONNECT |
  1524. ERR_DISCONNECT_RECONNECT |
  1525. ERR_DISCONNECTED | UNDEFINED))) {
  1526. dev_err(&vscsi->dev, "client crq full too long\n");
  1527. ibmvscsis_post_disconnect(vscsi,
  1528. ERR_DISCONNECT_RECONNECT,
  1529. 0);
  1530. }
  1531. }
  1532. }
  1533. /**
  1534. * ibmvscsis_send_messages() - Send a Response
  1535. * @vscsi: Pointer to our adapter structure
  1536. *
  1537. * Send a response, first checking the waiting queue. Responses are
  1538. * sent in order they are received. If the response cannot be sent,
  1539. * because the client queue is full, it stays on the waiting queue.
  1540. *
  1541. * PRECONDITION:
  1542. * Called with interrupt lock held
  1543. */
  1544. static void ibmvscsis_send_messages(struct scsi_info *vscsi)
  1545. {
  1546. u64 msg_hi = 0;
  1547. /* note do not attempt to access the IU_data_ptr with this pointer
  1548. * it is not valid
  1549. */
  1550. struct viosrp_crq *crq = (struct viosrp_crq *)&msg_hi;
  1551. struct ibmvscsis_cmd *cmd, *nxt;
  1552. struct iu_entry *iue;
  1553. long rc = ADAPT_SUCCESS;
  1554. bool retry = false;
  1555. if (!(vscsi->flags & RESPONSE_Q_DOWN)) {
  1556. do {
  1557. retry = false;
  1558. list_for_each_entry_safe(cmd, nxt, &vscsi->waiting_rsp,
  1559. list) {
  1560. /*
  1561. * Check to make sure abort cmd gets processed
  1562. * prior to the abort tmr cmd
  1563. */
  1564. if (cmd->flags & DELAY_SEND)
  1565. continue;
  1566. if (cmd->abort_cmd) {
  1567. retry = true;
  1568. cmd->abort_cmd->flags &= ~(DELAY_SEND);
  1569. cmd->abort_cmd = NULL;
  1570. }
  1571. /*
  1572. * If CMD_T_ABORTED w/o CMD_T_TAS scenarios and
  1573. * the case where LIO issued a
  1574. * ABORT_TASK: Sending TMR_TASK_DOES_NOT_EXIST
  1575. * case then we dont send a response, since it
  1576. * was already done.
  1577. */
  1578. if (cmd->se_cmd.transport_state & CMD_T_ABORTED &&
  1579. !(cmd->se_cmd.transport_state & CMD_T_TAS)) {
  1580. list_del(&cmd->list);
  1581. ibmvscsis_free_cmd_resources(vscsi,
  1582. cmd);
  1583. /*
  1584. * With a successfully aborted op
  1585. * through LIO we want to increment the
  1586. * the vscsi credit so that when we dont
  1587. * send a rsp to the original scsi abort
  1588. * op (h_send_crq), but the tm rsp to
  1589. * the abort is sent, the credit is
  1590. * correctly sent with the abort tm rsp.
  1591. * We would need 1 for the abort tm rsp
  1592. * and 1 credit for the aborted scsi op.
  1593. * Thus we need to increment here.
  1594. * Also we want to increment the credit
  1595. * here because we want to make sure
  1596. * cmd is actually released first
  1597. * otherwise the client will think it
  1598. * it can send a new cmd, and we could
  1599. * find ourselves short of cmd elements.
  1600. */
  1601. vscsi->credit += 1;
  1602. } else {
  1603. iue = cmd->iue;
  1604. crq->valid = VALID_CMD_RESP_EL;
  1605. crq->format = cmd->rsp.format;
  1606. if (cmd->flags & CMD_FAST_FAIL)
  1607. crq->status = VIOSRP_ADAPTER_FAIL;
  1608. crq->IU_length = cpu_to_be16(cmd->rsp.len);
  1609. rc = h_send_crq(vscsi->dma_dev->unit_address,
  1610. be64_to_cpu(msg_hi),
  1611. be64_to_cpu(cmd->rsp.tag));
  1612. pr_debug("send_messages: cmd %p, tag 0x%llx, rc %ld\n",
  1613. cmd, be64_to_cpu(cmd->rsp.tag), rc);
  1614. /* if all ok free up the command
  1615. * element resources
  1616. */
  1617. if (rc == H_SUCCESS) {
  1618. /* some movement has occurred */
  1619. vscsi->rsp_q_timer.timer_pops = 0;
  1620. list_del(&cmd->list);
  1621. ibmvscsis_free_cmd_resources(vscsi,
  1622. cmd);
  1623. } else {
  1624. srp_snd_msg_failed(vscsi, rc);
  1625. break;
  1626. }
  1627. }
  1628. }
  1629. } while (retry);
  1630. if (!rc) {
  1631. /*
  1632. * The timer could pop with the queue empty. If
  1633. * this happens, rc will always indicate a
  1634. * success; clear the pop count.
  1635. */
  1636. vscsi->rsp_q_timer.timer_pops = 0;
  1637. }
  1638. } else {
  1639. ibmvscsis_free_cmd_qs(vscsi);
  1640. }
  1641. }
  1642. /* Called with intr lock held */
  1643. static void ibmvscsis_send_mad_resp(struct scsi_info *vscsi,
  1644. struct ibmvscsis_cmd *cmd,
  1645. struct viosrp_crq *crq)
  1646. {
  1647. struct iu_entry *iue = cmd->iue;
  1648. struct mad_common *mad = (struct mad_common *)&vio_iu(iue)->mad;
  1649. uint flag_bits = 0;
  1650. long rc;
  1651. dma_wmb();
  1652. rc = h_copy_rdma(sizeof(struct mad_common),
  1653. vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma,
  1654. vscsi->dds.window[REMOTE].liobn,
  1655. be64_to_cpu(crq->IU_data_ptr));
  1656. if (!rc) {
  1657. cmd->rsp.format = VIOSRP_MAD_FORMAT;
  1658. cmd->rsp.len = sizeof(struct mad_common);
  1659. cmd->rsp.tag = mad->tag;
  1660. list_add_tail(&cmd->list, &vscsi->waiting_rsp);
  1661. ibmvscsis_send_messages(vscsi);
  1662. } else {
  1663. pr_debug("Error sending mad response, rc %ld\n", rc);
  1664. if (rc == H_PERMISSION) {
  1665. if (connection_broken(vscsi))
  1666. flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
  1667. }
  1668. dev_err(&vscsi->dev, "mad: failed to copy to client, rc %ld\n",
  1669. rc);
  1670. ibmvscsis_free_cmd_resources(vscsi, cmd);
  1671. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
  1672. flag_bits);
  1673. }
  1674. }
  1675. /**
  1676. * ibmvscsis_mad() - Service a MAnagement Data gram.
  1677. * @vscsi: Pointer to our adapter structure
  1678. * @crq: Pointer to the CRQ entry containing the MAD request
  1679. *
  1680. * EXECUTION ENVIRONMENT:
  1681. * Interrupt, called with adapter lock held
  1682. */
  1683. static long ibmvscsis_mad(struct scsi_info *vscsi, struct viosrp_crq *crq)
  1684. {
  1685. struct iu_entry *iue;
  1686. struct ibmvscsis_cmd *cmd;
  1687. struct mad_common *mad;
  1688. long rc = ADAPT_SUCCESS;
  1689. switch (vscsi->state) {
  1690. /*
  1691. * We have not exchanged Init Msgs yet, so this MAD was sent
  1692. * before the last Transport Event; client will not be
  1693. * expecting a response.
  1694. */
  1695. case WAIT_CONNECTION:
  1696. pr_debug("mad: in Wait Connection state, ignoring MAD, flags %d\n",
  1697. vscsi->flags);
  1698. return ADAPT_SUCCESS;
  1699. case SRP_PROCESSING:
  1700. case CONNECTED:
  1701. break;
  1702. /*
  1703. * We should never get here while we're in these states.
  1704. * Just log an error and get out.
  1705. */
  1706. case UNCONFIGURING:
  1707. case WAIT_IDLE:
  1708. case ERR_DISCONNECT:
  1709. case ERR_DISCONNECT_RECONNECT:
  1710. default:
  1711. dev_err(&vscsi->dev, "mad: invalid adapter state %d for mad\n",
  1712. vscsi->state);
  1713. return ADAPT_SUCCESS;
  1714. }
  1715. cmd = ibmvscsis_get_free_cmd(vscsi);
  1716. if (!cmd) {
  1717. dev_err(&vscsi->dev, "mad: failed to get cmd, debit %d\n",
  1718. vscsi->debit);
  1719. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  1720. return ERROR;
  1721. }
  1722. iue = cmd->iue;
  1723. cmd->type = ADAPTER_MAD;
  1724. rc = ibmvscsis_copy_crq_packet(vscsi, cmd, crq);
  1725. if (!rc) {
  1726. mad = (struct mad_common *)&vio_iu(iue)->mad;
  1727. pr_debug("mad: type %d\n", be32_to_cpu(mad->type));
  1728. rc = ibmvscsis_process_mad(vscsi, iue);
  1729. pr_debug("mad: status %hd, rc %ld\n", be16_to_cpu(mad->status),
  1730. rc);
  1731. if (!rc)
  1732. ibmvscsis_send_mad_resp(vscsi, cmd, crq);
  1733. } else {
  1734. ibmvscsis_free_cmd_resources(vscsi, cmd);
  1735. }
  1736. pr_debug("Leaving mad, rc %ld\n", rc);
  1737. return rc;
  1738. }
  1739. /**
  1740. * ibmvscsis_login_rsp() - Create/copy a login response notice to the client
  1741. * @vscsi: Pointer to our adapter structure
  1742. * @cmd: Pointer to the command for the SRP Login request
  1743. *
  1744. * EXECUTION ENVIRONMENT:
  1745. * Interrupt, interrupt lock held
  1746. */
  1747. static long ibmvscsis_login_rsp(struct scsi_info *vscsi,
  1748. struct ibmvscsis_cmd *cmd)
  1749. {
  1750. struct iu_entry *iue = cmd->iue;
  1751. struct srp_login_rsp *rsp = &vio_iu(iue)->srp.login_rsp;
  1752. struct format_code *fmt;
  1753. uint flag_bits = 0;
  1754. long rc = ADAPT_SUCCESS;
  1755. memset(rsp, 0, sizeof(struct srp_login_rsp));
  1756. rsp->opcode = SRP_LOGIN_RSP;
  1757. rsp->req_lim_delta = cpu_to_be32(vscsi->request_limit);
  1758. rsp->tag = cmd->rsp.tag;
  1759. rsp->max_it_iu_len = cpu_to_be32(SRP_MAX_IU_LEN);
  1760. rsp->max_ti_iu_len = cpu_to_be32(SRP_MAX_IU_LEN);
  1761. fmt = (struct format_code *)&rsp->buf_fmt;
  1762. fmt->buffers = SUPPORTED_FORMATS;
  1763. vscsi->credit = 0;
  1764. cmd->rsp.len = sizeof(struct srp_login_rsp);
  1765. dma_wmb();
  1766. rc = h_copy_rdma(cmd->rsp.len, vscsi->dds.window[LOCAL].liobn,
  1767. iue->sbuf->dma, vscsi->dds.window[REMOTE].liobn,
  1768. be64_to_cpu(iue->remote_token));
  1769. switch (rc) {
  1770. case H_SUCCESS:
  1771. break;
  1772. case H_PERMISSION:
  1773. if (connection_broken(vscsi))
  1774. flag_bits = RESPONSE_Q_DOWN | CLIENT_FAILED;
  1775. dev_err(&vscsi->dev, "login_rsp: error copying to client, rc %ld\n",
  1776. rc);
  1777. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
  1778. flag_bits);
  1779. break;
  1780. case H_SOURCE_PARM:
  1781. case H_DEST_PARM:
  1782. default:
  1783. dev_err(&vscsi->dev, "login_rsp: error copying to client, rc %ld\n",
  1784. rc);
  1785. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  1786. break;
  1787. }
  1788. return rc;
  1789. }
  1790. /**
  1791. * ibmvscsis_srp_login_rej() - Create/copy a login rejection notice to client
  1792. * @vscsi: Pointer to our adapter structure
  1793. * @cmd: Pointer to the command for the SRP Login request
  1794. * @reason: The reason the SRP Login is being rejected, per SRP protocol
  1795. *
  1796. * EXECUTION ENVIRONMENT:
  1797. * Interrupt, interrupt lock held
  1798. */
  1799. static long ibmvscsis_srp_login_rej(struct scsi_info *vscsi,
  1800. struct ibmvscsis_cmd *cmd, u32 reason)
  1801. {
  1802. struct iu_entry *iue = cmd->iue;
  1803. struct srp_login_rej *rej = &vio_iu(iue)->srp.login_rej;
  1804. struct format_code *fmt;
  1805. uint flag_bits = 0;
  1806. long rc = ADAPT_SUCCESS;
  1807. memset(rej, 0, sizeof(*rej));
  1808. rej->opcode = SRP_LOGIN_REJ;
  1809. rej->reason = cpu_to_be32(reason);
  1810. rej->tag = cmd->rsp.tag;
  1811. fmt = (struct format_code *)&rej->buf_fmt;
  1812. fmt->buffers = SUPPORTED_FORMATS;
  1813. cmd->rsp.len = sizeof(*rej);
  1814. dma_wmb();
  1815. rc = h_copy_rdma(cmd->rsp.len, vscsi->dds.window[LOCAL].liobn,
  1816. iue->sbuf->dma, vscsi->dds.window[REMOTE].liobn,
  1817. be64_to_cpu(iue->remote_token));
  1818. switch (rc) {
  1819. case H_SUCCESS:
  1820. break;
  1821. case H_PERMISSION:
  1822. if (connection_broken(vscsi))
  1823. flag_bits = RESPONSE_Q_DOWN | CLIENT_FAILED;
  1824. dev_err(&vscsi->dev, "login_rej: error copying to client, rc %ld\n",
  1825. rc);
  1826. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
  1827. flag_bits);
  1828. break;
  1829. case H_SOURCE_PARM:
  1830. case H_DEST_PARM:
  1831. default:
  1832. dev_err(&vscsi->dev, "login_rej: error copying to client, rc %ld\n",
  1833. rc);
  1834. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  1835. break;
  1836. }
  1837. return rc;
  1838. }
  1839. static int ibmvscsis_make_nexus(struct ibmvscsis_tport *tport)
  1840. {
  1841. char *name = tport->tport_name;
  1842. struct ibmvscsis_nexus *nexus;
  1843. int rc;
  1844. if (tport->ibmv_nexus) {
  1845. pr_debug("tport->ibmv_nexus already exists\n");
  1846. return 0;
  1847. }
  1848. nexus = kzalloc(sizeof(*nexus), GFP_KERNEL);
  1849. if (!nexus) {
  1850. pr_err("Unable to allocate struct ibmvscsis_nexus\n");
  1851. return -ENOMEM;
  1852. }
  1853. nexus->se_sess = target_alloc_session(&tport->se_tpg, 0, 0,
  1854. TARGET_PROT_NORMAL, name, nexus,
  1855. NULL);
  1856. if (IS_ERR(nexus->se_sess)) {
  1857. rc = PTR_ERR(nexus->se_sess);
  1858. goto transport_init_fail;
  1859. }
  1860. tport->ibmv_nexus = nexus;
  1861. return 0;
  1862. transport_init_fail:
  1863. kfree(nexus);
  1864. return rc;
  1865. }
  1866. static int ibmvscsis_drop_nexus(struct ibmvscsis_tport *tport)
  1867. {
  1868. struct se_session *se_sess;
  1869. struct ibmvscsis_nexus *nexus;
  1870. nexus = tport->ibmv_nexus;
  1871. if (!nexus)
  1872. return -ENODEV;
  1873. se_sess = nexus->se_sess;
  1874. if (!se_sess)
  1875. return -ENODEV;
  1876. /*
  1877. * Release the SCSI I_T Nexus to the emulated ibmvscsis Target Port
  1878. */
  1879. target_wait_for_sess_cmds(se_sess);
  1880. transport_deregister_session_configfs(se_sess);
  1881. transport_deregister_session(se_sess);
  1882. tport->ibmv_nexus = NULL;
  1883. kfree(nexus);
  1884. return 0;
  1885. }
  1886. /**
  1887. * ibmvscsis_srp_login() - Process an SRP Login Request
  1888. * @vscsi: Pointer to our adapter structure
  1889. * @cmd: Command element to use to process the SRP Login request
  1890. * @crq: Pointer to CRQ entry containing the SRP Login request
  1891. *
  1892. * EXECUTION ENVIRONMENT:
  1893. * Interrupt, called with interrupt lock held
  1894. */
  1895. static long ibmvscsis_srp_login(struct scsi_info *vscsi,
  1896. struct ibmvscsis_cmd *cmd,
  1897. struct viosrp_crq *crq)
  1898. {
  1899. struct iu_entry *iue = cmd->iue;
  1900. struct srp_login_req *req = &vio_iu(iue)->srp.login_req;
  1901. struct port_id {
  1902. __be64 id_extension;
  1903. __be64 io_guid;
  1904. } *iport, *tport;
  1905. struct format_code *fmt;
  1906. u32 reason = 0x0;
  1907. long rc = ADAPT_SUCCESS;
  1908. iport = (struct port_id *)req->initiator_port_id;
  1909. tport = (struct port_id *)req->target_port_id;
  1910. fmt = (struct format_code *)&req->req_buf_fmt;
  1911. if (be32_to_cpu(req->req_it_iu_len) > SRP_MAX_IU_LEN)
  1912. reason = SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE;
  1913. else if (be32_to_cpu(req->req_it_iu_len) < 64)
  1914. reason = SRP_LOGIN_REJ_UNABLE_ESTABLISH_CHANNEL;
  1915. else if ((be64_to_cpu(iport->id_extension) > (MAX_NUM_PORTS - 1)) ||
  1916. (be64_to_cpu(tport->id_extension) > (MAX_NUM_PORTS - 1)))
  1917. reason = SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL;
  1918. else if (req->req_flags & SRP_MULTICHAN_MULTI)
  1919. reason = SRP_LOGIN_REJ_MULTI_CHANNEL_UNSUPPORTED;
  1920. else if (fmt->buffers & (~SUPPORTED_FORMATS))
  1921. reason = SRP_LOGIN_REJ_UNSUPPORTED_DESCRIPTOR_FMT;
  1922. else if ((fmt->buffers & SUPPORTED_FORMATS) == 0)
  1923. reason = SRP_LOGIN_REJ_UNSUPPORTED_DESCRIPTOR_FMT;
  1924. if (vscsi->state == SRP_PROCESSING)
  1925. reason = SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED;
  1926. rc = ibmvscsis_make_nexus(&vscsi->tport);
  1927. if (rc)
  1928. reason = SRP_LOGIN_REJ_UNABLE_ESTABLISH_CHANNEL;
  1929. cmd->rsp.format = VIOSRP_SRP_FORMAT;
  1930. cmd->rsp.tag = req->tag;
  1931. pr_debug("srp_login: reason 0x%x\n", reason);
  1932. if (reason)
  1933. rc = ibmvscsis_srp_login_rej(vscsi, cmd, reason);
  1934. else
  1935. rc = ibmvscsis_login_rsp(vscsi, cmd);
  1936. if (!rc) {
  1937. if (!reason)
  1938. vscsi->state = SRP_PROCESSING;
  1939. list_add_tail(&cmd->list, &vscsi->waiting_rsp);
  1940. ibmvscsis_send_messages(vscsi);
  1941. } else {
  1942. ibmvscsis_free_cmd_resources(vscsi, cmd);
  1943. }
  1944. pr_debug("Leaving srp_login, rc %ld\n", rc);
  1945. return rc;
  1946. }
  1947. /**
  1948. * ibmvscsis_srp_i_logout() - Helper Function to close I_T Nexus
  1949. * @vscsi: Pointer to our adapter structure
  1950. * @cmd: Command element to use to process the Implicit Logout request
  1951. * @crq: Pointer to CRQ entry containing the Implicit Logout request
  1952. *
  1953. * Do the logic to close the I_T nexus. This function may not
  1954. * behave to specification.
  1955. *
  1956. * EXECUTION ENVIRONMENT:
  1957. * Interrupt, interrupt lock held
  1958. */
  1959. static long ibmvscsis_srp_i_logout(struct scsi_info *vscsi,
  1960. struct ibmvscsis_cmd *cmd,
  1961. struct viosrp_crq *crq)
  1962. {
  1963. struct iu_entry *iue = cmd->iue;
  1964. struct srp_i_logout *log_out = &vio_iu(iue)->srp.i_logout;
  1965. long rc = ADAPT_SUCCESS;
  1966. if ((vscsi->debit > 0) || !list_empty(&vscsi->schedule_q) ||
  1967. !list_empty(&vscsi->waiting_rsp)) {
  1968. dev_err(&vscsi->dev, "i_logout: outstanding work\n");
  1969. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
  1970. } else {
  1971. cmd->rsp.format = SRP_FORMAT;
  1972. cmd->rsp.tag = log_out->tag;
  1973. cmd->rsp.len = sizeof(struct mad_common);
  1974. list_add_tail(&cmd->list, &vscsi->waiting_rsp);
  1975. ibmvscsis_send_messages(vscsi);
  1976. ibmvscsis_post_disconnect(vscsi, WAIT_IDLE, 0);
  1977. }
  1978. return rc;
  1979. }
  1980. /* Called with intr lock held */
  1981. static void ibmvscsis_srp_cmd(struct scsi_info *vscsi, struct viosrp_crq *crq)
  1982. {
  1983. struct ibmvscsis_cmd *cmd;
  1984. struct iu_entry *iue;
  1985. struct srp_cmd *srp;
  1986. struct srp_tsk_mgmt *tsk;
  1987. long rc;
  1988. if (vscsi->request_limit - vscsi->debit <= 0) {
  1989. /* Client has exceeded request limit */
  1990. dev_err(&vscsi->dev, "Client exceeded the request limit (%d), debit %d\n",
  1991. vscsi->request_limit, vscsi->debit);
  1992. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  1993. return;
  1994. }
  1995. cmd = ibmvscsis_get_free_cmd(vscsi);
  1996. if (!cmd) {
  1997. dev_err(&vscsi->dev, "srp_cmd failed to get cmd, debit %d\n",
  1998. vscsi->debit);
  1999. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  2000. return;
  2001. }
  2002. iue = cmd->iue;
  2003. srp = &vio_iu(iue)->srp.cmd;
  2004. rc = ibmvscsis_copy_crq_packet(vscsi, cmd, crq);
  2005. if (rc) {
  2006. ibmvscsis_free_cmd_resources(vscsi, cmd);
  2007. return;
  2008. }
  2009. if (vscsi->state == SRP_PROCESSING) {
  2010. switch (srp->opcode) {
  2011. case SRP_LOGIN_REQ:
  2012. rc = ibmvscsis_srp_login(vscsi, cmd, crq);
  2013. break;
  2014. case SRP_TSK_MGMT:
  2015. tsk = &vio_iu(iue)->srp.tsk_mgmt;
  2016. pr_debug("tsk_mgmt tag: %llu (0x%llx)\n", tsk->tag,
  2017. tsk->tag);
  2018. cmd->rsp.tag = tsk->tag;
  2019. vscsi->debit += 1;
  2020. cmd->type = TASK_MANAGEMENT;
  2021. list_add_tail(&cmd->list, &vscsi->schedule_q);
  2022. queue_work(vscsi->work_q, &cmd->work);
  2023. break;
  2024. case SRP_CMD:
  2025. pr_debug("srp_cmd tag: %llu (0x%llx)\n", srp->tag,
  2026. srp->tag);
  2027. cmd->rsp.tag = srp->tag;
  2028. vscsi->debit += 1;
  2029. cmd->type = SCSI_CDB;
  2030. /*
  2031. * We want to keep track of work waiting for
  2032. * the workqueue.
  2033. */
  2034. list_add_tail(&cmd->list, &vscsi->schedule_q);
  2035. queue_work(vscsi->work_q, &cmd->work);
  2036. break;
  2037. case SRP_I_LOGOUT:
  2038. rc = ibmvscsis_srp_i_logout(vscsi, cmd, crq);
  2039. break;
  2040. case SRP_CRED_RSP:
  2041. case SRP_AER_RSP:
  2042. default:
  2043. ibmvscsis_free_cmd_resources(vscsi, cmd);
  2044. dev_err(&vscsi->dev, "invalid srp cmd, opcode %d\n",
  2045. (uint)srp->opcode);
  2046. ibmvscsis_post_disconnect(vscsi,
  2047. ERR_DISCONNECT_RECONNECT, 0);
  2048. break;
  2049. }
  2050. } else if (srp->opcode == SRP_LOGIN_REQ && vscsi->state == CONNECTED) {
  2051. rc = ibmvscsis_srp_login(vscsi, cmd, crq);
  2052. } else {
  2053. ibmvscsis_free_cmd_resources(vscsi, cmd);
  2054. dev_err(&vscsi->dev, "Invalid state %d to handle srp cmd\n",
  2055. vscsi->state);
  2056. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  2057. }
  2058. }
  2059. /**
  2060. * ibmvscsis_ping_response() - Respond to a ping request
  2061. * @vscsi: Pointer to our adapter structure
  2062. *
  2063. * Let the client know that the server is alive and waiting on
  2064. * its native I/O stack.
  2065. * If any type of error occurs from the call to queue a ping
  2066. * response then the client is either not accepting or receiving
  2067. * interrupts. Disconnect with an error.
  2068. *
  2069. * EXECUTION ENVIRONMENT:
  2070. * Interrupt, interrupt lock held
  2071. */
  2072. static long ibmvscsis_ping_response(struct scsi_info *vscsi)
  2073. {
  2074. struct viosrp_crq *crq;
  2075. u64 buffer[2] = { 0, 0 };
  2076. long rc;
  2077. crq = (struct viosrp_crq *)&buffer;
  2078. crq->valid = VALID_CMD_RESP_EL;
  2079. crq->format = (u8)MESSAGE_IN_CRQ;
  2080. crq->status = PING_RESPONSE;
  2081. rc = h_send_crq(vscsi->dds.unit_id, cpu_to_be64(buffer[MSG_HI]),
  2082. cpu_to_be64(buffer[MSG_LOW]));
  2083. switch (rc) {
  2084. case H_SUCCESS:
  2085. break;
  2086. case H_CLOSED:
  2087. vscsi->flags |= CLIENT_FAILED;
  2088. case H_DROPPED:
  2089. vscsi->flags |= RESPONSE_Q_DOWN;
  2090. case H_REMOTE_PARM:
  2091. dev_err(&vscsi->dev, "ping_response: h_send_crq failed, rc %ld\n",
  2092. rc);
  2093. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  2094. break;
  2095. default:
  2096. dev_err(&vscsi->dev, "ping_response: h_send_crq returned unknown rc %ld\n",
  2097. rc);
  2098. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
  2099. break;
  2100. }
  2101. return rc;
  2102. }
  2103. /**
  2104. * ibmvscsis_parse_command() - Parse an element taken from the cmd rsp queue.
  2105. * @vscsi: Pointer to our adapter structure
  2106. * @crq: Pointer to CRQ element containing the SRP request
  2107. *
  2108. * This function will return success if the command queue element is valid
  2109. * and the srp iu or MAD request it pointed to was also valid. That does
  2110. * not mean that an error was not returned to the client.
  2111. *
  2112. * EXECUTION ENVIRONMENT:
  2113. * Interrupt, intr lock held
  2114. */
  2115. static long ibmvscsis_parse_command(struct scsi_info *vscsi,
  2116. struct viosrp_crq *crq)
  2117. {
  2118. long rc = ADAPT_SUCCESS;
  2119. switch (crq->valid) {
  2120. case VALID_CMD_RESP_EL:
  2121. switch (crq->format) {
  2122. case OS400_FORMAT:
  2123. case AIX_FORMAT:
  2124. case LINUX_FORMAT:
  2125. case MAD_FORMAT:
  2126. if (vscsi->flags & PROCESSING_MAD) {
  2127. rc = ERROR;
  2128. dev_err(&vscsi->dev, "parse_command: already processing mad\n");
  2129. ibmvscsis_post_disconnect(vscsi,
  2130. ERR_DISCONNECT_RECONNECT,
  2131. 0);
  2132. } else {
  2133. vscsi->flags |= PROCESSING_MAD;
  2134. rc = ibmvscsis_mad(vscsi, crq);
  2135. }
  2136. break;
  2137. case SRP_FORMAT:
  2138. ibmvscsis_srp_cmd(vscsi, crq);
  2139. break;
  2140. case MESSAGE_IN_CRQ:
  2141. if (crq->status == PING)
  2142. ibmvscsis_ping_response(vscsi);
  2143. break;
  2144. default:
  2145. dev_err(&vscsi->dev, "parse_command: invalid format %d\n",
  2146. (uint)crq->format);
  2147. ibmvscsis_post_disconnect(vscsi,
  2148. ERR_DISCONNECT_RECONNECT, 0);
  2149. break;
  2150. }
  2151. break;
  2152. case VALID_TRANS_EVENT:
  2153. rc = ibmvscsis_trans_event(vscsi, crq);
  2154. break;
  2155. case VALID_INIT_MSG:
  2156. rc = ibmvscsis_init_msg(vscsi, crq);
  2157. break;
  2158. default:
  2159. dev_err(&vscsi->dev, "parse_command: invalid valid field %d\n",
  2160. (uint)crq->valid);
  2161. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  2162. break;
  2163. }
  2164. /*
  2165. * Return only what the interrupt handler cares
  2166. * about. Most errors we keep right on trucking.
  2167. */
  2168. rc = vscsi->flags & SCHEDULE_DISCONNECT;
  2169. return rc;
  2170. }
  2171. static int read_dma_window(struct scsi_info *vscsi)
  2172. {
  2173. struct vio_dev *vdev = vscsi->dma_dev;
  2174. const __be32 *dma_window;
  2175. const __be32 *prop;
  2176. /* TODO Using of_parse_dma_window would be better, but it doesn't give
  2177. * a way to read multiple windows without already knowing the size of
  2178. * a window or the number of windows.
  2179. */
  2180. dma_window = (const __be32 *)vio_get_attribute(vdev,
  2181. "ibm,my-dma-window",
  2182. NULL);
  2183. if (!dma_window) {
  2184. pr_err("Couldn't find ibm,my-dma-window property\n");
  2185. return -1;
  2186. }
  2187. vscsi->dds.window[LOCAL].liobn = be32_to_cpu(*dma_window);
  2188. dma_window++;
  2189. prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-address-cells",
  2190. NULL);
  2191. if (!prop) {
  2192. pr_warn("Couldn't find ibm,#dma-address-cells property\n");
  2193. dma_window++;
  2194. } else {
  2195. dma_window += be32_to_cpu(*prop);
  2196. }
  2197. prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-size-cells",
  2198. NULL);
  2199. if (!prop) {
  2200. pr_warn("Couldn't find ibm,#dma-size-cells property\n");
  2201. dma_window++;
  2202. } else {
  2203. dma_window += be32_to_cpu(*prop);
  2204. }
  2205. /* dma_window should point to the second window now */
  2206. vscsi->dds.window[REMOTE].liobn = be32_to_cpu(*dma_window);
  2207. return 0;
  2208. }
  2209. static struct ibmvscsis_tport *ibmvscsis_lookup_port(const char *name)
  2210. {
  2211. struct ibmvscsis_tport *tport = NULL;
  2212. struct vio_dev *vdev;
  2213. struct scsi_info *vscsi;
  2214. spin_lock_bh(&ibmvscsis_dev_lock);
  2215. list_for_each_entry(vscsi, &ibmvscsis_dev_list, list) {
  2216. vdev = vscsi->dma_dev;
  2217. if (!strcmp(dev_name(&vdev->dev), name)) {
  2218. tport = &vscsi->tport;
  2219. break;
  2220. }
  2221. }
  2222. spin_unlock_bh(&ibmvscsis_dev_lock);
  2223. return tport;
  2224. }
  2225. /**
  2226. * ibmvscsis_parse_cmd() - Parse SRP Command
  2227. * @vscsi: Pointer to our adapter structure
  2228. * @cmd: Pointer to command element with SRP command
  2229. *
  2230. * Parse the srp command; if it is valid then submit it to tcm.
  2231. * Note: The return code does not reflect the status of the SCSI CDB.
  2232. *
  2233. * EXECUTION ENVIRONMENT:
  2234. * Process level
  2235. */
  2236. static void ibmvscsis_parse_cmd(struct scsi_info *vscsi,
  2237. struct ibmvscsis_cmd *cmd)
  2238. {
  2239. struct iu_entry *iue = cmd->iue;
  2240. struct srp_cmd *srp = (struct srp_cmd *)iue->sbuf->buf;
  2241. struct ibmvscsis_nexus *nexus;
  2242. u64 data_len = 0;
  2243. enum dma_data_direction dir;
  2244. int attr = 0;
  2245. int rc = 0;
  2246. nexus = vscsi->tport.ibmv_nexus;
  2247. /*
  2248. * additional length in bytes. Note that the SRP spec says that
  2249. * additional length is in 4-byte words, but technically the
  2250. * additional length field is only the upper 6 bits of the byte.
  2251. * The lower 2 bits are reserved. If the lower 2 bits are 0 (as
  2252. * all reserved fields should be), then interpreting the byte as
  2253. * an int will yield the length in bytes.
  2254. */
  2255. if (srp->add_cdb_len & 0x03) {
  2256. dev_err(&vscsi->dev, "parse_cmd: reserved bits set in IU\n");
  2257. spin_lock_bh(&vscsi->intr_lock);
  2258. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  2259. ibmvscsis_free_cmd_resources(vscsi, cmd);
  2260. spin_unlock_bh(&vscsi->intr_lock);
  2261. return;
  2262. }
  2263. if (srp_get_desc_table(srp, &dir, &data_len)) {
  2264. dev_err(&vscsi->dev, "0x%llx: parsing SRP descriptor table failed.\n",
  2265. srp->tag);
  2266. goto fail;
  2267. }
  2268. cmd->rsp.sol_not = srp->sol_not;
  2269. switch (srp->task_attr) {
  2270. case SRP_SIMPLE_TASK:
  2271. attr = TCM_SIMPLE_TAG;
  2272. break;
  2273. case SRP_ORDERED_TASK:
  2274. attr = TCM_ORDERED_TAG;
  2275. break;
  2276. case SRP_HEAD_TASK:
  2277. attr = TCM_HEAD_TAG;
  2278. break;
  2279. case SRP_ACA_TASK:
  2280. attr = TCM_ACA_TAG;
  2281. break;
  2282. default:
  2283. dev_err(&vscsi->dev, "Invalid task attribute %d\n",
  2284. srp->task_attr);
  2285. goto fail;
  2286. }
  2287. cmd->se_cmd.tag = be64_to_cpu(srp->tag);
  2288. spin_lock_bh(&vscsi->intr_lock);
  2289. list_add_tail(&cmd->list, &vscsi->active_q);
  2290. spin_unlock_bh(&vscsi->intr_lock);
  2291. srp->lun.scsi_lun[0] &= 0x3f;
  2292. rc = target_submit_cmd(&cmd->se_cmd, nexus->se_sess, srp->cdb,
  2293. cmd->sense_buf, scsilun_to_int(&srp->lun),
  2294. data_len, attr, dir, 0);
  2295. if (rc) {
  2296. dev_err(&vscsi->dev, "target_submit_cmd failed, rc %d\n", rc);
  2297. spin_lock_bh(&vscsi->intr_lock);
  2298. list_del(&cmd->list);
  2299. ibmvscsis_free_cmd_resources(vscsi, cmd);
  2300. spin_unlock_bh(&vscsi->intr_lock);
  2301. goto fail;
  2302. }
  2303. return;
  2304. fail:
  2305. spin_lock_bh(&vscsi->intr_lock);
  2306. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  2307. spin_unlock_bh(&vscsi->intr_lock);
  2308. }
  2309. /**
  2310. * ibmvscsis_parse_task() - Parse SRP Task Management Request
  2311. * @vscsi: Pointer to our adapter structure
  2312. * @cmd: Pointer to command element with SRP task management request
  2313. *
  2314. * Parse the srp task management request; if it is valid then submit it to tcm.
  2315. * Note: The return code does not reflect the status of the task management
  2316. * request.
  2317. *
  2318. * EXECUTION ENVIRONMENT:
  2319. * Processor level
  2320. */
  2321. static void ibmvscsis_parse_task(struct scsi_info *vscsi,
  2322. struct ibmvscsis_cmd *cmd)
  2323. {
  2324. struct iu_entry *iue = cmd->iue;
  2325. struct srp_tsk_mgmt *srp_tsk = &vio_iu(iue)->srp.tsk_mgmt;
  2326. int tcm_type;
  2327. u64 tag_to_abort = 0;
  2328. int rc = 0;
  2329. struct ibmvscsis_nexus *nexus;
  2330. nexus = vscsi->tport.ibmv_nexus;
  2331. cmd->rsp.sol_not = srp_tsk->sol_not;
  2332. switch (srp_tsk->tsk_mgmt_func) {
  2333. case SRP_TSK_ABORT_TASK:
  2334. tcm_type = TMR_ABORT_TASK;
  2335. tag_to_abort = be64_to_cpu(srp_tsk->task_tag);
  2336. break;
  2337. case SRP_TSK_ABORT_TASK_SET:
  2338. tcm_type = TMR_ABORT_TASK_SET;
  2339. break;
  2340. case SRP_TSK_CLEAR_TASK_SET:
  2341. tcm_type = TMR_CLEAR_TASK_SET;
  2342. break;
  2343. case SRP_TSK_LUN_RESET:
  2344. tcm_type = TMR_LUN_RESET;
  2345. break;
  2346. case SRP_TSK_CLEAR_ACA:
  2347. tcm_type = TMR_CLEAR_ACA;
  2348. break;
  2349. default:
  2350. dev_err(&vscsi->dev, "unknown task mgmt func %d\n",
  2351. srp_tsk->tsk_mgmt_func);
  2352. cmd->se_cmd.se_tmr_req->response =
  2353. TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  2354. rc = -1;
  2355. break;
  2356. }
  2357. if (!rc) {
  2358. cmd->se_cmd.tag = be64_to_cpu(srp_tsk->tag);
  2359. spin_lock_bh(&vscsi->intr_lock);
  2360. list_add_tail(&cmd->list, &vscsi->active_q);
  2361. spin_unlock_bh(&vscsi->intr_lock);
  2362. srp_tsk->lun.scsi_lun[0] &= 0x3f;
  2363. pr_debug("calling submit_tmr, func %d\n",
  2364. srp_tsk->tsk_mgmt_func);
  2365. rc = target_submit_tmr(&cmd->se_cmd, nexus->se_sess, NULL,
  2366. scsilun_to_int(&srp_tsk->lun), srp_tsk,
  2367. tcm_type, GFP_KERNEL, tag_to_abort, 0);
  2368. if (rc) {
  2369. dev_err(&vscsi->dev, "target_submit_tmr failed, rc %d\n",
  2370. rc);
  2371. spin_lock_bh(&vscsi->intr_lock);
  2372. list_del(&cmd->list);
  2373. spin_unlock_bh(&vscsi->intr_lock);
  2374. cmd->se_cmd.se_tmr_req->response =
  2375. TMR_FUNCTION_REJECTED;
  2376. }
  2377. }
  2378. if (rc)
  2379. transport_send_check_condition_and_sense(&cmd->se_cmd, 0, 0);
  2380. }
  2381. static void ibmvscsis_scheduler(struct work_struct *work)
  2382. {
  2383. struct ibmvscsis_cmd *cmd = container_of(work, struct ibmvscsis_cmd,
  2384. work);
  2385. struct scsi_info *vscsi = cmd->adapter;
  2386. spin_lock_bh(&vscsi->intr_lock);
  2387. /* Remove from schedule_q */
  2388. list_del(&cmd->list);
  2389. /* Don't submit cmd if we're disconnecting */
  2390. if (vscsi->flags & (SCHEDULE_DISCONNECT | DISCONNECT_SCHEDULED)) {
  2391. ibmvscsis_free_cmd_resources(vscsi, cmd);
  2392. /* ibmvscsis_disconnect might be waiting for us */
  2393. if (list_empty(&vscsi->active_q) &&
  2394. list_empty(&vscsi->schedule_q) &&
  2395. (vscsi->flags & WAIT_FOR_IDLE)) {
  2396. vscsi->flags &= ~WAIT_FOR_IDLE;
  2397. complete(&vscsi->wait_idle);
  2398. }
  2399. spin_unlock_bh(&vscsi->intr_lock);
  2400. return;
  2401. }
  2402. spin_unlock_bh(&vscsi->intr_lock);
  2403. switch (cmd->type) {
  2404. case SCSI_CDB:
  2405. ibmvscsis_parse_cmd(vscsi, cmd);
  2406. break;
  2407. case TASK_MANAGEMENT:
  2408. ibmvscsis_parse_task(vscsi, cmd);
  2409. break;
  2410. default:
  2411. dev_err(&vscsi->dev, "scheduler, invalid cmd type %d\n",
  2412. cmd->type);
  2413. spin_lock_bh(&vscsi->intr_lock);
  2414. ibmvscsis_free_cmd_resources(vscsi, cmd);
  2415. spin_unlock_bh(&vscsi->intr_lock);
  2416. break;
  2417. }
  2418. }
  2419. static int ibmvscsis_alloc_cmds(struct scsi_info *vscsi, int num)
  2420. {
  2421. struct ibmvscsis_cmd *cmd;
  2422. int i;
  2423. INIT_LIST_HEAD(&vscsi->free_cmd);
  2424. vscsi->cmd_pool = kcalloc(num, sizeof(struct ibmvscsis_cmd),
  2425. GFP_KERNEL);
  2426. if (!vscsi->cmd_pool)
  2427. return -ENOMEM;
  2428. for (i = 0, cmd = (struct ibmvscsis_cmd *)vscsi->cmd_pool; i < num;
  2429. i++, cmd++) {
  2430. cmd->abort_cmd = NULL;
  2431. cmd->adapter = vscsi;
  2432. INIT_WORK(&cmd->work, ibmvscsis_scheduler);
  2433. list_add_tail(&cmd->list, &vscsi->free_cmd);
  2434. }
  2435. return 0;
  2436. }
  2437. static void ibmvscsis_free_cmds(struct scsi_info *vscsi)
  2438. {
  2439. kfree(vscsi->cmd_pool);
  2440. vscsi->cmd_pool = NULL;
  2441. INIT_LIST_HEAD(&vscsi->free_cmd);
  2442. }
  2443. /**
  2444. * ibmvscsis_service_wait_q() - Service Waiting Queue
  2445. * @timer: Pointer to timer which has expired
  2446. *
  2447. * This routine is called when the timer pops to service the waiting
  2448. * queue. Elements on the queue have completed, their responses have been
  2449. * copied to the client, but the client's response queue was full so
  2450. * the queue message could not be sent. The routine grabs the proper locks
  2451. * and calls send messages.
  2452. *
  2453. * EXECUTION ENVIRONMENT:
  2454. * called at interrupt level
  2455. */
  2456. static enum hrtimer_restart ibmvscsis_service_wait_q(struct hrtimer *timer)
  2457. {
  2458. struct timer_cb *p_timer = container_of(timer, struct timer_cb, timer);
  2459. struct scsi_info *vscsi = container_of(p_timer, struct scsi_info,
  2460. rsp_q_timer);
  2461. spin_lock_bh(&vscsi->intr_lock);
  2462. p_timer->timer_pops += 1;
  2463. p_timer->started = false;
  2464. ibmvscsis_send_messages(vscsi);
  2465. spin_unlock_bh(&vscsi->intr_lock);
  2466. return HRTIMER_NORESTART;
  2467. }
  2468. static long ibmvscsis_alloctimer(struct scsi_info *vscsi)
  2469. {
  2470. struct timer_cb *p_timer;
  2471. p_timer = &vscsi->rsp_q_timer;
  2472. hrtimer_init(&p_timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2473. p_timer->timer.function = ibmvscsis_service_wait_q;
  2474. p_timer->started = false;
  2475. p_timer->timer_pops = 0;
  2476. return ADAPT_SUCCESS;
  2477. }
  2478. static void ibmvscsis_freetimer(struct scsi_info *vscsi)
  2479. {
  2480. struct timer_cb *p_timer;
  2481. p_timer = &vscsi->rsp_q_timer;
  2482. (void)hrtimer_cancel(&p_timer->timer);
  2483. p_timer->started = false;
  2484. p_timer->timer_pops = 0;
  2485. }
  2486. static irqreturn_t ibmvscsis_interrupt(int dummy, void *data)
  2487. {
  2488. struct scsi_info *vscsi = data;
  2489. vio_disable_interrupts(vscsi->dma_dev);
  2490. tasklet_schedule(&vscsi->work_task);
  2491. return IRQ_HANDLED;
  2492. }
  2493. /**
  2494. * ibmvscsis_enable_change_state() - Set new state based on enabled status
  2495. * @vscsi: Pointer to our adapter structure
  2496. *
  2497. * This function determines our new state now that we are enabled. This
  2498. * may involve sending an Init Complete message to the client.
  2499. *
  2500. * Must be called with interrupt lock held.
  2501. */
  2502. static long ibmvscsis_enable_change_state(struct scsi_info *vscsi)
  2503. {
  2504. int bytes;
  2505. long rc = ADAPT_SUCCESS;
  2506. bytes = vscsi->cmd_q.size * PAGE_SIZE;
  2507. rc = h_reg_crq(vscsi->dds.unit_id, vscsi->cmd_q.crq_token, bytes);
  2508. if (rc == H_CLOSED || rc == H_SUCCESS) {
  2509. vscsi->state = WAIT_CONNECTION;
  2510. rc = ibmvscsis_establish_new_q(vscsi);
  2511. }
  2512. if (rc != ADAPT_SUCCESS) {
  2513. vscsi->state = ERR_DISCONNECTED;
  2514. vscsi->flags |= RESPONSE_Q_DOWN;
  2515. }
  2516. return rc;
  2517. }
  2518. /**
  2519. * ibmvscsis_create_command_q() - Create Command Queue
  2520. * @vscsi: Pointer to our adapter structure
  2521. * @num_cmds: Currently unused. In the future, may be used to determine
  2522. * the size of the CRQ.
  2523. *
  2524. * Allocates memory for command queue maps remote memory into an ioba
  2525. * initializes the command response queue
  2526. *
  2527. * EXECUTION ENVIRONMENT:
  2528. * Process level only
  2529. */
  2530. static long ibmvscsis_create_command_q(struct scsi_info *vscsi, int num_cmds)
  2531. {
  2532. int pages;
  2533. struct vio_dev *vdev = vscsi->dma_dev;
  2534. /* We might support multiple pages in the future, but just 1 for now */
  2535. pages = 1;
  2536. vscsi->cmd_q.size = pages;
  2537. vscsi->cmd_q.base_addr =
  2538. (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
  2539. if (!vscsi->cmd_q.base_addr)
  2540. return -ENOMEM;
  2541. vscsi->cmd_q.mask = ((uint)pages * CRQ_PER_PAGE) - 1;
  2542. vscsi->cmd_q.crq_token = dma_map_single(&vdev->dev,
  2543. vscsi->cmd_q.base_addr,
  2544. PAGE_SIZE, DMA_BIDIRECTIONAL);
  2545. if (dma_mapping_error(&vdev->dev, vscsi->cmd_q.crq_token)) {
  2546. free_page((unsigned long)vscsi->cmd_q.base_addr);
  2547. return -ENOMEM;
  2548. }
  2549. return 0;
  2550. }
  2551. /**
  2552. * ibmvscsis_destroy_command_q - Destroy Command Queue
  2553. * @vscsi: Pointer to our adapter structure
  2554. *
  2555. * Releases memory for command queue and unmaps mapped remote memory.
  2556. *
  2557. * EXECUTION ENVIRONMENT:
  2558. * Process level only
  2559. */
  2560. static void ibmvscsis_destroy_command_q(struct scsi_info *vscsi)
  2561. {
  2562. dma_unmap_single(&vscsi->dma_dev->dev, vscsi->cmd_q.crq_token,
  2563. PAGE_SIZE, DMA_BIDIRECTIONAL);
  2564. free_page((unsigned long)vscsi->cmd_q.base_addr);
  2565. vscsi->cmd_q.base_addr = NULL;
  2566. vscsi->state = NO_QUEUE;
  2567. }
  2568. static u8 ibmvscsis_fast_fail(struct scsi_info *vscsi,
  2569. struct ibmvscsis_cmd *cmd)
  2570. {
  2571. struct iu_entry *iue = cmd->iue;
  2572. struct se_cmd *se_cmd = &cmd->se_cmd;
  2573. struct srp_cmd *srp = (struct srp_cmd *)iue->sbuf->buf;
  2574. struct scsi_sense_hdr sshdr;
  2575. u8 rc = se_cmd->scsi_status;
  2576. if (vscsi->fast_fail && (READ_CMD(srp->cdb) || WRITE_CMD(srp->cdb)))
  2577. if (scsi_normalize_sense(se_cmd->sense_buffer,
  2578. se_cmd->scsi_sense_length, &sshdr))
  2579. if (sshdr.sense_key == HARDWARE_ERROR &&
  2580. (se_cmd->residual_count == 0 ||
  2581. se_cmd->residual_count == se_cmd->data_length)) {
  2582. rc = NO_SENSE;
  2583. cmd->flags |= CMD_FAST_FAIL;
  2584. }
  2585. return rc;
  2586. }
  2587. /**
  2588. * srp_build_response() - Build an SRP response buffer
  2589. * @vscsi: Pointer to our adapter structure
  2590. * @cmd: Pointer to command for which to send the response
  2591. * @len_p: Where to return the length of the IU response sent. This
  2592. * is needed to construct the CRQ response.
  2593. *
  2594. * Build the SRP response buffer and copy it to the client's memory space.
  2595. */
  2596. static long srp_build_response(struct scsi_info *vscsi,
  2597. struct ibmvscsis_cmd *cmd, uint *len_p)
  2598. {
  2599. struct iu_entry *iue = cmd->iue;
  2600. struct se_cmd *se_cmd = &cmd->se_cmd;
  2601. struct srp_rsp *rsp;
  2602. uint len;
  2603. u32 rsp_code;
  2604. char *data;
  2605. u32 *tsk_status;
  2606. long rc = ADAPT_SUCCESS;
  2607. spin_lock_bh(&vscsi->intr_lock);
  2608. rsp = &vio_iu(iue)->srp.rsp;
  2609. len = sizeof(*rsp);
  2610. memset(rsp, 0, len);
  2611. data = rsp->data;
  2612. rsp->opcode = SRP_RSP;
  2613. rsp->req_lim_delta = cpu_to_be32(1 + vscsi->credit);
  2614. rsp->tag = cmd->rsp.tag;
  2615. rsp->flags = 0;
  2616. if (cmd->type == SCSI_CDB) {
  2617. rsp->status = ibmvscsis_fast_fail(vscsi, cmd);
  2618. if (rsp->status) {
  2619. pr_debug("build_resp: cmd %p, scsi status %d\n", cmd,
  2620. (int)rsp->status);
  2621. ibmvscsis_determine_resid(se_cmd, rsp);
  2622. if (se_cmd->scsi_sense_length && se_cmd->sense_buffer) {
  2623. rsp->sense_data_len =
  2624. cpu_to_be32(se_cmd->scsi_sense_length);
  2625. rsp->flags |= SRP_RSP_FLAG_SNSVALID;
  2626. len += se_cmd->scsi_sense_length;
  2627. memcpy(data, se_cmd->sense_buffer,
  2628. se_cmd->scsi_sense_length);
  2629. }
  2630. rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
  2631. UCSOLNT_RESP_SHIFT;
  2632. } else if (cmd->flags & CMD_FAST_FAIL) {
  2633. pr_debug("build_resp: cmd %p, fast fail\n", cmd);
  2634. rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
  2635. UCSOLNT_RESP_SHIFT;
  2636. } else {
  2637. rsp->sol_not = (cmd->rsp.sol_not & SCSOLNT) >>
  2638. SCSOLNT_RESP_SHIFT;
  2639. }
  2640. } else {
  2641. /* this is task management */
  2642. rsp->status = 0;
  2643. rsp->resp_data_len = cpu_to_be32(4);
  2644. rsp->flags |= SRP_RSP_FLAG_RSPVALID;
  2645. switch (se_cmd->se_tmr_req->response) {
  2646. case TMR_FUNCTION_COMPLETE:
  2647. case TMR_TASK_DOES_NOT_EXIST:
  2648. rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_COMPLETE;
  2649. rsp->sol_not = (cmd->rsp.sol_not & SCSOLNT) >>
  2650. SCSOLNT_RESP_SHIFT;
  2651. break;
  2652. case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
  2653. case TMR_LUN_DOES_NOT_EXIST:
  2654. rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_NOT_SUPPORTED;
  2655. rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
  2656. UCSOLNT_RESP_SHIFT;
  2657. break;
  2658. case TMR_FUNCTION_FAILED:
  2659. case TMR_FUNCTION_REJECTED:
  2660. default:
  2661. rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_FAILED;
  2662. rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
  2663. UCSOLNT_RESP_SHIFT;
  2664. break;
  2665. }
  2666. tsk_status = (u32 *)data;
  2667. *tsk_status = cpu_to_be32(rsp_code);
  2668. data = (char *)(tsk_status + 1);
  2669. len += 4;
  2670. }
  2671. dma_wmb();
  2672. rc = h_copy_rdma(len, vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma,
  2673. vscsi->dds.window[REMOTE].liobn,
  2674. be64_to_cpu(iue->remote_token));
  2675. switch (rc) {
  2676. case H_SUCCESS:
  2677. vscsi->credit = 0;
  2678. *len_p = len;
  2679. break;
  2680. case H_PERMISSION:
  2681. if (connection_broken(vscsi))
  2682. vscsi->flags |= RESPONSE_Q_DOWN | CLIENT_FAILED;
  2683. dev_err(&vscsi->dev, "build_response: error copying to client, rc %ld, flags 0x%x, state 0x%hx\n",
  2684. rc, vscsi->flags, vscsi->state);
  2685. break;
  2686. case H_SOURCE_PARM:
  2687. case H_DEST_PARM:
  2688. default:
  2689. dev_err(&vscsi->dev, "build_response: error copying to client, rc %ld\n",
  2690. rc);
  2691. break;
  2692. }
  2693. spin_unlock_bh(&vscsi->intr_lock);
  2694. return rc;
  2695. }
  2696. static int ibmvscsis_rdma(struct ibmvscsis_cmd *cmd, struct scatterlist *sg,
  2697. int nsg, struct srp_direct_buf *md, int nmd,
  2698. enum dma_data_direction dir, unsigned int bytes)
  2699. {
  2700. struct iu_entry *iue = cmd->iue;
  2701. struct srp_target *target = iue->target;
  2702. struct scsi_info *vscsi = target->ldata;
  2703. struct scatterlist *sgp;
  2704. dma_addr_t client_ioba, server_ioba;
  2705. ulong buf_len;
  2706. ulong client_len, server_len;
  2707. int md_idx;
  2708. long tx_len;
  2709. long rc = 0;
  2710. if (bytes == 0)
  2711. return 0;
  2712. sgp = sg;
  2713. client_len = 0;
  2714. server_len = 0;
  2715. md_idx = 0;
  2716. tx_len = bytes;
  2717. do {
  2718. if (client_len == 0) {
  2719. if (md_idx >= nmd) {
  2720. dev_err(&vscsi->dev, "rdma: ran out of client memory descriptors\n");
  2721. rc = -EIO;
  2722. break;
  2723. }
  2724. client_ioba = be64_to_cpu(md[md_idx].va);
  2725. client_len = be32_to_cpu(md[md_idx].len);
  2726. }
  2727. if (server_len == 0) {
  2728. if (!sgp) {
  2729. dev_err(&vscsi->dev, "rdma: ran out of scatter/gather list\n");
  2730. rc = -EIO;
  2731. break;
  2732. }
  2733. server_ioba = sg_dma_address(sgp);
  2734. server_len = sg_dma_len(sgp);
  2735. }
  2736. buf_len = tx_len;
  2737. if (buf_len > client_len)
  2738. buf_len = client_len;
  2739. if (buf_len > server_len)
  2740. buf_len = server_len;
  2741. if (buf_len > max_vdma_size)
  2742. buf_len = max_vdma_size;
  2743. if (dir == DMA_TO_DEVICE) {
  2744. /* read from client */
  2745. rc = h_copy_rdma(buf_len,
  2746. vscsi->dds.window[REMOTE].liobn,
  2747. client_ioba,
  2748. vscsi->dds.window[LOCAL].liobn,
  2749. server_ioba);
  2750. } else {
  2751. /* The h_copy_rdma will cause phyp, running in another
  2752. * partition, to read memory, so we need to make sure
  2753. * the data has been written out, hence these syncs.
  2754. */
  2755. /* ensure that everything is in memory */
  2756. isync();
  2757. /* ensure that memory has been made visible */
  2758. dma_wmb();
  2759. rc = h_copy_rdma(buf_len,
  2760. vscsi->dds.window[LOCAL].liobn,
  2761. server_ioba,
  2762. vscsi->dds.window[REMOTE].liobn,
  2763. client_ioba);
  2764. }
  2765. switch (rc) {
  2766. case H_SUCCESS:
  2767. break;
  2768. case H_PERMISSION:
  2769. case H_SOURCE_PARM:
  2770. case H_DEST_PARM:
  2771. if (connection_broken(vscsi)) {
  2772. spin_lock_bh(&vscsi->intr_lock);
  2773. vscsi->flags |=
  2774. (RESPONSE_Q_DOWN | CLIENT_FAILED);
  2775. spin_unlock_bh(&vscsi->intr_lock);
  2776. }
  2777. dev_err(&vscsi->dev, "rdma: h_copy_rdma failed, rc %ld\n",
  2778. rc);
  2779. break;
  2780. default:
  2781. dev_err(&vscsi->dev, "rdma: unknown error %ld from h_copy_rdma\n",
  2782. rc);
  2783. break;
  2784. }
  2785. if (!rc) {
  2786. tx_len -= buf_len;
  2787. if (tx_len) {
  2788. client_len -= buf_len;
  2789. if (client_len == 0)
  2790. md_idx++;
  2791. else
  2792. client_ioba += buf_len;
  2793. server_len -= buf_len;
  2794. if (server_len == 0)
  2795. sgp = sg_next(sgp);
  2796. else
  2797. server_ioba += buf_len;
  2798. } else {
  2799. break;
  2800. }
  2801. }
  2802. } while (!rc);
  2803. return rc;
  2804. }
  2805. /**
  2806. * ibmvscsis_handle_crq() - Handle CRQ
  2807. * @data: Pointer to our adapter structure
  2808. *
  2809. * Read the command elements from the command queue and copy the payloads
  2810. * associated with the command elements to local memory and execute the
  2811. * SRP requests.
  2812. *
  2813. * Note: this is an edge triggered interrupt. It can not be shared.
  2814. */
  2815. static void ibmvscsis_handle_crq(unsigned long data)
  2816. {
  2817. struct scsi_info *vscsi = (struct scsi_info *)data;
  2818. struct viosrp_crq *crq;
  2819. long rc;
  2820. bool ack = true;
  2821. volatile u8 valid;
  2822. spin_lock_bh(&vscsi->intr_lock);
  2823. pr_debug("got interrupt\n");
  2824. /*
  2825. * if we are in a path where we are waiting for all pending commands
  2826. * to complete because we received a transport event and anything in
  2827. * the command queue is for a new connection, do nothing
  2828. */
  2829. if (TARGET_STOP(vscsi)) {
  2830. vio_enable_interrupts(vscsi->dma_dev);
  2831. pr_debug("handle_crq, don't process: flags 0x%x, state 0x%hx\n",
  2832. vscsi->flags, vscsi->state);
  2833. spin_unlock_bh(&vscsi->intr_lock);
  2834. return;
  2835. }
  2836. rc = vscsi->flags & SCHEDULE_DISCONNECT;
  2837. crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
  2838. valid = crq->valid;
  2839. dma_rmb();
  2840. while (valid) {
  2841. /*
  2842. * These are edege triggered interrupts. After dropping out of
  2843. * the while loop, the code must check for work since an
  2844. * interrupt could be lost, and an elment be left on the queue,
  2845. * hence the label.
  2846. */
  2847. cmd_work:
  2848. vscsi->cmd_q.index =
  2849. (vscsi->cmd_q.index + 1) & vscsi->cmd_q.mask;
  2850. if (!rc) {
  2851. rc = ibmvscsis_parse_command(vscsi, crq);
  2852. } else {
  2853. if ((uint)crq->valid == VALID_TRANS_EVENT) {
  2854. /*
  2855. * must service the transport layer events even
  2856. * in an error state, dont break out until all
  2857. * the consecutive transport events have been
  2858. * processed
  2859. */
  2860. rc = ibmvscsis_trans_event(vscsi, crq);
  2861. } else if (vscsi->flags & TRANS_EVENT) {
  2862. /*
  2863. * if a transport event has occurred leave
  2864. * everything but transport events on the queue
  2865. *
  2866. * need to decrement the queue index so we can
  2867. * look at the element again
  2868. */
  2869. if (vscsi->cmd_q.index)
  2870. vscsi->cmd_q.index -= 1;
  2871. else
  2872. /*
  2873. * index is at 0 it just wrapped.
  2874. * have it index last element in q
  2875. */
  2876. vscsi->cmd_q.index = vscsi->cmd_q.mask;
  2877. break;
  2878. }
  2879. }
  2880. crq->valid = INVALIDATE_CMD_RESP_EL;
  2881. crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
  2882. valid = crq->valid;
  2883. dma_rmb();
  2884. }
  2885. if (!rc) {
  2886. if (ack) {
  2887. vio_enable_interrupts(vscsi->dma_dev);
  2888. ack = false;
  2889. pr_debug("handle_crq, reenabling interrupts\n");
  2890. }
  2891. valid = crq->valid;
  2892. dma_rmb();
  2893. if (valid)
  2894. goto cmd_work;
  2895. } else {
  2896. pr_debug("handle_crq, error: flags 0x%x, state 0x%hx, crq index 0x%x\n",
  2897. vscsi->flags, vscsi->state, vscsi->cmd_q.index);
  2898. }
  2899. pr_debug("Leaving handle_crq: schedule_q empty %d, flags 0x%x, state 0x%hx\n",
  2900. (int)list_empty(&vscsi->schedule_q), vscsi->flags,
  2901. vscsi->state);
  2902. spin_unlock_bh(&vscsi->intr_lock);
  2903. }
  2904. static int ibmvscsis_probe(struct vio_dev *vdev,
  2905. const struct vio_device_id *id)
  2906. {
  2907. struct scsi_info *vscsi;
  2908. int rc = 0;
  2909. long hrc = 0;
  2910. char wq_name[24];
  2911. vscsi = kzalloc(sizeof(*vscsi), GFP_KERNEL);
  2912. if (!vscsi) {
  2913. rc = -ENOMEM;
  2914. pr_err("probe: allocation of adapter failed\n");
  2915. return rc;
  2916. }
  2917. vscsi->dma_dev = vdev;
  2918. vscsi->dev = vdev->dev;
  2919. INIT_LIST_HEAD(&vscsi->schedule_q);
  2920. INIT_LIST_HEAD(&vscsi->waiting_rsp);
  2921. INIT_LIST_HEAD(&vscsi->active_q);
  2922. snprintf(vscsi->tport.tport_name, IBMVSCSIS_NAMELEN, "%s",
  2923. dev_name(&vdev->dev));
  2924. pr_debug("probe tport_name: %s\n", vscsi->tport.tport_name);
  2925. rc = read_dma_window(vscsi);
  2926. if (rc)
  2927. goto free_adapter;
  2928. pr_debug("Probe: liobn 0x%x, riobn 0x%x\n",
  2929. vscsi->dds.window[LOCAL].liobn,
  2930. vscsi->dds.window[REMOTE].liobn);
  2931. strcpy(vscsi->eye, "VSCSI ");
  2932. strncat(vscsi->eye, vdev->name, MAX_EYE);
  2933. vscsi->dds.unit_id = vdev->unit_address;
  2934. strncpy(vscsi->dds.partition_name, partition_name,
  2935. sizeof(vscsi->dds.partition_name));
  2936. vscsi->dds.partition_num = partition_number;
  2937. spin_lock_bh(&ibmvscsis_dev_lock);
  2938. list_add_tail(&vscsi->list, &ibmvscsis_dev_list);
  2939. spin_unlock_bh(&ibmvscsis_dev_lock);
  2940. /*
  2941. * TBD: How do we determine # of cmds to request? Do we know how
  2942. * many "children" we have?
  2943. */
  2944. vscsi->request_limit = INITIAL_SRP_LIMIT;
  2945. rc = srp_target_alloc(&vscsi->target, &vdev->dev, vscsi->request_limit,
  2946. SRP_MAX_IU_LEN);
  2947. if (rc)
  2948. goto rem_list;
  2949. vscsi->target.ldata = vscsi;
  2950. rc = ibmvscsis_alloc_cmds(vscsi, vscsi->request_limit);
  2951. if (rc) {
  2952. dev_err(&vscsi->dev, "alloc_cmds failed, rc %d, num %d\n",
  2953. rc, vscsi->request_limit);
  2954. goto free_target;
  2955. }
  2956. /*
  2957. * Note: the lock is used in freeing timers, so must initialize
  2958. * first so that ordering in case of error is correct.
  2959. */
  2960. spin_lock_init(&vscsi->intr_lock);
  2961. rc = ibmvscsis_alloctimer(vscsi);
  2962. if (rc) {
  2963. dev_err(&vscsi->dev, "probe: alloctimer failed, rc %d\n", rc);
  2964. goto free_cmds;
  2965. }
  2966. rc = ibmvscsis_create_command_q(vscsi, 256);
  2967. if (rc) {
  2968. dev_err(&vscsi->dev, "probe: create_command_q failed, rc %d\n",
  2969. rc);
  2970. goto free_timer;
  2971. }
  2972. vscsi->map_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  2973. if (!vscsi->map_buf) {
  2974. rc = -ENOMEM;
  2975. dev_err(&vscsi->dev, "probe: allocating cmd buffer failed\n");
  2976. goto destroy_queue;
  2977. }
  2978. vscsi->map_ioba = dma_map_single(&vdev->dev, vscsi->map_buf, PAGE_SIZE,
  2979. DMA_BIDIRECTIONAL);
  2980. if (dma_mapping_error(&vdev->dev, vscsi->map_ioba)) {
  2981. rc = -ENOMEM;
  2982. dev_err(&vscsi->dev, "probe: error mapping command buffer\n");
  2983. goto free_buf;
  2984. }
  2985. hrc = h_vioctl(vscsi->dds.unit_id, H_GET_PARTNER_INFO,
  2986. (u64)vscsi->map_ioba | ((u64)PAGE_SIZE << 32), 0, 0, 0,
  2987. 0);
  2988. if (hrc == H_SUCCESS)
  2989. vscsi->client_data.partition_number =
  2990. be64_to_cpu(*(u64 *)vscsi->map_buf);
  2991. /*
  2992. * We expect the VIOCTL to fail if we're configured as "any
  2993. * client can connect" and the client isn't activated yet.
  2994. * We'll make the call again when he sends an init msg.
  2995. */
  2996. pr_debug("probe hrc %ld, client partition num %d\n",
  2997. hrc, vscsi->client_data.partition_number);
  2998. tasklet_init(&vscsi->work_task, ibmvscsis_handle_crq,
  2999. (unsigned long)vscsi);
  3000. init_completion(&vscsi->wait_idle);
  3001. init_completion(&vscsi->unconfig);
  3002. snprintf(wq_name, 24, "ibmvscsis%s", dev_name(&vdev->dev));
  3003. vscsi->work_q = create_workqueue(wq_name);
  3004. if (!vscsi->work_q) {
  3005. rc = -ENOMEM;
  3006. dev_err(&vscsi->dev, "create_workqueue failed\n");
  3007. goto unmap_buf;
  3008. }
  3009. rc = request_irq(vdev->irq, ibmvscsis_interrupt, 0, "ibmvscsis", vscsi);
  3010. if (rc) {
  3011. rc = -EPERM;
  3012. dev_err(&vscsi->dev, "probe: request_irq failed, rc %d\n", rc);
  3013. goto destroy_WQ;
  3014. }
  3015. vscsi->state = WAIT_ENABLED;
  3016. dev_set_drvdata(&vdev->dev, vscsi);
  3017. return 0;
  3018. destroy_WQ:
  3019. destroy_workqueue(vscsi->work_q);
  3020. unmap_buf:
  3021. dma_unmap_single(&vdev->dev, vscsi->map_ioba, PAGE_SIZE,
  3022. DMA_BIDIRECTIONAL);
  3023. free_buf:
  3024. kfree(vscsi->map_buf);
  3025. destroy_queue:
  3026. tasklet_kill(&vscsi->work_task);
  3027. ibmvscsis_unregister_command_q(vscsi);
  3028. ibmvscsis_destroy_command_q(vscsi);
  3029. free_timer:
  3030. ibmvscsis_freetimer(vscsi);
  3031. free_cmds:
  3032. ibmvscsis_free_cmds(vscsi);
  3033. free_target:
  3034. srp_target_free(&vscsi->target);
  3035. rem_list:
  3036. spin_lock_bh(&ibmvscsis_dev_lock);
  3037. list_del(&vscsi->list);
  3038. spin_unlock_bh(&ibmvscsis_dev_lock);
  3039. free_adapter:
  3040. kfree(vscsi);
  3041. return rc;
  3042. }
  3043. static int ibmvscsis_remove(struct vio_dev *vdev)
  3044. {
  3045. struct scsi_info *vscsi = dev_get_drvdata(&vdev->dev);
  3046. pr_debug("remove (%s)\n", dev_name(&vscsi->dma_dev->dev));
  3047. spin_lock_bh(&vscsi->intr_lock);
  3048. ibmvscsis_post_disconnect(vscsi, UNCONFIGURING, 0);
  3049. vscsi->flags |= CFG_SLEEPING;
  3050. spin_unlock_bh(&vscsi->intr_lock);
  3051. wait_for_completion(&vscsi->unconfig);
  3052. vio_disable_interrupts(vdev);
  3053. free_irq(vdev->irq, vscsi);
  3054. destroy_workqueue(vscsi->work_q);
  3055. dma_unmap_single(&vdev->dev, vscsi->map_ioba, PAGE_SIZE,
  3056. DMA_BIDIRECTIONAL);
  3057. kfree(vscsi->map_buf);
  3058. tasklet_kill(&vscsi->work_task);
  3059. ibmvscsis_destroy_command_q(vscsi);
  3060. ibmvscsis_freetimer(vscsi);
  3061. ibmvscsis_free_cmds(vscsi);
  3062. srp_target_free(&vscsi->target);
  3063. spin_lock_bh(&ibmvscsis_dev_lock);
  3064. list_del(&vscsi->list);
  3065. spin_unlock_bh(&ibmvscsis_dev_lock);
  3066. kfree(vscsi);
  3067. return 0;
  3068. }
  3069. static ssize_t system_id_show(struct device *dev,
  3070. struct device_attribute *attr, char *buf)
  3071. {
  3072. return snprintf(buf, PAGE_SIZE, "%s\n", system_id);
  3073. }
  3074. static ssize_t partition_number_show(struct device *dev,
  3075. struct device_attribute *attr, char *buf)
  3076. {
  3077. return snprintf(buf, PAGE_SIZE, "%x\n", partition_number);
  3078. }
  3079. static ssize_t unit_address_show(struct device *dev,
  3080. struct device_attribute *attr, char *buf)
  3081. {
  3082. struct scsi_info *vscsi = container_of(dev, struct scsi_info, dev);
  3083. return snprintf(buf, PAGE_SIZE, "%x\n", vscsi->dma_dev->unit_address);
  3084. }
  3085. static int ibmvscsis_get_system_info(void)
  3086. {
  3087. struct device_node *rootdn, *vdevdn;
  3088. const char *id, *model, *name;
  3089. const uint *num;
  3090. rootdn = of_find_node_by_path("/");
  3091. if (!rootdn)
  3092. return -ENOENT;
  3093. model = of_get_property(rootdn, "model", NULL);
  3094. id = of_get_property(rootdn, "system-id", NULL);
  3095. if (model && id)
  3096. snprintf(system_id, sizeof(system_id), "%s-%s", model, id);
  3097. name = of_get_property(rootdn, "ibm,partition-name", NULL);
  3098. if (name)
  3099. strncpy(partition_name, name, sizeof(partition_name));
  3100. num = of_get_property(rootdn, "ibm,partition-no", NULL);
  3101. if (num)
  3102. partition_number = of_read_number(num, 1);
  3103. of_node_put(rootdn);
  3104. vdevdn = of_find_node_by_path("/vdevice");
  3105. if (vdevdn) {
  3106. const uint *mvds;
  3107. mvds = of_get_property(vdevdn, "ibm,max-virtual-dma-size",
  3108. NULL);
  3109. if (mvds)
  3110. max_vdma_size = *mvds;
  3111. of_node_put(vdevdn);
  3112. }
  3113. return 0;
  3114. }
  3115. static char *ibmvscsis_get_fabric_name(void)
  3116. {
  3117. return "ibmvscsis";
  3118. }
  3119. static char *ibmvscsis_get_fabric_wwn(struct se_portal_group *se_tpg)
  3120. {
  3121. struct ibmvscsis_tport *tport =
  3122. container_of(se_tpg, struct ibmvscsis_tport, se_tpg);
  3123. return tport->tport_name;
  3124. }
  3125. static u16 ibmvscsis_get_tag(struct se_portal_group *se_tpg)
  3126. {
  3127. struct ibmvscsis_tport *tport =
  3128. container_of(se_tpg, struct ibmvscsis_tport, se_tpg);
  3129. return tport->tport_tpgt;
  3130. }
  3131. static u32 ibmvscsis_get_default_depth(struct se_portal_group *se_tpg)
  3132. {
  3133. return 1;
  3134. }
  3135. static int ibmvscsis_check_true(struct se_portal_group *se_tpg)
  3136. {
  3137. return 1;
  3138. }
  3139. static int ibmvscsis_check_false(struct se_portal_group *se_tpg)
  3140. {
  3141. return 0;
  3142. }
  3143. static u32 ibmvscsis_tpg_get_inst_index(struct se_portal_group *se_tpg)
  3144. {
  3145. return 1;
  3146. }
  3147. static int ibmvscsis_check_stop_free(struct se_cmd *se_cmd)
  3148. {
  3149. return target_put_sess_cmd(se_cmd);
  3150. }
  3151. static void ibmvscsis_release_cmd(struct se_cmd *se_cmd)
  3152. {
  3153. struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
  3154. se_cmd);
  3155. struct scsi_info *vscsi = cmd->adapter;
  3156. spin_lock_bh(&vscsi->intr_lock);
  3157. /* Remove from active_q */
  3158. list_move_tail(&cmd->list, &vscsi->waiting_rsp);
  3159. ibmvscsis_send_messages(vscsi);
  3160. spin_unlock_bh(&vscsi->intr_lock);
  3161. }
  3162. static u32 ibmvscsis_sess_get_index(struct se_session *se_sess)
  3163. {
  3164. return 0;
  3165. }
  3166. static int ibmvscsis_write_pending(struct se_cmd *se_cmd)
  3167. {
  3168. struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
  3169. se_cmd);
  3170. struct scsi_info *vscsi = cmd->adapter;
  3171. struct iu_entry *iue = cmd->iue;
  3172. int rc;
  3173. /*
  3174. * If CLIENT_FAILED OR RESPONSE_Q_DOWN, then just return success
  3175. * since LIO can't do anything about it, and we dont want to
  3176. * attempt an srp_transfer_data.
  3177. */
  3178. if ((vscsi->flags & (CLIENT_FAILED | RESPONSE_Q_DOWN))) {
  3179. pr_err("write_pending failed since: %d\n", vscsi->flags);
  3180. return 0;
  3181. }
  3182. rc = srp_transfer_data(cmd, &vio_iu(iue)->srp.cmd, ibmvscsis_rdma,
  3183. 1, 1);
  3184. if (rc) {
  3185. pr_err("srp_transfer_data() failed: %d\n", rc);
  3186. return -EIO;
  3187. }
  3188. /*
  3189. * We now tell TCM to add this WRITE CDB directly into the TCM storage
  3190. * object execution queue.
  3191. */
  3192. target_execute_cmd(se_cmd);
  3193. return 0;
  3194. }
  3195. static int ibmvscsis_write_pending_status(struct se_cmd *se_cmd)
  3196. {
  3197. return 0;
  3198. }
  3199. static void ibmvscsis_set_default_node_attrs(struct se_node_acl *nacl)
  3200. {
  3201. }
  3202. static int ibmvscsis_get_cmd_state(struct se_cmd *se_cmd)
  3203. {
  3204. return 0;
  3205. }
  3206. static int ibmvscsis_queue_data_in(struct se_cmd *se_cmd)
  3207. {
  3208. struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
  3209. se_cmd);
  3210. struct iu_entry *iue = cmd->iue;
  3211. struct scsi_info *vscsi = cmd->adapter;
  3212. char *sd;
  3213. uint len = 0;
  3214. int rc;
  3215. rc = srp_transfer_data(cmd, &vio_iu(iue)->srp.cmd, ibmvscsis_rdma, 1,
  3216. 1);
  3217. if (rc) {
  3218. pr_err("srp_transfer_data failed: %d\n", rc);
  3219. sd = se_cmd->sense_buffer;
  3220. se_cmd->scsi_sense_length = 18;
  3221. memset(se_cmd->sense_buffer, 0, se_cmd->scsi_sense_length);
  3222. /* Logical Unit Communication Time-out asc/ascq = 0x0801 */
  3223. scsi_build_sense_buffer(0, se_cmd->sense_buffer, MEDIUM_ERROR,
  3224. 0x08, 0x01);
  3225. }
  3226. srp_build_response(vscsi, cmd, &len);
  3227. cmd->rsp.format = SRP_FORMAT;
  3228. cmd->rsp.len = len;
  3229. return 0;
  3230. }
  3231. static int ibmvscsis_queue_status(struct se_cmd *se_cmd)
  3232. {
  3233. struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
  3234. se_cmd);
  3235. struct scsi_info *vscsi = cmd->adapter;
  3236. uint len;
  3237. pr_debug("queue_status %p\n", se_cmd);
  3238. srp_build_response(vscsi, cmd, &len);
  3239. cmd->rsp.format = SRP_FORMAT;
  3240. cmd->rsp.len = len;
  3241. return 0;
  3242. }
  3243. static void ibmvscsis_queue_tm_rsp(struct se_cmd *se_cmd)
  3244. {
  3245. struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
  3246. se_cmd);
  3247. struct scsi_info *vscsi = cmd->adapter;
  3248. struct ibmvscsis_cmd *cmd_itr;
  3249. struct iu_entry *iue = iue = cmd->iue;
  3250. struct srp_tsk_mgmt *srp_tsk = &vio_iu(iue)->srp.tsk_mgmt;
  3251. u64 tag_to_abort = be64_to_cpu(srp_tsk->task_tag);
  3252. uint len;
  3253. pr_debug("queue_tm_rsp %p, status %d\n",
  3254. se_cmd, (int)se_cmd->se_tmr_req->response);
  3255. if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK &&
  3256. cmd->se_cmd.se_tmr_req->response == TMR_TASK_DOES_NOT_EXIST) {
  3257. spin_lock_bh(&vscsi->intr_lock);
  3258. list_for_each_entry(cmd_itr, &vscsi->active_q, list) {
  3259. if (tag_to_abort == cmd_itr->se_cmd.tag) {
  3260. cmd_itr->abort_cmd = cmd;
  3261. cmd->flags |= DELAY_SEND;
  3262. break;
  3263. }
  3264. }
  3265. spin_unlock_bh(&vscsi->intr_lock);
  3266. }
  3267. srp_build_response(vscsi, cmd, &len);
  3268. cmd->rsp.format = SRP_FORMAT;
  3269. cmd->rsp.len = len;
  3270. }
  3271. static void ibmvscsis_aborted_task(struct se_cmd *se_cmd)
  3272. {
  3273. pr_debug("ibmvscsis_aborted_task %p task_tag: %llu\n",
  3274. se_cmd, se_cmd->tag);
  3275. }
  3276. static struct se_wwn *ibmvscsis_make_tport(struct target_fabric_configfs *tf,
  3277. struct config_group *group,
  3278. const char *name)
  3279. {
  3280. struct ibmvscsis_tport *tport;
  3281. tport = ibmvscsis_lookup_port(name);
  3282. if (tport) {
  3283. tport->tport_proto_id = SCSI_PROTOCOL_SRP;
  3284. pr_debug("make_tport(%s), pointer:%p, tport_id:%x\n",
  3285. name, tport, tport->tport_proto_id);
  3286. return &tport->tport_wwn;
  3287. }
  3288. return ERR_PTR(-EINVAL);
  3289. }
  3290. static void ibmvscsis_drop_tport(struct se_wwn *wwn)
  3291. {
  3292. struct ibmvscsis_tport *tport = container_of(wwn,
  3293. struct ibmvscsis_tport,
  3294. tport_wwn);
  3295. pr_debug("drop_tport(%s)\n",
  3296. config_item_name(&tport->tport_wwn.wwn_group.cg_item));
  3297. }
  3298. static struct se_portal_group *ibmvscsis_make_tpg(struct se_wwn *wwn,
  3299. struct config_group *group,
  3300. const char *name)
  3301. {
  3302. struct ibmvscsis_tport *tport =
  3303. container_of(wwn, struct ibmvscsis_tport, tport_wwn);
  3304. int rc;
  3305. tport->releasing = false;
  3306. rc = core_tpg_register(&tport->tport_wwn, &tport->se_tpg,
  3307. tport->tport_proto_id);
  3308. if (rc)
  3309. return ERR_PTR(rc);
  3310. return &tport->se_tpg;
  3311. }
  3312. static void ibmvscsis_drop_tpg(struct se_portal_group *se_tpg)
  3313. {
  3314. struct ibmvscsis_tport *tport = container_of(se_tpg,
  3315. struct ibmvscsis_tport,
  3316. se_tpg);
  3317. tport->releasing = true;
  3318. tport->enabled = false;
  3319. /*
  3320. * Release the virtual I_T Nexus for this ibmvscsis TPG
  3321. */
  3322. ibmvscsis_drop_nexus(tport);
  3323. /*
  3324. * Deregister the se_tpg from TCM..
  3325. */
  3326. core_tpg_deregister(se_tpg);
  3327. }
  3328. static ssize_t ibmvscsis_wwn_version_show(struct config_item *item,
  3329. char *page)
  3330. {
  3331. return scnprintf(page, PAGE_SIZE, "%s\n", IBMVSCSIS_VERSION);
  3332. }
  3333. CONFIGFS_ATTR_RO(ibmvscsis_wwn_, version);
  3334. static struct configfs_attribute *ibmvscsis_wwn_attrs[] = {
  3335. &ibmvscsis_wwn_attr_version,
  3336. NULL,
  3337. };
  3338. static ssize_t ibmvscsis_tpg_enable_show(struct config_item *item,
  3339. char *page)
  3340. {
  3341. struct se_portal_group *se_tpg = to_tpg(item);
  3342. struct ibmvscsis_tport *tport = container_of(se_tpg,
  3343. struct ibmvscsis_tport,
  3344. se_tpg);
  3345. return snprintf(page, PAGE_SIZE, "%d\n", (tport->enabled) ? 1 : 0);
  3346. }
  3347. static ssize_t ibmvscsis_tpg_enable_store(struct config_item *item,
  3348. const char *page, size_t count)
  3349. {
  3350. struct se_portal_group *se_tpg = to_tpg(item);
  3351. struct ibmvscsis_tport *tport = container_of(se_tpg,
  3352. struct ibmvscsis_tport,
  3353. se_tpg);
  3354. struct scsi_info *vscsi = container_of(tport, struct scsi_info, tport);
  3355. unsigned long tmp;
  3356. int rc;
  3357. long lrc;
  3358. rc = kstrtoul(page, 0, &tmp);
  3359. if (rc < 0) {
  3360. pr_err("Unable to extract srpt_tpg_store_enable\n");
  3361. return -EINVAL;
  3362. }
  3363. if ((tmp != 0) && (tmp != 1)) {
  3364. pr_err("Illegal value for srpt_tpg_store_enable\n");
  3365. return -EINVAL;
  3366. }
  3367. if (tmp) {
  3368. spin_lock_bh(&vscsi->intr_lock);
  3369. tport->enabled = true;
  3370. lrc = ibmvscsis_enable_change_state(vscsi);
  3371. if (lrc)
  3372. pr_err("enable_change_state failed, rc %ld state %d\n",
  3373. lrc, vscsi->state);
  3374. spin_unlock_bh(&vscsi->intr_lock);
  3375. } else {
  3376. spin_lock_bh(&vscsi->intr_lock);
  3377. tport->enabled = false;
  3378. /* This simulates the server going down */
  3379. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
  3380. spin_unlock_bh(&vscsi->intr_lock);
  3381. }
  3382. pr_debug("tpg_enable_store, tmp %ld, state %d\n", tmp, vscsi->state);
  3383. return count;
  3384. }
  3385. CONFIGFS_ATTR(ibmvscsis_tpg_, enable);
  3386. static struct configfs_attribute *ibmvscsis_tpg_attrs[] = {
  3387. &ibmvscsis_tpg_attr_enable,
  3388. NULL,
  3389. };
  3390. static const struct target_core_fabric_ops ibmvscsis_ops = {
  3391. .module = THIS_MODULE,
  3392. .name = "ibmvscsis",
  3393. .max_data_sg_nents = MAX_TXU / PAGE_SIZE,
  3394. .get_fabric_name = ibmvscsis_get_fabric_name,
  3395. .tpg_get_wwn = ibmvscsis_get_fabric_wwn,
  3396. .tpg_get_tag = ibmvscsis_get_tag,
  3397. .tpg_get_default_depth = ibmvscsis_get_default_depth,
  3398. .tpg_check_demo_mode = ibmvscsis_check_true,
  3399. .tpg_check_demo_mode_cache = ibmvscsis_check_true,
  3400. .tpg_check_demo_mode_write_protect = ibmvscsis_check_false,
  3401. .tpg_check_prod_mode_write_protect = ibmvscsis_check_false,
  3402. .tpg_get_inst_index = ibmvscsis_tpg_get_inst_index,
  3403. .check_stop_free = ibmvscsis_check_stop_free,
  3404. .release_cmd = ibmvscsis_release_cmd,
  3405. .sess_get_index = ibmvscsis_sess_get_index,
  3406. .write_pending = ibmvscsis_write_pending,
  3407. .write_pending_status = ibmvscsis_write_pending_status,
  3408. .set_default_node_attributes = ibmvscsis_set_default_node_attrs,
  3409. .get_cmd_state = ibmvscsis_get_cmd_state,
  3410. .queue_data_in = ibmvscsis_queue_data_in,
  3411. .queue_status = ibmvscsis_queue_status,
  3412. .queue_tm_rsp = ibmvscsis_queue_tm_rsp,
  3413. .aborted_task = ibmvscsis_aborted_task,
  3414. /*
  3415. * Setup function pointers for logic in target_core_fabric_configfs.c
  3416. */
  3417. .fabric_make_wwn = ibmvscsis_make_tport,
  3418. .fabric_drop_wwn = ibmvscsis_drop_tport,
  3419. .fabric_make_tpg = ibmvscsis_make_tpg,
  3420. .fabric_drop_tpg = ibmvscsis_drop_tpg,
  3421. .tfc_wwn_attrs = ibmvscsis_wwn_attrs,
  3422. .tfc_tpg_base_attrs = ibmvscsis_tpg_attrs,
  3423. };
  3424. static void ibmvscsis_dev_release(struct device *dev) {};
  3425. static struct class_attribute ibmvscsis_class_attrs[] = {
  3426. __ATTR_NULL,
  3427. };
  3428. static struct device_attribute dev_attr_system_id =
  3429. __ATTR(system_id, S_IRUGO, system_id_show, NULL);
  3430. static struct device_attribute dev_attr_partition_number =
  3431. __ATTR(partition_number, S_IRUGO, partition_number_show, NULL);
  3432. static struct device_attribute dev_attr_unit_address =
  3433. __ATTR(unit_address, S_IRUGO, unit_address_show, NULL);
  3434. static struct attribute *ibmvscsis_dev_attrs[] = {
  3435. &dev_attr_system_id.attr,
  3436. &dev_attr_partition_number.attr,
  3437. &dev_attr_unit_address.attr,
  3438. };
  3439. ATTRIBUTE_GROUPS(ibmvscsis_dev);
  3440. static struct class ibmvscsis_class = {
  3441. .name = "ibmvscsis",
  3442. .dev_release = ibmvscsis_dev_release,
  3443. .class_attrs = ibmvscsis_class_attrs,
  3444. .dev_groups = ibmvscsis_dev_groups,
  3445. };
  3446. static struct vio_device_id ibmvscsis_device_table[] = {
  3447. { "v-scsi-host", "IBM,v-scsi-host" },
  3448. { "", "" }
  3449. };
  3450. MODULE_DEVICE_TABLE(vio, ibmvscsis_device_table);
  3451. static struct vio_driver ibmvscsis_driver = {
  3452. .name = "ibmvscsis",
  3453. .id_table = ibmvscsis_device_table,
  3454. .probe = ibmvscsis_probe,
  3455. .remove = ibmvscsis_remove,
  3456. };
  3457. /*
  3458. * ibmvscsis_init() - Kernel Module initialization
  3459. *
  3460. * Note: vio_register_driver() registers callback functions, and at least one
  3461. * of those callback functions calls TCM - Linux IO Target Subsystem, thus
  3462. * the SCSI Target template must be registered before vio_register_driver()
  3463. * is called.
  3464. */
  3465. static int __init ibmvscsis_init(void)
  3466. {
  3467. int rc = 0;
  3468. rc = ibmvscsis_get_system_info();
  3469. if (rc) {
  3470. pr_err("rc %d from get_system_info\n", rc);
  3471. goto out;
  3472. }
  3473. rc = class_register(&ibmvscsis_class);
  3474. if (rc) {
  3475. pr_err("failed class register\n");
  3476. goto out;
  3477. }
  3478. rc = target_register_template(&ibmvscsis_ops);
  3479. if (rc) {
  3480. pr_err("rc %d from target_register_template\n", rc);
  3481. goto unregister_class;
  3482. }
  3483. rc = vio_register_driver(&ibmvscsis_driver);
  3484. if (rc) {
  3485. pr_err("rc %d from vio_register_driver\n", rc);
  3486. goto unregister_target;
  3487. }
  3488. return 0;
  3489. unregister_target:
  3490. target_unregister_template(&ibmvscsis_ops);
  3491. unregister_class:
  3492. class_unregister(&ibmvscsis_class);
  3493. out:
  3494. return rc;
  3495. }
  3496. static void __exit ibmvscsis_exit(void)
  3497. {
  3498. pr_info("Unregister IBM virtual SCSI host driver\n");
  3499. vio_unregister_driver(&ibmvscsis_driver);
  3500. target_unregister_template(&ibmvscsis_ops);
  3501. class_unregister(&ibmvscsis_class);
  3502. }
  3503. MODULE_DESCRIPTION("IBMVSCSIS fabric driver");
  3504. MODULE_AUTHOR("Bryant G. Ly and Michael Cyr");
  3505. MODULE_LICENSE("GPL");
  3506. MODULE_VERSION(IBMVSCSIS_VERSION);
  3507. module_init(ibmvscsis_init);
  3508. module_exit(ibmvscsis_exit);