libiscsi.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * iSCSI lib functions
  4. *
  5. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  6. * Copyright (C) 2004 - 2006 Mike Christie
  7. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  8. * Copyright (C) 2004 - 2005 Alex Aizman
  9. * maintained by [email protected]
  10. */
  11. #include <linux/types.h>
  12. #include <linux/kfifo.h>
  13. #include <linux/delay.h>
  14. #include <linux/log2.h>
  15. #include <linux/slab.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/module.h>
  18. #include <asm/unaligned.h>
  19. #include <net/tcp.h>
  20. #include <scsi/scsi_cmnd.h>
  21. #include <scsi/scsi_device.h>
  22. #include <scsi/scsi_eh.h>
  23. #include <scsi/scsi_tcq.h>
  24. #include <scsi/scsi_host.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/iscsi_proto.h>
  27. #include <scsi/scsi_transport.h>
  28. #include <scsi/scsi_transport_iscsi.h>
  29. #include <scsi/libiscsi.h>
  30. #include <trace/events/iscsi.h>
  31. static int iscsi_dbg_lib_conn;
  32. module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
  33. S_IRUGO | S_IWUSR);
  34. MODULE_PARM_DESC(debug_libiscsi_conn,
  35. "Turn on debugging for connections in libiscsi module. "
  36. "Set to 1 to turn on, and zero to turn off. Default is off.");
  37. static int iscsi_dbg_lib_session;
  38. module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
  39. S_IRUGO | S_IWUSR);
  40. MODULE_PARM_DESC(debug_libiscsi_session,
  41. "Turn on debugging for sessions in libiscsi module. "
  42. "Set to 1 to turn on, and zero to turn off. Default is off.");
  43. static int iscsi_dbg_lib_eh;
  44. module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
  45. S_IRUGO | S_IWUSR);
  46. MODULE_PARM_DESC(debug_libiscsi_eh,
  47. "Turn on debugging for error handling in libiscsi module. "
  48. "Set to 1 to turn on, and zero to turn off. Default is off.");
  49. #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
  50. do { \
  51. if (iscsi_dbg_lib_conn) \
  52. iscsi_conn_printk(KERN_INFO, _conn, \
  53. "%s " dbg_fmt, \
  54. __func__, ##arg); \
  55. iscsi_dbg_trace(trace_iscsi_dbg_conn, \
  56. &(_conn)->cls_conn->dev, \
  57. "%s " dbg_fmt, __func__, ##arg);\
  58. } while (0);
  59. #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
  60. do { \
  61. if (iscsi_dbg_lib_session) \
  62. iscsi_session_printk(KERN_INFO, _session, \
  63. "%s " dbg_fmt, \
  64. __func__, ##arg); \
  65. iscsi_dbg_trace(trace_iscsi_dbg_session, \
  66. &(_session)->cls_session->dev, \
  67. "%s " dbg_fmt, __func__, ##arg); \
  68. } while (0);
  69. #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
  70. do { \
  71. if (iscsi_dbg_lib_eh) \
  72. iscsi_session_printk(KERN_INFO, _session, \
  73. "%s " dbg_fmt, \
  74. __func__, ##arg); \
  75. iscsi_dbg_trace(trace_iscsi_dbg_eh, \
  76. &(_session)->cls_session->dev, \
  77. "%s " dbg_fmt, __func__, ##arg); \
  78. } while (0);
  79. #define ISCSI_CMD_COMPL_WAIT 5
  80. inline void iscsi_conn_queue_xmit(struct iscsi_conn *conn)
  81. {
  82. struct Scsi_Host *shost = conn->session->host;
  83. struct iscsi_host *ihost = shost_priv(shost);
  84. if (ihost->workq)
  85. queue_work(ihost->workq, &conn->xmitwork);
  86. }
  87. EXPORT_SYMBOL_GPL(iscsi_conn_queue_xmit);
  88. inline void iscsi_conn_queue_recv(struct iscsi_conn *conn)
  89. {
  90. struct Scsi_Host *shost = conn->session->host;
  91. struct iscsi_host *ihost = shost_priv(shost);
  92. if (ihost->workq && !test_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags))
  93. queue_work(ihost->workq, &conn->recvwork);
  94. }
  95. EXPORT_SYMBOL_GPL(iscsi_conn_queue_recv);
  96. static void __iscsi_update_cmdsn(struct iscsi_session *session,
  97. uint32_t exp_cmdsn, uint32_t max_cmdsn)
  98. {
  99. /*
  100. * standard specifies this check for when to update expected and
  101. * max sequence numbers
  102. */
  103. if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
  104. return;
  105. if (exp_cmdsn != session->exp_cmdsn &&
  106. !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
  107. session->exp_cmdsn = exp_cmdsn;
  108. if (max_cmdsn != session->max_cmdsn &&
  109. !iscsi_sna_lt(max_cmdsn, session->max_cmdsn))
  110. session->max_cmdsn = max_cmdsn;
  111. }
  112. void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  113. {
  114. __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
  115. be32_to_cpu(hdr->max_cmdsn));
  116. }
  117. EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
  118. /**
  119. * iscsi_prep_data_out_pdu - initialize Data-Out
  120. * @task: scsi command task
  121. * @r2t: R2T info
  122. * @hdr: iscsi data in pdu
  123. *
  124. * Notes:
  125. * Initialize Data-Out within this R2T sequence and finds
  126. * proper data_offset within this SCSI command.
  127. *
  128. * This function is called with connection lock taken.
  129. **/
  130. void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
  131. struct iscsi_data *hdr)
  132. {
  133. struct iscsi_conn *conn = task->conn;
  134. unsigned int left = r2t->data_length - r2t->sent;
  135. task->hdr_len = sizeof(struct iscsi_data);
  136. memset(hdr, 0, sizeof(struct iscsi_data));
  137. hdr->ttt = r2t->ttt;
  138. hdr->datasn = cpu_to_be32(r2t->datasn);
  139. r2t->datasn++;
  140. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  141. hdr->lun = task->lun;
  142. hdr->itt = task->hdr_itt;
  143. hdr->exp_statsn = r2t->exp_statsn;
  144. hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
  145. if (left > conn->max_xmit_dlength) {
  146. hton24(hdr->dlength, conn->max_xmit_dlength);
  147. r2t->data_count = conn->max_xmit_dlength;
  148. hdr->flags = 0;
  149. } else {
  150. hton24(hdr->dlength, left);
  151. r2t->data_count = left;
  152. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  153. }
  154. conn->dataout_pdus_cnt++;
  155. }
  156. EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
  157. static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
  158. {
  159. unsigned exp_len = task->hdr_len + len;
  160. if (exp_len > task->hdr_max) {
  161. WARN_ON(1);
  162. return -EINVAL;
  163. }
  164. WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
  165. task->hdr_len = exp_len;
  166. return 0;
  167. }
  168. /*
  169. * make an extended cdb AHS
  170. */
  171. static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
  172. {
  173. struct scsi_cmnd *cmd = task->sc;
  174. unsigned rlen, pad_len;
  175. unsigned short ahslength;
  176. struct iscsi_ecdb_ahdr *ecdb_ahdr;
  177. int rc;
  178. ecdb_ahdr = iscsi_next_hdr(task);
  179. rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
  180. BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
  181. ahslength = rlen + sizeof(ecdb_ahdr->reserved);
  182. pad_len = iscsi_padding(rlen);
  183. rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
  184. sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
  185. if (rc)
  186. return rc;
  187. if (pad_len)
  188. memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
  189. ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
  190. ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
  191. ecdb_ahdr->reserved = 0;
  192. memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
  193. ISCSI_DBG_SESSION(task->conn->session,
  194. "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
  195. "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
  196. "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
  197. task->hdr_len);
  198. return 0;
  199. }
  200. /**
  201. * iscsi_check_tmf_restrictions - check if a task is affected by TMF
  202. * @task: iscsi task
  203. * @opcode: opcode to check for
  204. *
  205. * During TMF a task has to be checked if it's affected.
  206. * All unrelated I/O can be passed through, but I/O to the
  207. * affected LUN should be restricted.
  208. * If 'fast_abort' is set we won't be sending any I/O to the
  209. * affected LUN.
  210. * Otherwise the target is waiting for all TTTs to be completed,
  211. * so we have to send all outstanding Data-Out PDUs to the target.
  212. */
  213. static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
  214. {
  215. struct iscsi_session *session = task->conn->session;
  216. struct iscsi_tm *tmf = &session->tmhdr;
  217. u64 hdr_lun;
  218. if (session->tmf_state == TMF_INITIAL)
  219. return 0;
  220. if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
  221. return 0;
  222. switch (ISCSI_TM_FUNC_VALUE(tmf)) {
  223. case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
  224. /*
  225. * Allow PDUs for unrelated LUNs
  226. */
  227. hdr_lun = scsilun_to_int(&tmf->lun);
  228. if (hdr_lun != task->sc->device->lun)
  229. return 0;
  230. fallthrough;
  231. case ISCSI_TM_FUNC_TARGET_WARM_RESET:
  232. /*
  233. * Fail all SCSI cmd PDUs
  234. */
  235. if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
  236. iscsi_session_printk(KERN_INFO, session,
  237. "task [op %x itt 0x%x/0x%x] rejected.\n",
  238. opcode, task->itt, task->hdr_itt);
  239. return -EACCES;
  240. }
  241. /*
  242. * And also all data-out PDUs in response to R2T
  243. * if fast_abort is set.
  244. */
  245. if (session->fast_abort) {
  246. iscsi_session_printk(KERN_INFO, session,
  247. "task [op %x itt 0x%x/0x%x] fast abort.\n",
  248. opcode, task->itt, task->hdr_itt);
  249. return -EACCES;
  250. }
  251. break;
  252. case ISCSI_TM_FUNC_ABORT_TASK:
  253. /*
  254. * the caller has already checked if the task
  255. * they want to abort was in the pending queue so if
  256. * we are here the cmd pdu has gone out already, and
  257. * we will only hit this for data-outs
  258. */
  259. if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
  260. task->hdr_itt == tmf->rtt) {
  261. ISCSI_DBG_SESSION(session,
  262. "Preventing task %x/%x from sending "
  263. "data-out due to abort task in "
  264. "progress\n", task->itt,
  265. task->hdr_itt);
  266. return -EACCES;
  267. }
  268. break;
  269. }
  270. return 0;
  271. }
  272. /**
  273. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  274. * @task: iscsi task
  275. *
  276. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  277. * fields like dlength or final based on how much data it sends
  278. */
  279. static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
  280. {
  281. struct iscsi_conn *conn = task->conn;
  282. struct iscsi_session *session = conn->session;
  283. struct scsi_cmnd *sc = task->sc;
  284. struct iscsi_scsi_req *hdr;
  285. unsigned hdrlength, cmd_len, transfer_length;
  286. itt_t itt;
  287. int rc;
  288. rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
  289. if (rc)
  290. return rc;
  291. if (conn->session->tt->alloc_pdu) {
  292. rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
  293. if (rc)
  294. return rc;
  295. }
  296. hdr = (struct iscsi_scsi_req *)task->hdr;
  297. itt = hdr->itt;
  298. memset(hdr, 0, sizeof(*hdr));
  299. if (session->tt->parse_pdu_itt)
  300. hdr->itt = task->hdr_itt = itt;
  301. else
  302. hdr->itt = task->hdr_itt = build_itt(task->itt,
  303. task->conn->session->age);
  304. task->hdr_len = 0;
  305. rc = iscsi_add_hdr(task, sizeof(*hdr));
  306. if (rc)
  307. return rc;
  308. hdr->opcode = ISCSI_OP_SCSI_CMD;
  309. hdr->flags = ISCSI_ATTR_SIMPLE;
  310. int_to_scsilun(sc->device->lun, &hdr->lun);
  311. task->lun = hdr->lun;
  312. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  313. cmd_len = sc->cmd_len;
  314. if (cmd_len < ISCSI_CDB_SIZE)
  315. memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
  316. else if (cmd_len > ISCSI_CDB_SIZE) {
  317. rc = iscsi_prep_ecdb_ahs(task);
  318. if (rc)
  319. return rc;
  320. cmd_len = ISCSI_CDB_SIZE;
  321. }
  322. memcpy(hdr->cdb, sc->cmnd, cmd_len);
  323. task->imm_count = 0;
  324. if (scsi_get_prot_op(sc) != SCSI_PROT_NORMAL)
  325. task->protected = true;
  326. transfer_length = scsi_transfer_length(sc);
  327. hdr->data_length = cpu_to_be32(transfer_length);
  328. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  329. struct iscsi_r2t_info *r2t = &task->unsol_r2t;
  330. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  331. /*
  332. * Write counters:
  333. *
  334. * imm_count bytes to be sent right after
  335. * SCSI PDU Header
  336. *
  337. * unsol_count bytes(as Data-Out) to be sent
  338. * without R2T ack right after
  339. * immediate data
  340. *
  341. * r2t data_length bytes to be sent via R2T ack's
  342. *
  343. * pad_count bytes to be sent as zero-padding
  344. */
  345. memset(r2t, 0, sizeof(*r2t));
  346. if (session->imm_data_en) {
  347. if (transfer_length >= session->first_burst)
  348. task->imm_count = min(session->first_burst,
  349. conn->max_xmit_dlength);
  350. else
  351. task->imm_count = min(transfer_length,
  352. conn->max_xmit_dlength);
  353. hton24(hdr->dlength, task->imm_count);
  354. } else
  355. zero_data(hdr->dlength);
  356. if (!session->initial_r2t_en) {
  357. r2t->data_length = min(session->first_burst,
  358. transfer_length) -
  359. task->imm_count;
  360. r2t->data_offset = task->imm_count;
  361. r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  362. r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
  363. }
  364. if (!task->unsol_r2t.data_length)
  365. /* No unsolicit Data-Out's */
  366. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  367. } else {
  368. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  369. zero_data(hdr->dlength);
  370. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  371. hdr->flags |= ISCSI_FLAG_CMD_READ;
  372. }
  373. /* calculate size of additional header segments (AHSs) */
  374. hdrlength = task->hdr_len - sizeof(*hdr);
  375. WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
  376. hdrlength /= ISCSI_PAD_LEN;
  377. WARN_ON(hdrlength >= 256);
  378. hdr->hlength = hdrlength & 0xFF;
  379. hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
  380. if (session->tt->init_task && session->tt->init_task(task))
  381. return -EIO;
  382. task->state = ISCSI_TASK_RUNNING;
  383. session->cmdsn++;
  384. conn->scsicmd_pdus_cnt++;
  385. ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
  386. "itt 0x%x len %d cmdsn %d win %d]\n",
  387. sc->sc_data_direction == DMA_TO_DEVICE ?
  388. "write" : "read", conn->id, sc, sc->cmnd[0],
  389. task->itt, transfer_length,
  390. session->cmdsn,
  391. session->max_cmdsn - session->exp_cmdsn + 1);
  392. return 0;
  393. }
  394. /**
  395. * iscsi_free_task - free a task
  396. * @task: iscsi cmd task
  397. *
  398. * Must be called with session back_lock.
  399. * This function returns the scsi command to scsi-ml or cleans
  400. * up mgmt tasks then returns the task to the pool.
  401. */
  402. static void iscsi_free_task(struct iscsi_task *task)
  403. {
  404. struct iscsi_conn *conn = task->conn;
  405. struct iscsi_session *session = conn->session;
  406. struct scsi_cmnd *sc = task->sc;
  407. int oldstate = task->state;
  408. ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
  409. task->itt, task->state, task->sc);
  410. session->tt->cleanup_task(task);
  411. task->state = ISCSI_TASK_FREE;
  412. task->sc = NULL;
  413. /*
  414. * login task is preallocated so do not free
  415. */
  416. if (conn->login_task == task)
  417. return;
  418. kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
  419. if (sc) {
  420. /* SCSI eh reuses commands to verify us */
  421. iscsi_cmd(sc)->task = NULL;
  422. /*
  423. * queue command may call this to free the task, so
  424. * it will decide how to return sc to scsi-ml.
  425. */
  426. if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
  427. scsi_done(sc);
  428. }
  429. }
  430. bool iscsi_get_task(struct iscsi_task *task)
  431. {
  432. return refcount_inc_not_zero(&task->refcount);
  433. }
  434. EXPORT_SYMBOL_GPL(iscsi_get_task);
  435. /**
  436. * __iscsi_put_task - drop the refcount on a task
  437. * @task: iscsi_task to drop the refcount on
  438. *
  439. * The back_lock must be held when calling in case it frees the task.
  440. */
  441. void __iscsi_put_task(struct iscsi_task *task)
  442. {
  443. if (refcount_dec_and_test(&task->refcount))
  444. iscsi_free_task(task);
  445. }
  446. EXPORT_SYMBOL_GPL(__iscsi_put_task);
  447. void iscsi_put_task(struct iscsi_task *task)
  448. {
  449. struct iscsi_session *session = task->conn->session;
  450. if (refcount_dec_and_test(&task->refcount)) {
  451. spin_lock_bh(&session->back_lock);
  452. iscsi_free_task(task);
  453. spin_unlock_bh(&session->back_lock);
  454. }
  455. }
  456. EXPORT_SYMBOL_GPL(iscsi_put_task);
  457. /**
  458. * iscsi_complete_task - finish a task
  459. * @task: iscsi cmd task
  460. * @state: state to complete task with
  461. *
  462. * Must be called with session back_lock.
  463. */
  464. static void iscsi_complete_task(struct iscsi_task *task, int state)
  465. {
  466. struct iscsi_conn *conn = task->conn;
  467. ISCSI_DBG_SESSION(conn->session,
  468. "complete task itt 0x%x state %d sc %p\n",
  469. task->itt, task->state, task->sc);
  470. if (task->state == ISCSI_TASK_COMPLETED ||
  471. task->state == ISCSI_TASK_ABRT_TMF ||
  472. task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
  473. task->state == ISCSI_TASK_REQUEUE_SCSIQ)
  474. return;
  475. WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
  476. task->state = state;
  477. if (READ_ONCE(conn->ping_task) == task)
  478. WRITE_ONCE(conn->ping_task, NULL);
  479. /* release get from queueing */
  480. __iscsi_put_task(task);
  481. }
  482. /**
  483. * iscsi_complete_scsi_task - finish scsi task normally
  484. * @task: iscsi task for scsi cmd
  485. * @exp_cmdsn: expected cmd sn in cpu format
  486. * @max_cmdsn: max cmd sn in cpu format
  487. *
  488. * This is used when drivers do not need or cannot perform
  489. * lower level pdu processing.
  490. *
  491. * Called with session back_lock
  492. */
  493. void iscsi_complete_scsi_task(struct iscsi_task *task,
  494. uint32_t exp_cmdsn, uint32_t max_cmdsn)
  495. {
  496. struct iscsi_conn *conn = task->conn;
  497. ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
  498. conn->last_recv = jiffies;
  499. __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
  500. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  501. }
  502. EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
  503. /*
  504. * Must be called with back and frwd lock
  505. */
  506. static bool cleanup_queued_task(struct iscsi_task *task)
  507. {
  508. struct iscsi_conn *conn = task->conn;
  509. bool early_complete = false;
  510. /*
  511. * We might have raced where we handled a R2T early and got a response
  512. * but have not yet taken the task off the requeue list, then a TMF or
  513. * recovery happened and so we can still see it here.
  514. */
  515. if (task->state == ISCSI_TASK_COMPLETED)
  516. early_complete = true;
  517. if (!list_empty(&task->running)) {
  518. list_del_init(&task->running);
  519. /*
  520. * If it's on a list but still running this could be cleanup
  521. * from a TMF or session recovery.
  522. */
  523. if (task->state == ISCSI_TASK_RUNNING ||
  524. task->state == ISCSI_TASK_COMPLETED)
  525. __iscsi_put_task(task);
  526. }
  527. if (conn->session->running_aborted_task == task) {
  528. conn->session->running_aborted_task = NULL;
  529. __iscsi_put_task(task);
  530. }
  531. if (conn->task == task) {
  532. conn->task = NULL;
  533. __iscsi_put_task(task);
  534. }
  535. return early_complete;
  536. }
  537. /*
  538. * session back and frwd lock must be held and if not called for a task that
  539. * is still pending or from the xmit thread, then xmit thread must be suspended
  540. */
  541. static void __fail_scsi_task(struct iscsi_task *task, int err)
  542. {
  543. struct iscsi_conn *conn = task->conn;
  544. struct scsi_cmnd *sc;
  545. int state;
  546. if (cleanup_queued_task(task))
  547. return;
  548. if (task->state == ISCSI_TASK_PENDING) {
  549. /*
  550. * cmd never made it to the xmit thread, so we should not count
  551. * the cmd in the sequencing
  552. */
  553. conn->session->queued_cmdsn--;
  554. /* it was never sent so just complete like normal */
  555. state = ISCSI_TASK_COMPLETED;
  556. } else if (err == DID_TRANSPORT_DISRUPTED)
  557. state = ISCSI_TASK_ABRT_SESS_RECOV;
  558. else
  559. state = ISCSI_TASK_ABRT_TMF;
  560. sc = task->sc;
  561. sc->result = err << 16;
  562. scsi_set_resid(sc, scsi_bufflen(sc));
  563. iscsi_complete_task(task, state);
  564. }
  565. static void fail_scsi_task(struct iscsi_task *task, int err)
  566. {
  567. struct iscsi_session *session = task->conn->session;
  568. spin_lock_bh(&session->back_lock);
  569. __fail_scsi_task(task, err);
  570. spin_unlock_bh(&session->back_lock);
  571. }
  572. static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
  573. struct iscsi_task *task)
  574. {
  575. struct iscsi_session *session = conn->session;
  576. struct iscsi_hdr *hdr = task->hdr;
  577. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  578. uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  579. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  580. return -ENOTCONN;
  581. if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
  582. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  583. /*
  584. * pre-format CmdSN for outgoing PDU.
  585. */
  586. nop->cmdsn = cpu_to_be32(session->cmdsn);
  587. if (hdr->itt != RESERVED_ITT) {
  588. /*
  589. * TODO: We always use immediate for normal session pdus.
  590. * If we start to send tmfs or nops as non-immediate then
  591. * we should start checking the cmdsn numbers for mgmt tasks.
  592. *
  593. * During discovery sessions iscsid sends TEXT as non immediate,
  594. * but we always only send one PDU at a time.
  595. */
  596. if (conn->c_stage == ISCSI_CONN_STARTED &&
  597. !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
  598. session->queued_cmdsn++;
  599. session->cmdsn++;
  600. }
  601. }
  602. if (session->tt->init_task && session->tt->init_task(task))
  603. return -EIO;
  604. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  605. session->state = ISCSI_STATE_LOGGING_OUT;
  606. task->state = ISCSI_TASK_RUNNING;
  607. ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
  608. "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
  609. hdr->itt, task->data_count);
  610. return 0;
  611. }
  612. /**
  613. * iscsi_alloc_mgmt_task - allocate and setup a mgmt task.
  614. * @conn: iscsi conn that the task will be sent on.
  615. * @hdr: iscsi pdu that will be sent.
  616. * @data: buffer for data segment if needed.
  617. * @data_size: length of data in bytes.
  618. */
  619. static struct iscsi_task *
  620. iscsi_alloc_mgmt_task(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  621. char *data, uint32_t data_size)
  622. {
  623. struct iscsi_session *session = conn->session;
  624. uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  625. struct iscsi_task *task;
  626. itt_t itt;
  627. if (session->state == ISCSI_STATE_TERMINATE ||
  628. !test_bit(ISCSI_CONN_FLAG_BOUND, &conn->flags))
  629. return NULL;
  630. if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
  631. /*
  632. * Login and Text are sent serially, in
  633. * request-followed-by-response sequence.
  634. * Same task can be used. Same ITT must be used.
  635. * Note that login_task is preallocated at conn_create().
  636. */
  637. if (conn->login_task->state != ISCSI_TASK_FREE) {
  638. iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
  639. "progress. Cannot start new task.\n");
  640. return NULL;
  641. }
  642. if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) {
  643. iscsi_conn_printk(KERN_ERR, conn, "Invalid buffer len of %u for login task. Max len is %u\n", data_size, ISCSI_DEF_MAX_RECV_SEG_LEN);
  644. return NULL;
  645. }
  646. task = conn->login_task;
  647. } else {
  648. if (session->state != ISCSI_STATE_LOGGED_IN)
  649. return NULL;
  650. if (data_size != 0) {
  651. iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode);
  652. return NULL;
  653. }
  654. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  655. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  656. if (!kfifo_out(&session->cmdpool.queue,
  657. (void*)&task, sizeof(void*)))
  658. return NULL;
  659. }
  660. /*
  661. * released in complete pdu for task we expect a response for, and
  662. * released by the lld when it has transmitted the task for
  663. * pdus we do not expect a response for.
  664. */
  665. refcount_set(&task->refcount, 1);
  666. task->conn = conn;
  667. task->sc = NULL;
  668. INIT_LIST_HEAD(&task->running);
  669. task->state = ISCSI_TASK_PENDING;
  670. if (data_size) {
  671. memcpy(task->data, data, data_size);
  672. task->data_count = data_size;
  673. } else
  674. task->data_count = 0;
  675. if (conn->session->tt->alloc_pdu) {
  676. if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
  677. iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
  678. "pdu for mgmt task.\n");
  679. goto free_task;
  680. }
  681. }
  682. itt = task->hdr->itt;
  683. task->hdr_len = sizeof(struct iscsi_hdr);
  684. memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
  685. if (hdr->itt != RESERVED_ITT) {
  686. if (session->tt->parse_pdu_itt)
  687. task->hdr->itt = itt;
  688. else
  689. task->hdr->itt = build_itt(task->itt,
  690. task->conn->session->age);
  691. }
  692. return task;
  693. free_task:
  694. iscsi_put_task(task);
  695. return NULL;
  696. }
  697. /**
  698. * iscsi_send_mgmt_task - Send task created with iscsi_alloc_mgmt_task.
  699. * @task: iscsi task to send.
  700. *
  701. * On failure this returns a non-zero error code, and the driver must free
  702. * the task with iscsi_put_task;
  703. */
  704. static int iscsi_send_mgmt_task(struct iscsi_task *task)
  705. {
  706. struct iscsi_conn *conn = task->conn;
  707. struct iscsi_session *session = conn->session;
  708. struct iscsi_host *ihost = shost_priv(conn->session->host);
  709. int rc = 0;
  710. if (!ihost->workq) {
  711. rc = iscsi_prep_mgmt_task(conn, task);
  712. if (rc)
  713. return rc;
  714. rc = session->tt->xmit_task(task);
  715. if (rc)
  716. return rc;
  717. } else {
  718. list_add_tail(&task->running, &conn->mgmtqueue);
  719. iscsi_conn_queue_xmit(conn);
  720. }
  721. return 0;
  722. }
  723. static int __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  724. char *data, uint32_t data_size)
  725. {
  726. struct iscsi_task *task;
  727. int rc;
  728. task = iscsi_alloc_mgmt_task(conn, hdr, data, data_size);
  729. if (!task)
  730. return -ENOMEM;
  731. rc = iscsi_send_mgmt_task(task);
  732. if (rc)
  733. iscsi_put_task(task);
  734. return rc;
  735. }
  736. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  737. char *data, uint32_t data_size)
  738. {
  739. struct iscsi_conn *conn = cls_conn->dd_data;
  740. struct iscsi_session *session = conn->session;
  741. int err = 0;
  742. spin_lock_bh(&session->frwd_lock);
  743. if (__iscsi_conn_send_pdu(conn, hdr, data, data_size))
  744. err = -EPERM;
  745. spin_unlock_bh(&session->frwd_lock);
  746. return err;
  747. }
  748. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  749. /**
  750. * iscsi_scsi_cmd_rsp - SCSI Command Response processing
  751. * @conn: iscsi connection
  752. * @hdr: iscsi header
  753. * @task: scsi command task
  754. * @data: cmd data buffer
  755. * @datalen: len of buffer
  756. *
  757. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  758. * then completes the command and task. called under back_lock
  759. **/
  760. static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  761. struct iscsi_task *task, char *data,
  762. int datalen)
  763. {
  764. struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
  765. struct iscsi_session *session = conn->session;
  766. struct scsi_cmnd *sc = task->sc;
  767. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  768. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  769. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  770. if (task->protected) {
  771. sector_t sector;
  772. u8 ascq;
  773. /**
  774. * Transports that didn't implement check_protection
  775. * callback but still published T10-PI support to scsi-mid
  776. * deserve this BUG_ON.
  777. **/
  778. BUG_ON(!session->tt->check_protection);
  779. ascq = session->tt->check_protection(task, &sector);
  780. if (ascq) {
  781. scsi_build_sense(sc, 1, ILLEGAL_REQUEST, 0x10, ascq);
  782. scsi_set_sense_information(sc->sense_buffer,
  783. SCSI_SENSE_BUFFERSIZE,
  784. sector);
  785. goto out;
  786. }
  787. }
  788. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  789. sc->result = DID_ERROR << 16;
  790. goto out;
  791. }
  792. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  793. uint16_t senselen;
  794. if (datalen < 2) {
  795. invalid_datalen:
  796. iscsi_conn_printk(KERN_ERR, conn,
  797. "Got CHECK_CONDITION but invalid data "
  798. "buffer size of %d\n", datalen);
  799. sc->result = DID_BAD_TARGET << 16;
  800. goto out;
  801. }
  802. senselen = get_unaligned_be16(data);
  803. if (datalen < senselen)
  804. goto invalid_datalen;
  805. memcpy(sc->sense_buffer, data + 2,
  806. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  807. ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
  808. min_t(uint16_t, senselen,
  809. SCSI_SENSE_BUFFERSIZE));
  810. }
  811. if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
  812. ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
  813. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  814. }
  815. if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
  816. ISCSI_FLAG_CMD_OVERFLOW)) {
  817. int res_count = be32_to_cpu(rhdr->residual_count);
  818. if (res_count > 0 &&
  819. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  820. res_count <= scsi_bufflen(sc)))
  821. /* write side for bidi or uni-io set_resid */
  822. scsi_set_resid(sc, res_count);
  823. else
  824. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  825. }
  826. out:
  827. ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
  828. sc, sc->result, task->itt);
  829. conn->scsirsp_pdus_cnt++;
  830. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  831. }
  832. /**
  833. * iscsi_data_in_rsp - SCSI Data-In Response processing
  834. * @conn: iscsi connection
  835. * @hdr: iscsi pdu
  836. * @task: scsi command task
  837. *
  838. * iscsi_data_in_rsp sets up the scsi_cmnd fields based on the data received
  839. * then completes the command and task. called under back_lock
  840. **/
  841. static void
  842. iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  843. struct iscsi_task *task)
  844. {
  845. struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
  846. struct scsi_cmnd *sc = task->sc;
  847. if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
  848. return;
  849. iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
  850. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  851. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  852. if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
  853. ISCSI_FLAG_DATA_OVERFLOW)) {
  854. int res_count = be32_to_cpu(rhdr->residual_count);
  855. if (res_count > 0 &&
  856. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  857. res_count <= sc->sdb.length))
  858. scsi_set_resid(sc, res_count);
  859. else
  860. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  861. }
  862. ISCSI_DBG_SESSION(conn->session, "data in with status done "
  863. "[sc %p res %d itt 0x%x]\n",
  864. sc, sc->result, task->itt);
  865. conn->scsirsp_pdus_cnt++;
  866. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  867. }
  868. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  869. {
  870. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  871. struct iscsi_session *session = conn->session;
  872. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  873. conn->tmfrsp_pdus_cnt++;
  874. if (session->tmf_state != TMF_QUEUED)
  875. return;
  876. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  877. session->tmf_state = TMF_SUCCESS;
  878. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  879. session->tmf_state = TMF_NOT_FOUND;
  880. else
  881. session->tmf_state = TMF_FAILED;
  882. wake_up(&session->ehwait);
  883. }
  884. static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
  885. {
  886. struct iscsi_nopout hdr;
  887. struct iscsi_task *task;
  888. if (!rhdr) {
  889. if (READ_ONCE(conn->ping_task))
  890. return -EINVAL;
  891. }
  892. memset(&hdr, 0, sizeof(struct iscsi_nopout));
  893. hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
  894. hdr.flags = ISCSI_FLAG_CMD_FINAL;
  895. if (rhdr) {
  896. hdr.lun = rhdr->lun;
  897. hdr.ttt = rhdr->ttt;
  898. hdr.itt = RESERVED_ITT;
  899. } else
  900. hdr.ttt = RESERVED_ITT;
  901. task = iscsi_alloc_mgmt_task(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
  902. if (!task)
  903. return -ENOMEM;
  904. if (!rhdr)
  905. WRITE_ONCE(conn->ping_task, task);
  906. if (iscsi_send_mgmt_task(task)) {
  907. if (!rhdr)
  908. WRITE_ONCE(conn->ping_task, NULL);
  909. iscsi_put_task(task);
  910. iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
  911. return -EIO;
  912. } else if (!rhdr) {
  913. /* only track our nops */
  914. conn->last_ping = jiffies;
  915. }
  916. return 0;
  917. }
  918. /**
  919. * iscsi_nop_out_rsp - SCSI NOP Response processing
  920. * @task: scsi command task
  921. * @nop: the nop structure
  922. * @data: where to put the data
  923. * @datalen: length of data
  924. *
  925. * iscsi_nop_out_rsp handles nop response from use or
  926. * from user space. called under back_lock
  927. **/
  928. static int iscsi_nop_out_rsp(struct iscsi_task *task,
  929. struct iscsi_nopin *nop, char *data, int datalen)
  930. {
  931. struct iscsi_conn *conn = task->conn;
  932. int rc = 0;
  933. if (READ_ONCE(conn->ping_task) != task) {
  934. /*
  935. * If this is not in response to one of our
  936. * nops then it must be from userspace.
  937. */
  938. if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
  939. data, datalen))
  940. rc = ISCSI_ERR_CONN_FAILED;
  941. } else
  942. mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
  943. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  944. return rc;
  945. }
  946. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  947. char *data, int datalen)
  948. {
  949. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  950. struct iscsi_hdr rejected_pdu;
  951. int opcode, rc = 0;
  952. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  953. if (ntoh24(reject->dlength) > datalen ||
  954. ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
  955. iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
  956. "pdu. Invalid data length (pdu dlength "
  957. "%u, datalen %d\n", ntoh24(reject->dlength),
  958. datalen);
  959. return ISCSI_ERR_PROTO;
  960. }
  961. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  962. opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
  963. switch (reject->reason) {
  964. case ISCSI_REASON_DATA_DIGEST_ERROR:
  965. iscsi_conn_printk(KERN_ERR, conn,
  966. "pdu (op 0x%x itt 0x%x) rejected "
  967. "due to DataDigest error.\n",
  968. opcode, rejected_pdu.itt);
  969. break;
  970. case ISCSI_REASON_IMM_CMD_REJECT:
  971. iscsi_conn_printk(KERN_ERR, conn,
  972. "pdu (op 0x%x itt 0x%x) rejected. Too many "
  973. "immediate commands.\n",
  974. opcode, rejected_pdu.itt);
  975. /*
  976. * We only send one TMF at a time so if the target could not
  977. * handle it, then it should get fixed (RFC mandates that
  978. * a target can handle one immediate TMF per conn).
  979. *
  980. * For nops-outs, we could have sent more than one if
  981. * the target is sending us lots of nop-ins
  982. */
  983. if (opcode != ISCSI_OP_NOOP_OUT)
  984. return 0;
  985. if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
  986. /*
  987. * nop-out in response to target's nop-out rejected.
  988. * Just resend.
  989. */
  990. /* In RX path we are under back lock */
  991. spin_unlock(&conn->session->back_lock);
  992. spin_lock(&conn->session->frwd_lock);
  993. iscsi_send_nopout(conn,
  994. (struct iscsi_nopin*)&rejected_pdu);
  995. spin_unlock(&conn->session->frwd_lock);
  996. spin_lock(&conn->session->back_lock);
  997. } else {
  998. struct iscsi_task *task;
  999. /*
  1000. * Our nop as ping got dropped. We know the target
  1001. * and transport are ok so just clean up
  1002. */
  1003. task = iscsi_itt_to_task(conn, rejected_pdu.itt);
  1004. if (!task) {
  1005. iscsi_conn_printk(KERN_ERR, conn,
  1006. "Invalid pdu reject. Could "
  1007. "not lookup rejected task.\n");
  1008. rc = ISCSI_ERR_BAD_ITT;
  1009. } else
  1010. rc = iscsi_nop_out_rsp(task,
  1011. (struct iscsi_nopin*)&rejected_pdu,
  1012. NULL, 0);
  1013. }
  1014. break;
  1015. default:
  1016. iscsi_conn_printk(KERN_ERR, conn,
  1017. "pdu (op 0x%x itt 0x%x) rejected. Reason "
  1018. "code 0x%x\n", rejected_pdu.opcode,
  1019. rejected_pdu.itt, reject->reason);
  1020. break;
  1021. }
  1022. return rc;
  1023. }
  1024. /**
  1025. * iscsi_itt_to_task - look up task by itt
  1026. * @conn: iscsi connection
  1027. * @itt: itt
  1028. *
  1029. * This should be used for mgmt tasks like login and nops, or if
  1030. * the LDD's itt space does not include the session age.
  1031. *
  1032. * The session back_lock must be held.
  1033. */
  1034. struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
  1035. {
  1036. struct iscsi_session *session = conn->session;
  1037. int i;
  1038. if (itt == RESERVED_ITT)
  1039. return NULL;
  1040. if (session->tt->parse_pdu_itt)
  1041. session->tt->parse_pdu_itt(conn, itt, &i, NULL);
  1042. else
  1043. i = get_itt(itt);
  1044. if (i >= session->cmds_max)
  1045. return NULL;
  1046. return session->cmds[i];
  1047. }
  1048. EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
  1049. /**
  1050. * __iscsi_complete_pdu - complete pdu
  1051. * @conn: iscsi conn
  1052. * @hdr: iscsi header
  1053. * @data: data buffer
  1054. * @datalen: len of data buffer
  1055. *
  1056. * Completes pdu processing by freeing any resources allocated at
  1057. * queuecommand or send generic. session back_lock must be held and verify
  1058. * itt must have been called.
  1059. */
  1060. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  1061. char *data, int datalen)
  1062. {
  1063. struct iscsi_session *session = conn->session;
  1064. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  1065. struct iscsi_task *task;
  1066. uint32_t itt;
  1067. conn->last_recv = jiffies;
  1068. rc = iscsi_verify_itt(conn, hdr->itt);
  1069. if (rc)
  1070. return rc;
  1071. if (hdr->itt != RESERVED_ITT)
  1072. itt = get_itt(hdr->itt);
  1073. else
  1074. itt = ~0U;
  1075. ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
  1076. opcode, conn->id, itt, datalen);
  1077. if (itt == ~0U) {
  1078. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1079. switch(opcode) {
  1080. case ISCSI_OP_NOOP_IN:
  1081. if (datalen) {
  1082. rc = ISCSI_ERR_PROTO;
  1083. break;
  1084. }
  1085. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  1086. break;
  1087. /* In RX path we are under back lock */
  1088. spin_unlock(&session->back_lock);
  1089. spin_lock(&session->frwd_lock);
  1090. iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
  1091. spin_unlock(&session->frwd_lock);
  1092. spin_lock(&session->back_lock);
  1093. break;
  1094. case ISCSI_OP_REJECT:
  1095. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  1096. break;
  1097. case ISCSI_OP_ASYNC_EVENT:
  1098. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  1099. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  1100. rc = ISCSI_ERR_CONN_FAILED;
  1101. break;
  1102. default:
  1103. rc = ISCSI_ERR_BAD_OPCODE;
  1104. break;
  1105. }
  1106. goto out;
  1107. }
  1108. switch(opcode) {
  1109. case ISCSI_OP_SCSI_CMD_RSP:
  1110. case ISCSI_OP_SCSI_DATA_IN:
  1111. task = iscsi_itt_to_ctask(conn, hdr->itt);
  1112. if (!task)
  1113. return ISCSI_ERR_BAD_ITT;
  1114. task->last_xfer = jiffies;
  1115. break;
  1116. case ISCSI_OP_R2T:
  1117. /*
  1118. * LLD handles R2Ts if they need to.
  1119. */
  1120. return 0;
  1121. case ISCSI_OP_LOGOUT_RSP:
  1122. case ISCSI_OP_LOGIN_RSP:
  1123. case ISCSI_OP_TEXT_RSP:
  1124. case ISCSI_OP_SCSI_TMFUNC_RSP:
  1125. case ISCSI_OP_NOOP_IN:
  1126. task = iscsi_itt_to_task(conn, hdr->itt);
  1127. if (!task)
  1128. return ISCSI_ERR_BAD_ITT;
  1129. break;
  1130. default:
  1131. return ISCSI_ERR_BAD_OPCODE;
  1132. }
  1133. switch(opcode) {
  1134. case ISCSI_OP_SCSI_CMD_RSP:
  1135. iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
  1136. break;
  1137. case ISCSI_OP_SCSI_DATA_IN:
  1138. iscsi_data_in_rsp(conn, hdr, task);
  1139. break;
  1140. case ISCSI_OP_LOGOUT_RSP:
  1141. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1142. if (datalen) {
  1143. rc = ISCSI_ERR_PROTO;
  1144. break;
  1145. }
  1146. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  1147. goto recv_pdu;
  1148. case ISCSI_OP_LOGIN_RSP:
  1149. case ISCSI_OP_TEXT_RSP:
  1150. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1151. /*
  1152. * login related PDU's exp_statsn is handled in
  1153. * userspace
  1154. */
  1155. goto recv_pdu;
  1156. case ISCSI_OP_SCSI_TMFUNC_RSP:
  1157. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1158. if (datalen) {
  1159. rc = ISCSI_ERR_PROTO;
  1160. break;
  1161. }
  1162. iscsi_tmf_rsp(conn, hdr);
  1163. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  1164. break;
  1165. case ISCSI_OP_NOOP_IN:
  1166. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1167. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
  1168. rc = ISCSI_ERR_PROTO;
  1169. break;
  1170. }
  1171. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  1172. rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
  1173. data, datalen);
  1174. break;
  1175. default:
  1176. rc = ISCSI_ERR_BAD_OPCODE;
  1177. break;
  1178. }
  1179. out:
  1180. return rc;
  1181. recv_pdu:
  1182. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  1183. rc = ISCSI_ERR_CONN_FAILED;
  1184. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  1185. return rc;
  1186. }
  1187. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  1188. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  1189. char *data, int datalen)
  1190. {
  1191. int rc;
  1192. spin_lock(&conn->session->back_lock);
  1193. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  1194. spin_unlock(&conn->session->back_lock);
  1195. return rc;
  1196. }
  1197. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  1198. int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
  1199. {
  1200. struct iscsi_session *session = conn->session;
  1201. int age = 0, i = 0;
  1202. if (itt == RESERVED_ITT)
  1203. return 0;
  1204. if (session->tt->parse_pdu_itt)
  1205. session->tt->parse_pdu_itt(conn, itt, &i, &age);
  1206. else {
  1207. i = get_itt(itt);
  1208. age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
  1209. }
  1210. if (age != session->age) {
  1211. iscsi_conn_printk(KERN_ERR, conn,
  1212. "received itt %x expected session age (%x)\n",
  1213. (__force u32)itt, session->age);
  1214. return ISCSI_ERR_BAD_ITT;
  1215. }
  1216. if (i >= session->cmds_max) {
  1217. iscsi_conn_printk(KERN_ERR, conn,
  1218. "received invalid itt index %u (max cmds "
  1219. "%u.\n", i, session->cmds_max);
  1220. return ISCSI_ERR_BAD_ITT;
  1221. }
  1222. return 0;
  1223. }
  1224. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  1225. /**
  1226. * iscsi_itt_to_ctask - look up ctask by itt
  1227. * @conn: iscsi connection
  1228. * @itt: itt
  1229. *
  1230. * This should be used for cmd tasks.
  1231. *
  1232. * The session back_lock must be held.
  1233. */
  1234. struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
  1235. {
  1236. struct iscsi_task *task;
  1237. if (iscsi_verify_itt(conn, itt))
  1238. return NULL;
  1239. task = iscsi_itt_to_task(conn, itt);
  1240. if (!task || !task->sc)
  1241. return NULL;
  1242. if (iscsi_cmd(task->sc)->age != conn->session->age) {
  1243. iscsi_session_printk(KERN_ERR, conn->session,
  1244. "task's session age %d, expected %d\n",
  1245. iscsi_cmd(task->sc)->age, conn->session->age);
  1246. return NULL;
  1247. }
  1248. return task;
  1249. }
  1250. EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
  1251. void iscsi_session_failure(struct iscsi_session *session,
  1252. enum iscsi_err err)
  1253. {
  1254. struct iscsi_conn *conn;
  1255. spin_lock_bh(&session->frwd_lock);
  1256. conn = session->leadconn;
  1257. if (session->state == ISCSI_STATE_TERMINATE || !conn) {
  1258. spin_unlock_bh(&session->frwd_lock);
  1259. return;
  1260. }
  1261. iscsi_get_conn(conn->cls_conn);
  1262. spin_unlock_bh(&session->frwd_lock);
  1263. /*
  1264. * if the host is being removed bypass the connection
  1265. * recovery initialization because we are going to kill
  1266. * the session.
  1267. */
  1268. if (err == ISCSI_ERR_INVALID_HOST)
  1269. iscsi_conn_error_event(conn->cls_conn, err);
  1270. else
  1271. iscsi_conn_failure(conn, err);
  1272. iscsi_put_conn(conn->cls_conn);
  1273. }
  1274. EXPORT_SYMBOL_GPL(iscsi_session_failure);
  1275. static bool iscsi_set_conn_failed(struct iscsi_conn *conn)
  1276. {
  1277. struct iscsi_session *session = conn->session;
  1278. if (session->state == ISCSI_STATE_FAILED)
  1279. return false;
  1280. if (conn->stop_stage == 0)
  1281. session->state = ISCSI_STATE_FAILED;
  1282. set_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags);
  1283. set_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags);
  1284. return true;
  1285. }
  1286. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  1287. {
  1288. struct iscsi_session *session = conn->session;
  1289. bool needs_evt;
  1290. spin_lock_bh(&session->frwd_lock);
  1291. needs_evt = iscsi_set_conn_failed(conn);
  1292. spin_unlock_bh(&session->frwd_lock);
  1293. if (needs_evt)
  1294. iscsi_conn_error_event(conn->cls_conn, err);
  1295. }
  1296. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  1297. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  1298. {
  1299. struct iscsi_session *session = conn->session;
  1300. /*
  1301. * Check for iSCSI window and take care of CmdSN wrap-around
  1302. */
  1303. if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
  1304. ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
  1305. "%u MaxCmdSN %u CmdSN %u/%u\n",
  1306. session->exp_cmdsn, session->max_cmdsn,
  1307. session->cmdsn, session->queued_cmdsn);
  1308. return -ENOSPC;
  1309. }
  1310. return 0;
  1311. }
  1312. static int iscsi_xmit_task(struct iscsi_conn *conn, struct iscsi_task *task,
  1313. bool was_requeue)
  1314. {
  1315. int rc;
  1316. if (!conn->task) {
  1317. /*
  1318. * Take a ref so we can access it after xmit_task().
  1319. *
  1320. * This should never fail because the failure paths will have
  1321. * stopped the xmit thread.
  1322. */
  1323. if (!iscsi_get_task(task)) {
  1324. WARN_ON_ONCE(1);
  1325. return 0;
  1326. }
  1327. } else {
  1328. /* Already have a ref from when we failed to send it last call */
  1329. conn->task = NULL;
  1330. }
  1331. /*
  1332. * If this was a requeue for a R2T we have an extra ref on the task in
  1333. * case a bad target sends a cmd rsp before we have handled the task.
  1334. */
  1335. if (was_requeue)
  1336. iscsi_put_task(task);
  1337. /*
  1338. * Do this after dropping the extra ref because if this was a requeue
  1339. * it's removed from that list and cleanup_queued_task would miss it.
  1340. */
  1341. if (test_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags)) {
  1342. /*
  1343. * Save the task and ref in case we weren't cleaning up this
  1344. * task and get woken up again.
  1345. */
  1346. conn->task = task;
  1347. return -ENODATA;
  1348. }
  1349. spin_unlock_bh(&conn->session->frwd_lock);
  1350. rc = conn->session->tt->xmit_task(task);
  1351. spin_lock_bh(&conn->session->frwd_lock);
  1352. if (!rc) {
  1353. /* done with this task */
  1354. task->last_xfer = jiffies;
  1355. } else {
  1356. /*
  1357. * get an extra ref that is released next time we access it
  1358. * as conn->task above.
  1359. */
  1360. iscsi_get_task(task);
  1361. conn->task = task;
  1362. }
  1363. iscsi_put_task(task);
  1364. return rc;
  1365. }
  1366. /**
  1367. * iscsi_requeue_task - requeue task to run from session workqueue
  1368. * @task: task to requeue
  1369. *
  1370. * Callers must have taken a ref to the task that is going to be requeued.
  1371. */
  1372. void iscsi_requeue_task(struct iscsi_task *task)
  1373. {
  1374. struct iscsi_conn *conn = task->conn;
  1375. /*
  1376. * this may be on the requeue list already if the xmit_task callout
  1377. * is handling the r2ts while we are adding new ones
  1378. */
  1379. spin_lock_bh(&conn->session->frwd_lock);
  1380. if (list_empty(&task->running)) {
  1381. list_add_tail(&task->running, &conn->requeue);
  1382. } else {
  1383. /*
  1384. * Don't need the extra ref since it's already requeued and
  1385. * has a ref.
  1386. */
  1387. iscsi_put_task(task);
  1388. }
  1389. iscsi_conn_queue_xmit(conn);
  1390. spin_unlock_bh(&conn->session->frwd_lock);
  1391. }
  1392. EXPORT_SYMBOL_GPL(iscsi_requeue_task);
  1393. /**
  1394. * iscsi_data_xmit - xmit any command into the scheduled connection
  1395. * @conn: iscsi connection
  1396. *
  1397. * Notes:
  1398. * The function can return -EAGAIN in which case the caller must
  1399. * re-schedule it again later or recover. '0' return code means
  1400. * successful xmit.
  1401. **/
  1402. static int iscsi_data_xmit(struct iscsi_conn *conn)
  1403. {
  1404. struct iscsi_task *task;
  1405. int rc = 0;
  1406. spin_lock_bh(&conn->session->frwd_lock);
  1407. if (test_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags)) {
  1408. ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
  1409. spin_unlock_bh(&conn->session->frwd_lock);
  1410. return -ENODATA;
  1411. }
  1412. if (conn->task) {
  1413. rc = iscsi_xmit_task(conn, conn->task, false);
  1414. if (rc)
  1415. goto done;
  1416. }
  1417. /*
  1418. * process mgmt pdus like nops before commands since we should
  1419. * only have one nop-out as a ping from us and targets should not
  1420. * overflow us with nop-ins
  1421. */
  1422. check_mgmt:
  1423. while (!list_empty(&conn->mgmtqueue)) {
  1424. task = list_entry(conn->mgmtqueue.next, struct iscsi_task,
  1425. running);
  1426. list_del_init(&task->running);
  1427. if (iscsi_prep_mgmt_task(conn, task)) {
  1428. /* regular RX path uses back_lock */
  1429. spin_lock_bh(&conn->session->back_lock);
  1430. __iscsi_put_task(task);
  1431. spin_unlock_bh(&conn->session->back_lock);
  1432. continue;
  1433. }
  1434. rc = iscsi_xmit_task(conn, task, false);
  1435. if (rc)
  1436. goto done;
  1437. }
  1438. check_requeue:
  1439. while (!list_empty(&conn->requeue)) {
  1440. /*
  1441. * we always do fastlogout - conn stop code will clean up.
  1442. */
  1443. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  1444. break;
  1445. task = list_entry(conn->requeue.next, struct iscsi_task,
  1446. running);
  1447. if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
  1448. break;
  1449. list_del_init(&task->running);
  1450. rc = iscsi_xmit_task(conn, task, true);
  1451. if (rc)
  1452. goto done;
  1453. if (!list_empty(&conn->mgmtqueue))
  1454. goto check_mgmt;
  1455. }
  1456. /* process pending command queue */
  1457. while (!list_empty(&conn->cmdqueue)) {
  1458. task = list_entry(conn->cmdqueue.next, struct iscsi_task,
  1459. running);
  1460. list_del_init(&task->running);
  1461. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  1462. fail_scsi_task(task, DID_IMM_RETRY);
  1463. continue;
  1464. }
  1465. rc = iscsi_prep_scsi_cmd_pdu(task);
  1466. if (rc) {
  1467. if (rc == -ENOMEM || rc == -EACCES)
  1468. fail_scsi_task(task, DID_IMM_RETRY);
  1469. else
  1470. fail_scsi_task(task, DID_ABORT);
  1471. continue;
  1472. }
  1473. rc = iscsi_xmit_task(conn, task, false);
  1474. if (rc)
  1475. goto done;
  1476. /*
  1477. * we could continuously get new task requests so
  1478. * we need to check the mgmt queue for nops that need to
  1479. * be sent to aviod starvation
  1480. */
  1481. if (!list_empty(&conn->mgmtqueue))
  1482. goto check_mgmt;
  1483. if (!list_empty(&conn->requeue))
  1484. goto check_requeue;
  1485. }
  1486. spin_unlock_bh(&conn->session->frwd_lock);
  1487. return -ENODATA;
  1488. done:
  1489. spin_unlock_bh(&conn->session->frwd_lock);
  1490. return rc;
  1491. }
  1492. static void iscsi_xmitworker(struct work_struct *work)
  1493. {
  1494. struct iscsi_conn *conn =
  1495. container_of(work, struct iscsi_conn, xmitwork);
  1496. int rc;
  1497. /*
  1498. * serialize Xmit worker on a per-connection basis.
  1499. */
  1500. do {
  1501. rc = iscsi_data_xmit(conn);
  1502. } while (rc >= 0 || rc == -EAGAIN);
  1503. }
  1504. static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
  1505. struct scsi_cmnd *sc)
  1506. {
  1507. struct iscsi_task *task;
  1508. if (!kfifo_out(&conn->session->cmdpool.queue,
  1509. (void *) &task, sizeof(void *)))
  1510. return NULL;
  1511. iscsi_cmd(sc)->age = conn->session->age;
  1512. iscsi_cmd(sc)->task = task;
  1513. refcount_set(&task->refcount, 1);
  1514. task->state = ISCSI_TASK_PENDING;
  1515. task->conn = conn;
  1516. task->sc = sc;
  1517. task->have_checked_conn = false;
  1518. task->last_timeout = jiffies;
  1519. task->last_xfer = jiffies;
  1520. task->protected = false;
  1521. INIT_LIST_HEAD(&task->running);
  1522. return task;
  1523. }
  1524. enum {
  1525. FAILURE_BAD_HOST = 1,
  1526. FAILURE_SESSION_FAILED,
  1527. FAILURE_SESSION_FREED,
  1528. FAILURE_WINDOW_CLOSED,
  1529. FAILURE_OOM,
  1530. FAILURE_SESSION_TERMINATE,
  1531. FAILURE_SESSION_IN_RECOVERY,
  1532. FAILURE_SESSION_RECOVERY_TIMEOUT,
  1533. FAILURE_SESSION_LOGGING_OUT,
  1534. FAILURE_SESSION_NOT_READY,
  1535. };
  1536. int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
  1537. {
  1538. struct iscsi_cls_session *cls_session;
  1539. struct iscsi_host *ihost;
  1540. int reason = 0;
  1541. struct iscsi_session *session;
  1542. struct iscsi_conn *conn;
  1543. struct iscsi_task *task = NULL;
  1544. sc->result = 0;
  1545. iscsi_cmd(sc)->task = NULL;
  1546. ihost = shost_priv(host);
  1547. cls_session = starget_to_session(scsi_target(sc->device));
  1548. session = cls_session->dd_data;
  1549. spin_lock_bh(&session->frwd_lock);
  1550. reason = iscsi_session_chkready(cls_session);
  1551. if (reason) {
  1552. sc->result = reason;
  1553. goto fault;
  1554. }
  1555. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1556. /*
  1557. * to handle the race between when we set the recovery state
  1558. * and block the session we requeue here (commands could
  1559. * be entering our queuecommand while a block is starting
  1560. * up because the block code is not locked)
  1561. */
  1562. switch (session->state) {
  1563. case ISCSI_STATE_FAILED:
  1564. /*
  1565. * cmds should fail during shutdown, if the session
  1566. * state is bad, allowing completion to happen
  1567. */
  1568. if (unlikely(system_state != SYSTEM_RUNNING)) {
  1569. reason = FAILURE_SESSION_FAILED;
  1570. sc->result = DID_NO_CONNECT << 16;
  1571. break;
  1572. }
  1573. fallthrough;
  1574. case ISCSI_STATE_IN_RECOVERY:
  1575. reason = FAILURE_SESSION_IN_RECOVERY;
  1576. sc->result = DID_IMM_RETRY << 16;
  1577. break;
  1578. case ISCSI_STATE_LOGGING_OUT:
  1579. reason = FAILURE_SESSION_LOGGING_OUT;
  1580. sc->result = DID_IMM_RETRY << 16;
  1581. break;
  1582. case ISCSI_STATE_RECOVERY_FAILED:
  1583. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  1584. sc->result = DID_TRANSPORT_FAILFAST << 16;
  1585. break;
  1586. case ISCSI_STATE_TERMINATE:
  1587. reason = FAILURE_SESSION_TERMINATE;
  1588. sc->result = DID_NO_CONNECT << 16;
  1589. break;
  1590. default:
  1591. reason = FAILURE_SESSION_FREED;
  1592. sc->result = DID_NO_CONNECT << 16;
  1593. }
  1594. goto fault;
  1595. }
  1596. conn = session->leadconn;
  1597. if (!conn) {
  1598. reason = FAILURE_SESSION_FREED;
  1599. sc->result = DID_NO_CONNECT << 16;
  1600. goto fault;
  1601. }
  1602. if (test_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags)) {
  1603. reason = FAILURE_SESSION_IN_RECOVERY;
  1604. sc->result = DID_REQUEUE << 16;
  1605. goto fault;
  1606. }
  1607. if (iscsi_check_cmdsn_window_closed(conn)) {
  1608. reason = FAILURE_WINDOW_CLOSED;
  1609. goto reject;
  1610. }
  1611. task = iscsi_alloc_task(conn, sc);
  1612. if (!task) {
  1613. reason = FAILURE_OOM;
  1614. goto reject;
  1615. }
  1616. if (!ihost->workq) {
  1617. reason = iscsi_prep_scsi_cmd_pdu(task);
  1618. if (reason) {
  1619. if (reason == -ENOMEM || reason == -EACCES) {
  1620. reason = FAILURE_OOM;
  1621. goto prepd_reject;
  1622. } else {
  1623. sc->result = DID_ABORT << 16;
  1624. goto prepd_fault;
  1625. }
  1626. }
  1627. if (session->tt->xmit_task(task)) {
  1628. session->cmdsn--;
  1629. reason = FAILURE_SESSION_NOT_READY;
  1630. goto prepd_reject;
  1631. }
  1632. } else {
  1633. list_add_tail(&task->running, &conn->cmdqueue);
  1634. iscsi_conn_queue_xmit(conn);
  1635. }
  1636. session->queued_cmdsn++;
  1637. spin_unlock_bh(&session->frwd_lock);
  1638. return 0;
  1639. prepd_reject:
  1640. spin_lock_bh(&session->back_lock);
  1641. iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
  1642. spin_unlock_bh(&session->back_lock);
  1643. reject:
  1644. spin_unlock_bh(&session->frwd_lock);
  1645. ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
  1646. sc->cmnd[0], reason);
  1647. return SCSI_MLQUEUE_TARGET_BUSY;
  1648. prepd_fault:
  1649. spin_lock_bh(&session->back_lock);
  1650. iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
  1651. spin_unlock_bh(&session->back_lock);
  1652. fault:
  1653. spin_unlock_bh(&session->frwd_lock);
  1654. ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
  1655. sc->cmnd[0], reason);
  1656. scsi_set_resid(sc, scsi_bufflen(sc));
  1657. scsi_done(sc);
  1658. return 0;
  1659. }
  1660. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  1661. int iscsi_target_alloc(struct scsi_target *starget)
  1662. {
  1663. struct iscsi_cls_session *cls_session = starget_to_session(starget);
  1664. struct iscsi_session *session = cls_session->dd_data;
  1665. starget->can_queue = session->scsi_cmds_max;
  1666. return 0;
  1667. }
  1668. EXPORT_SYMBOL_GPL(iscsi_target_alloc);
  1669. static void iscsi_tmf_timedout(struct timer_list *t)
  1670. {
  1671. struct iscsi_session *session = from_timer(session, t, tmf_timer);
  1672. spin_lock(&session->frwd_lock);
  1673. if (session->tmf_state == TMF_QUEUED) {
  1674. session->tmf_state = TMF_TIMEDOUT;
  1675. ISCSI_DBG_EH(session, "tmf timedout\n");
  1676. /* unblock eh_abort() */
  1677. wake_up(&session->ehwait);
  1678. }
  1679. spin_unlock(&session->frwd_lock);
  1680. }
  1681. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  1682. struct iscsi_tm *hdr, int age,
  1683. int timeout)
  1684. __must_hold(&session->frwd_lock)
  1685. {
  1686. struct iscsi_session *session = conn->session;
  1687. if (__iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0)) {
  1688. spin_unlock_bh(&session->frwd_lock);
  1689. iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
  1690. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1691. spin_lock_bh(&session->frwd_lock);
  1692. return -EPERM;
  1693. }
  1694. conn->tmfcmd_pdus_cnt++;
  1695. session->tmf_timer.expires = timeout * HZ + jiffies;
  1696. add_timer(&session->tmf_timer);
  1697. ISCSI_DBG_EH(session, "tmf set timeout\n");
  1698. spin_unlock_bh(&session->frwd_lock);
  1699. mutex_unlock(&session->eh_mutex);
  1700. /*
  1701. * block eh thread until:
  1702. *
  1703. * 1) tmf response
  1704. * 2) tmf timeout
  1705. * 3) session is terminated or restarted or userspace has
  1706. * given up on recovery
  1707. */
  1708. wait_event_interruptible(session->ehwait, age != session->age ||
  1709. session->state != ISCSI_STATE_LOGGED_IN ||
  1710. session->tmf_state != TMF_QUEUED);
  1711. if (signal_pending(current))
  1712. flush_signals(current);
  1713. del_timer_sync(&session->tmf_timer);
  1714. mutex_lock(&session->eh_mutex);
  1715. spin_lock_bh(&session->frwd_lock);
  1716. /* if the session drops it will clean up the task */
  1717. if (age != session->age ||
  1718. session->state != ISCSI_STATE_LOGGED_IN)
  1719. return -ENOTCONN;
  1720. return 0;
  1721. }
  1722. /*
  1723. * Fail commands. session frwd lock held and xmit thread flushed.
  1724. */
  1725. static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error)
  1726. {
  1727. struct iscsi_session *session = conn->session;
  1728. struct iscsi_task *task;
  1729. int i;
  1730. restart_cmd_loop:
  1731. spin_lock_bh(&session->back_lock);
  1732. for (i = 0; i < session->cmds_max; i++) {
  1733. task = session->cmds[i];
  1734. if (!task->sc || task->state == ISCSI_TASK_FREE)
  1735. continue;
  1736. if (lun != -1 && lun != task->sc->device->lun)
  1737. continue;
  1738. /*
  1739. * The cmd is completing but if this is called from an eh
  1740. * callout path then when we return scsi-ml owns the cmd. Wait
  1741. * for the completion path to finish freeing the cmd.
  1742. */
  1743. if (!iscsi_get_task(task)) {
  1744. spin_unlock_bh(&session->back_lock);
  1745. spin_unlock_bh(&session->frwd_lock);
  1746. udelay(ISCSI_CMD_COMPL_WAIT);
  1747. spin_lock_bh(&session->frwd_lock);
  1748. goto restart_cmd_loop;
  1749. }
  1750. ISCSI_DBG_SESSION(session,
  1751. "failing sc %p itt 0x%x state %d\n",
  1752. task->sc, task->itt, task->state);
  1753. __fail_scsi_task(task, error);
  1754. __iscsi_put_task(task);
  1755. }
  1756. spin_unlock_bh(&session->back_lock);
  1757. }
  1758. /**
  1759. * iscsi_suspend_queue - suspend iscsi_queuecommand
  1760. * @conn: iscsi conn to stop queueing IO on
  1761. *
  1762. * This grabs the session frwd_lock to make sure no one is in
  1763. * xmit_task/queuecommand, and then sets suspend to prevent
  1764. * new commands from being queued. This only needs to be called
  1765. * by offload drivers that need to sync a path like ep disconnect
  1766. * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
  1767. * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
  1768. */
  1769. void iscsi_suspend_queue(struct iscsi_conn *conn)
  1770. {
  1771. spin_lock_bh(&conn->session->frwd_lock);
  1772. set_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags);
  1773. spin_unlock_bh(&conn->session->frwd_lock);
  1774. }
  1775. EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
  1776. /**
  1777. * iscsi_suspend_tx - suspend iscsi_data_xmit
  1778. * @conn: iscsi conn to stop processing IO on.
  1779. *
  1780. * This function sets the suspend bit to prevent iscsi_data_xmit
  1781. * from sending new IO, and if work is queued on the xmit thread
  1782. * it will wait for it to be completed.
  1783. */
  1784. void iscsi_suspend_tx(struct iscsi_conn *conn)
  1785. {
  1786. struct Scsi_Host *shost = conn->session->host;
  1787. struct iscsi_host *ihost = shost_priv(shost);
  1788. set_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags);
  1789. if (ihost->workq)
  1790. flush_work(&conn->xmitwork);
  1791. }
  1792. EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
  1793. static void iscsi_start_tx(struct iscsi_conn *conn)
  1794. {
  1795. clear_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags);
  1796. iscsi_conn_queue_xmit(conn);
  1797. }
  1798. /**
  1799. * iscsi_suspend_rx - Prevent recvwork from running again.
  1800. * @conn: iscsi conn to stop.
  1801. */
  1802. void iscsi_suspend_rx(struct iscsi_conn *conn)
  1803. {
  1804. struct Scsi_Host *shost = conn->session->host;
  1805. struct iscsi_host *ihost = shost_priv(shost);
  1806. set_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags);
  1807. if (ihost->workq)
  1808. flush_work(&conn->recvwork);
  1809. }
  1810. EXPORT_SYMBOL_GPL(iscsi_suspend_rx);
  1811. /*
  1812. * We want to make sure a ping is in flight. It has timed out.
  1813. * And we are not busy processing a pdu that is making
  1814. * progress but got started before the ping and is taking a while
  1815. * to complete so the ping is just stuck behind it in a queue.
  1816. */
  1817. static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
  1818. {
  1819. if (READ_ONCE(conn->ping_task) &&
  1820. time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
  1821. (conn->ping_timeout * HZ), jiffies))
  1822. return 1;
  1823. else
  1824. return 0;
  1825. }
  1826. enum scsi_timeout_action iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
  1827. {
  1828. enum scsi_timeout_action rc = SCSI_EH_NOT_HANDLED;
  1829. struct iscsi_task *task = NULL, *running_task;
  1830. struct iscsi_cls_session *cls_session;
  1831. struct iscsi_session *session;
  1832. struct iscsi_conn *conn;
  1833. int i;
  1834. cls_session = starget_to_session(scsi_target(sc->device));
  1835. session = cls_session->dd_data;
  1836. ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
  1837. spin_lock_bh(&session->frwd_lock);
  1838. spin_lock(&session->back_lock);
  1839. task = iscsi_cmd(sc)->task;
  1840. if (!task) {
  1841. /*
  1842. * Raced with completion. Blk layer has taken ownership
  1843. * so let timeout code complete it now.
  1844. */
  1845. rc = SCSI_EH_NOT_HANDLED;
  1846. spin_unlock(&session->back_lock);
  1847. goto done;
  1848. }
  1849. if (!iscsi_get_task(task)) {
  1850. /*
  1851. * Racing with the completion path right now, so give it more
  1852. * time so that path can complete it like normal.
  1853. */
  1854. rc = SCSI_EH_RESET_TIMER;
  1855. task = NULL;
  1856. spin_unlock(&session->back_lock);
  1857. goto done;
  1858. }
  1859. spin_unlock(&session->back_lock);
  1860. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1861. /*
  1862. * During shutdown, if session is prematurely disconnected,
  1863. * recovery won't happen and there will be hung cmds. Not
  1864. * handling cmds would trigger EH, also bad in this case.
  1865. * Instead, handle cmd, allow completion to happen and let
  1866. * upper layer to deal with the result.
  1867. */
  1868. if (unlikely(system_state != SYSTEM_RUNNING)) {
  1869. sc->result = DID_NO_CONNECT << 16;
  1870. ISCSI_DBG_EH(session, "sc on shutdown, handled\n");
  1871. rc = SCSI_EH_NOT_HANDLED;
  1872. goto done;
  1873. }
  1874. /*
  1875. * We are probably in the middle of iscsi recovery so let
  1876. * that complete and handle the error.
  1877. */
  1878. rc = SCSI_EH_RESET_TIMER;
  1879. goto done;
  1880. }
  1881. conn = session->leadconn;
  1882. if (!conn) {
  1883. /* In the middle of shuting down */
  1884. rc = SCSI_EH_RESET_TIMER;
  1885. goto done;
  1886. }
  1887. /*
  1888. * If we have sent (at least queued to the network layer) a pdu or
  1889. * recvd one for the task since the last timeout ask for
  1890. * more time. If on the next timeout we have not made progress
  1891. * we can check if it is the task or connection when we send the
  1892. * nop as a ping.
  1893. */
  1894. if (time_after(task->last_xfer, task->last_timeout)) {
  1895. ISCSI_DBG_EH(session, "Command making progress. Asking "
  1896. "scsi-ml for more time to complete. "
  1897. "Last data xfer at %lu. Last timeout was at "
  1898. "%lu\n.", task->last_xfer, task->last_timeout);
  1899. task->have_checked_conn = false;
  1900. rc = SCSI_EH_RESET_TIMER;
  1901. goto done;
  1902. }
  1903. if (!conn->recv_timeout && !conn->ping_timeout)
  1904. goto done;
  1905. /*
  1906. * if the ping timedout then we are in the middle of cleaning up
  1907. * and can let the iscsi eh handle it
  1908. */
  1909. if (iscsi_has_ping_timed_out(conn)) {
  1910. rc = SCSI_EH_RESET_TIMER;
  1911. goto done;
  1912. }
  1913. spin_lock(&session->back_lock);
  1914. for (i = 0; i < conn->session->cmds_max; i++) {
  1915. running_task = conn->session->cmds[i];
  1916. if (!running_task->sc || running_task == task ||
  1917. running_task->state != ISCSI_TASK_RUNNING)
  1918. continue;
  1919. /*
  1920. * Only check if cmds started before this one have made
  1921. * progress, or this could never fail
  1922. */
  1923. if (time_after(running_task->sc->jiffies_at_alloc,
  1924. task->sc->jiffies_at_alloc))
  1925. continue;
  1926. if (time_after(running_task->last_xfer, task->last_timeout)) {
  1927. /*
  1928. * This task has not made progress, but a task
  1929. * started before us has transferred data since
  1930. * we started/last-checked. We could be queueing
  1931. * too many tasks or the LU is bad.
  1932. *
  1933. * If the device is bad the cmds ahead of us on
  1934. * other devs will complete, and this loop will
  1935. * eventually fail starting the scsi eh.
  1936. */
  1937. ISCSI_DBG_EH(session, "Command has not made progress "
  1938. "but commands ahead of it have. "
  1939. "Asking scsi-ml for more time to "
  1940. "complete. Our last xfer vs running task "
  1941. "last xfer %lu/%lu. Last check %lu.\n",
  1942. task->last_xfer, running_task->last_xfer,
  1943. task->last_timeout);
  1944. spin_unlock(&session->back_lock);
  1945. rc = SCSI_EH_RESET_TIMER;
  1946. goto done;
  1947. }
  1948. }
  1949. spin_unlock(&session->back_lock);
  1950. /* Assumes nop timeout is shorter than scsi cmd timeout */
  1951. if (task->have_checked_conn)
  1952. goto done;
  1953. /*
  1954. * Checking the transport already or nop from a cmd timeout still
  1955. * running
  1956. */
  1957. if (READ_ONCE(conn->ping_task)) {
  1958. task->have_checked_conn = true;
  1959. rc = SCSI_EH_RESET_TIMER;
  1960. goto done;
  1961. }
  1962. /* Make sure there is a transport check done */
  1963. iscsi_send_nopout(conn, NULL);
  1964. task->have_checked_conn = true;
  1965. rc = SCSI_EH_RESET_TIMER;
  1966. done:
  1967. spin_unlock_bh(&session->frwd_lock);
  1968. if (task) {
  1969. task->last_timeout = jiffies;
  1970. iscsi_put_task(task);
  1971. }
  1972. ISCSI_DBG_EH(session, "return %s\n", rc == SCSI_EH_RESET_TIMER ?
  1973. "timer reset" : "shutdown or nh");
  1974. return rc;
  1975. }
  1976. EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out);
  1977. static void iscsi_check_transport_timeouts(struct timer_list *t)
  1978. {
  1979. struct iscsi_conn *conn = from_timer(conn, t, transport_timer);
  1980. struct iscsi_session *session = conn->session;
  1981. unsigned long recv_timeout, next_timeout = 0, last_recv;
  1982. spin_lock(&session->frwd_lock);
  1983. if (session->state != ISCSI_STATE_LOGGED_IN)
  1984. goto done;
  1985. recv_timeout = conn->recv_timeout;
  1986. if (!recv_timeout)
  1987. goto done;
  1988. recv_timeout *= HZ;
  1989. last_recv = conn->last_recv;
  1990. if (iscsi_has_ping_timed_out(conn)) {
  1991. iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
  1992. "expired, recv timeout %d, last rx %lu, "
  1993. "last ping %lu, now %lu\n",
  1994. conn->ping_timeout, conn->recv_timeout,
  1995. last_recv, conn->last_ping, jiffies);
  1996. spin_unlock(&session->frwd_lock);
  1997. iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT);
  1998. return;
  1999. }
  2000. if (time_before_eq(last_recv + recv_timeout, jiffies)) {
  2001. /* send a ping to try to provoke some traffic */
  2002. ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
  2003. if (iscsi_send_nopout(conn, NULL))
  2004. next_timeout = jiffies + (1 * HZ);
  2005. else
  2006. next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
  2007. } else
  2008. next_timeout = last_recv + recv_timeout;
  2009. ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
  2010. mod_timer(&conn->transport_timer, next_timeout);
  2011. done:
  2012. spin_unlock(&session->frwd_lock);
  2013. }
  2014. /**
  2015. * iscsi_conn_unbind - prevent queueing to conn.
  2016. * @cls_conn: iscsi conn ep is bound to.
  2017. * @is_active: is the conn in use for boot or is this for EH/termination
  2018. *
  2019. * This must be called by drivers implementing the ep_disconnect callout.
  2020. * It disables queueing to the connection from libiscsi in preparation for
  2021. * an ep_disconnect call.
  2022. */
  2023. void iscsi_conn_unbind(struct iscsi_cls_conn *cls_conn, bool is_active)
  2024. {
  2025. struct iscsi_session *session;
  2026. struct iscsi_conn *conn;
  2027. if (!cls_conn)
  2028. return;
  2029. conn = cls_conn->dd_data;
  2030. session = conn->session;
  2031. /*
  2032. * Wait for iscsi_eh calls to exit. We don't wait for the tmf to
  2033. * complete or timeout. The caller just wants to know what's running
  2034. * is everything that needs to be cleaned up, and no cmds will be
  2035. * queued.
  2036. */
  2037. mutex_lock(&session->eh_mutex);
  2038. iscsi_suspend_queue(conn);
  2039. iscsi_suspend_tx(conn);
  2040. spin_lock_bh(&session->frwd_lock);
  2041. clear_bit(ISCSI_CONN_FLAG_BOUND, &conn->flags);
  2042. if (!is_active) {
  2043. /*
  2044. * if logout timed out before userspace could even send a PDU
  2045. * the state might still be in ISCSI_STATE_LOGGED_IN and
  2046. * allowing new cmds and TMFs.
  2047. */
  2048. if (session->state == ISCSI_STATE_LOGGED_IN)
  2049. iscsi_set_conn_failed(conn);
  2050. }
  2051. spin_unlock_bh(&session->frwd_lock);
  2052. mutex_unlock(&session->eh_mutex);
  2053. }
  2054. EXPORT_SYMBOL_GPL(iscsi_conn_unbind);
  2055. static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
  2056. struct iscsi_tm *hdr)
  2057. {
  2058. memset(hdr, 0, sizeof(*hdr));
  2059. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  2060. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  2061. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  2062. hdr->lun = task->lun;
  2063. hdr->rtt = task->hdr_itt;
  2064. hdr->refcmdsn = task->cmdsn;
  2065. }
  2066. int iscsi_eh_abort(struct scsi_cmnd *sc)
  2067. {
  2068. struct iscsi_cls_session *cls_session;
  2069. struct iscsi_session *session;
  2070. struct iscsi_conn *conn;
  2071. struct iscsi_task *task;
  2072. struct iscsi_tm *hdr;
  2073. int age;
  2074. cls_session = starget_to_session(scsi_target(sc->device));
  2075. session = cls_session->dd_data;
  2076. ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
  2077. completion_check:
  2078. mutex_lock(&session->eh_mutex);
  2079. spin_lock_bh(&session->frwd_lock);
  2080. /*
  2081. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  2082. * got the command.
  2083. */
  2084. if (!iscsi_cmd(sc)->task) {
  2085. ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
  2086. "it completed.\n");
  2087. spin_unlock_bh(&session->frwd_lock);
  2088. mutex_unlock(&session->eh_mutex);
  2089. return SUCCESS;
  2090. }
  2091. /*
  2092. * If we are not logged in or we have started a new session
  2093. * then let the host reset code handle this
  2094. */
  2095. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  2096. iscsi_cmd(sc)->age != session->age) {
  2097. spin_unlock_bh(&session->frwd_lock);
  2098. mutex_unlock(&session->eh_mutex);
  2099. ISCSI_DBG_EH(session, "failing abort due to dropped "
  2100. "session.\n");
  2101. return FAILED;
  2102. }
  2103. spin_lock(&session->back_lock);
  2104. task = iscsi_cmd(sc)->task;
  2105. if (!task || !task->sc) {
  2106. /* task completed before time out */
  2107. ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
  2108. spin_unlock(&session->back_lock);
  2109. spin_unlock_bh(&session->frwd_lock);
  2110. mutex_unlock(&session->eh_mutex);
  2111. return SUCCESS;
  2112. }
  2113. if (!iscsi_get_task(task)) {
  2114. spin_unlock(&session->back_lock);
  2115. spin_unlock_bh(&session->frwd_lock);
  2116. mutex_unlock(&session->eh_mutex);
  2117. /* We are just about to call iscsi_free_task so wait for it. */
  2118. udelay(ISCSI_CMD_COMPL_WAIT);
  2119. goto completion_check;
  2120. }
  2121. ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n", sc, task->itt);
  2122. conn = session->leadconn;
  2123. iscsi_get_conn(conn->cls_conn);
  2124. conn->eh_abort_cnt++;
  2125. age = session->age;
  2126. spin_unlock(&session->back_lock);
  2127. if (task->state == ISCSI_TASK_PENDING) {
  2128. fail_scsi_task(task, DID_ABORT);
  2129. goto success;
  2130. }
  2131. /* only have one tmf outstanding at a time */
  2132. if (session->tmf_state != TMF_INITIAL)
  2133. goto failed;
  2134. session->tmf_state = TMF_QUEUED;
  2135. hdr = &session->tmhdr;
  2136. iscsi_prep_abort_task_pdu(task, hdr);
  2137. if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout))
  2138. goto failed;
  2139. switch (session->tmf_state) {
  2140. case TMF_SUCCESS:
  2141. spin_unlock_bh(&session->frwd_lock);
  2142. /*
  2143. * stop tx side incase the target had sent a abort rsp but
  2144. * the initiator was still writing out data.
  2145. */
  2146. iscsi_suspend_tx(conn);
  2147. /*
  2148. * we do not stop the recv side because targets have been
  2149. * good and have never sent us a successful tmf response
  2150. * then sent more data for the cmd.
  2151. */
  2152. spin_lock_bh(&session->frwd_lock);
  2153. fail_scsi_task(task, DID_ABORT);
  2154. session->tmf_state = TMF_INITIAL;
  2155. memset(hdr, 0, sizeof(*hdr));
  2156. spin_unlock_bh(&session->frwd_lock);
  2157. iscsi_start_tx(conn);
  2158. goto success_unlocked;
  2159. case TMF_TIMEDOUT:
  2160. session->running_aborted_task = task;
  2161. spin_unlock_bh(&session->frwd_lock);
  2162. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2163. goto failed_unlocked;
  2164. case TMF_NOT_FOUND:
  2165. if (iscsi_task_is_completed(task)) {
  2166. session->tmf_state = TMF_INITIAL;
  2167. memset(hdr, 0, sizeof(*hdr));
  2168. /* task completed before tmf abort response */
  2169. ISCSI_DBG_EH(session, "sc completed while abort in "
  2170. "progress\n");
  2171. goto success;
  2172. }
  2173. fallthrough;
  2174. default:
  2175. session->tmf_state = TMF_INITIAL;
  2176. goto failed;
  2177. }
  2178. success:
  2179. spin_unlock_bh(&session->frwd_lock);
  2180. success_unlocked:
  2181. ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
  2182. sc, task->itt);
  2183. iscsi_put_task(task);
  2184. iscsi_put_conn(conn->cls_conn);
  2185. mutex_unlock(&session->eh_mutex);
  2186. return SUCCESS;
  2187. failed:
  2188. spin_unlock_bh(&session->frwd_lock);
  2189. failed_unlocked:
  2190. ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
  2191. task ? task->itt : 0);
  2192. /*
  2193. * The driver might be accessing the task so hold the ref. The conn
  2194. * stop cleanup will drop the ref after ep_disconnect so we know the
  2195. * driver's no longer touching the task.
  2196. */
  2197. if (!session->running_aborted_task)
  2198. iscsi_put_task(task);
  2199. iscsi_put_conn(conn->cls_conn);
  2200. mutex_unlock(&session->eh_mutex);
  2201. return FAILED;
  2202. }
  2203. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  2204. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  2205. {
  2206. memset(hdr, 0, sizeof(*hdr));
  2207. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  2208. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  2209. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  2210. int_to_scsilun(sc->device->lun, &hdr->lun);
  2211. hdr->rtt = RESERVED_ITT;
  2212. }
  2213. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  2214. {
  2215. struct iscsi_cls_session *cls_session;
  2216. struct iscsi_session *session;
  2217. struct iscsi_conn *conn;
  2218. struct iscsi_tm *hdr;
  2219. int rc = FAILED;
  2220. cls_session = starget_to_session(scsi_target(sc->device));
  2221. session = cls_session->dd_data;
  2222. ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc,
  2223. sc->device->lun);
  2224. mutex_lock(&session->eh_mutex);
  2225. spin_lock_bh(&session->frwd_lock);
  2226. /*
  2227. * Just check if we are not logged in. We cannot check for
  2228. * the phase because the reset could come from a ioctl.
  2229. */
  2230. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  2231. goto unlock;
  2232. conn = session->leadconn;
  2233. /* only have one tmf outstanding at a time */
  2234. if (session->tmf_state != TMF_INITIAL)
  2235. goto unlock;
  2236. session->tmf_state = TMF_QUEUED;
  2237. hdr = &session->tmhdr;
  2238. iscsi_prep_lun_reset_pdu(sc, hdr);
  2239. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  2240. session->lu_reset_timeout)) {
  2241. rc = FAILED;
  2242. goto unlock;
  2243. }
  2244. switch (session->tmf_state) {
  2245. case TMF_SUCCESS:
  2246. break;
  2247. case TMF_TIMEDOUT:
  2248. spin_unlock_bh(&session->frwd_lock);
  2249. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2250. goto done;
  2251. default:
  2252. session->tmf_state = TMF_INITIAL;
  2253. goto unlock;
  2254. }
  2255. rc = SUCCESS;
  2256. spin_unlock_bh(&session->frwd_lock);
  2257. iscsi_suspend_tx(conn);
  2258. spin_lock_bh(&session->frwd_lock);
  2259. memset(hdr, 0, sizeof(*hdr));
  2260. fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
  2261. session->tmf_state = TMF_INITIAL;
  2262. spin_unlock_bh(&session->frwd_lock);
  2263. iscsi_start_tx(conn);
  2264. goto done;
  2265. unlock:
  2266. spin_unlock_bh(&session->frwd_lock);
  2267. done:
  2268. ISCSI_DBG_EH(session, "dev reset result = %s\n",
  2269. rc == SUCCESS ? "SUCCESS" : "FAILED");
  2270. mutex_unlock(&session->eh_mutex);
  2271. return rc;
  2272. }
  2273. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  2274. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  2275. {
  2276. struct iscsi_session *session = cls_session->dd_data;
  2277. spin_lock_bh(&session->frwd_lock);
  2278. if (session->state != ISCSI_STATE_LOGGED_IN) {
  2279. session->state = ISCSI_STATE_RECOVERY_FAILED;
  2280. wake_up(&session->ehwait);
  2281. }
  2282. spin_unlock_bh(&session->frwd_lock);
  2283. }
  2284. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  2285. /**
  2286. * iscsi_eh_session_reset - drop session and attempt relogin
  2287. * @sc: scsi command
  2288. *
  2289. * This function will wait for a relogin, session termination from
  2290. * userspace, or a recovery/replacement timeout.
  2291. */
  2292. int iscsi_eh_session_reset(struct scsi_cmnd *sc)
  2293. {
  2294. struct iscsi_cls_session *cls_session;
  2295. struct iscsi_session *session;
  2296. struct iscsi_conn *conn;
  2297. cls_session = starget_to_session(scsi_target(sc->device));
  2298. session = cls_session->dd_data;
  2299. mutex_lock(&session->eh_mutex);
  2300. spin_lock_bh(&session->frwd_lock);
  2301. if (session->state == ISCSI_STATE_TERMINATE) {
  2302. failed:
  2303. ISCSI_DBG_EH(session,
  2304. "failing session reset: Could not log back into "
  2305. "%s [age %d]\n", session->targetname,
  2306. session->age);
  2307. spin_unlock_bh(&session->frwd_lock);
  2308. mutex_unlock(&session->eh_mutex);
  2309. return FAILED;
  2310. }
  2311. conn = session->leadconn;
  2312. iscsi_get_conn(conn->cls_conn);
  2313. spin_unlock_bh(&session->frwd_lock);
  2314. mutex_unlock(&session->eh_mutex);
  2315. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2316. iscsi_put_conn(conn->cls_conn);
  2317. ISCSI_DBG_EH(session, "wait for relogin\n");
  2318. wait_event_interruptible(session->ehwait,
  2319. session->state == ISCSI_STATE_TERMINATE ||
  2320. session->state == ISCSI_STATE_LOGGED_IN ||
  2321. session->state == ISCSI_STATE_RECOVERY_FAILED);
  2322. if (signal_pending(current))
  2323. flush_signals(current);
  2324. mutex_lock(&session->eh_mutex);
  2325. spin_lock_bh(&session->frwd_lock);
  2326. if (session->state == ISCSI_STATE_LOGGED_IN) {
  2327. ISCSI_DBG_EH(session,
  2328. "session reset succeeded for %s,%s\n",
  2329. session->targetname, conn->persistent_address);
  2330. } else
  2331. goto failed;
  2332. spin_unlock_bh(&session->frwd_lock);
  2333. mutex_unlock(&session->eh_mutex);
  2334. return SUCCESS;
  2335. }
  2336. EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
  2337. static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  2338. {
  2339. memset(hdr, 0, sizeof(*hdr));
  2340. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  2341. hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  2342. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  2343. hdr->rtt = RESERVED_ITT;
  2344. }
  2345. /**
  2346. * iscsi_eh_target_reset - reset target
  2347. * @sc: scsi command
  2348. *
  2349. * This will attempt to send a warm target reset.
  2350. */
  2351. static int iscsi_eh_target_reset(struct scsi_cmnd *sc)
  2352. {
  2353. struct iscsi_cls_session *cls_session;
  2354. struct iscsi_session *session;
  2355. struct iscsi_conn *conn;
  2356. struct iscsi_tm *hdr;
  2357. int rc = FAILED;
  2358. cls_session = starget_to_session(scsi_target(sc->device));
  2359. session = cls_session->dd_data;
  2360. ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
  2361. session->targetname);
  2362. mutex_lock(&session->eh_mutex);
  2363. spin_lock_bh(&session->frwd_lock);
  2364. /*
  2365. * Just check if we are not logged in. We cannot check for
  2366. * the phase because the reset could come from a ioctl.
  2367. */
  2368. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  2369. goto unlock;
  2370. conn = session->leadconn;
  2371. /* only have one tmf outstanding at a time */
  2372. if (session->tmf_state != TMF_INITIAL)
  2373. goto unlock;
  2374. session->tmf_state = TMF_QUEUED;
  2375. hdr = &session->tmhdr;
  2376. iscsi_prep_tgt_reset_pdu(sc, hdr);
  2377. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  2378. session->tgt_reset_timeout)) {
  2379. rc = FAILED;
  2380. goto unlock;
  2381. }
  2382. switch (session->tmf_state) {
  2383. case TMF_SUCCESS:
  2384. break;
  2385. case TMF_TIMEDOUT:
  2386. spin_unlock_bh(&session->frwd_lock);
  2387. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2388. goto done;
  2389. default:
  2390. session->tmf_state = TMF_INITIAL;
  2391. goto unlock;
  2392. }
  2393. rc = SUCCESS;
  2394. spin_unlock_bh(&session->frwd_lock);
  2395. iscsi_suspend_tx(conn);
  2396. spin_lock_bh(&session->frwd_lock);
  2397. memset(hdr, 0, sizeof(*hdr));
  2398. fail_scsi_tasks(conn, -1, DID_ERROR);
  2399. session->tmf_state = TMF_INITIAL;
  2400. spin_unlock_bh(&session->frwd_lock);
  2401. iscsi_start_tx(conn);
  2402. goto done;
  2403. unlock:
  2404. spin_unlock_bh(&session->frwd_lock);
  2405. done:
  2406. ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
  2407. rc == SUCCESS ? "SUCCESS" : "FAILED");
  2408. mutex_unlock(&session->eh_mutex);
  2409. return rc;
  2410. }
  2411. /**
  2412. * iscsi_eh_recover_target - reset target and possibly the session
  2413. * @sc: scsi command
  2414. *
  2415. * This will attempt to send a warm target reset. If that fails,
  2416. * we will escalate to ERL0 session recovery.
  2417. */
  2418. int iscsi_eh_recover_target(struct scsi_cmnd *sc)
  2419. {
  2420. int rc;
  2421. rc = iscsi_eh_target_reset(sc);
  2422. if (rc == FAILED)
  2423. rc = iscsi_eh_session_reset(sc);
  2424. return rc;
  2425. }
  2426. EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
  2427. /*
  2428. * Pre-allocate a pool of @max items of @item_size. By default, the pool
  2429. * should be accessed via kfifo_{get,put} on q->queue.
  2430. * Optionally, the caller can obtain the array of object pointers
  2431. * by passing in a non-NULL @items pointer
  2432. */
  2433. int
  2434. iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
  2435. {
  2436. int i, num_arrays = 1;
  2437. memset(q, 0, sizeof(*q));
  2438. q->max = max;
  2439. /* If the user passed an items pointer, he wants a copy of
  2440. * the array. */
  2441. if (items)
  2442. num_arrays++;
  2443. q->pool = kvcalloc(num_arrays * max, sizeof(void *), GFP_KERNEL);
  2444. if (q->pool == NULL)
  2445. return -ENOMEM;
  2446. kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
  2447. for (i = 0; i < max; i++) {
  2448. q->pool[i] = kzalloc(item_size, GFP_KERNEL);
  2449. if (q->pool[i] == NULL) {
  2450. q->max = i;
  2451. goto enomem;
  2452. }
  2453. kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
  2454. }
  2455. if (items) {
  2456. *items = q->pool + max;
  2457. memcpy(*items, q->pool, max * sizeof(void *));
  2458. }
  2459. return 0;
  2460. enomem:
  2461. iscsi_pool_free(q);
  2462. return -ENOMEM;
  2463. }
  2464. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  2465. void iscsi_pool_free(struct iscsi_pool *q)
  2466. {
  2467. int i;
  2468. for (i = 0; i < q->max; i++)
  2469. kfree(q->pool[i]);
  2470. kvfree(q->pool);
  2471. }
  2472. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  2473. int iscsi_host_get_max_scsi_cmds(struct Scsi_Host *shost,
  2474. uint16_t requested_cmds_max)
  2475. {
  2476. int scsi_cmds, total_cmds = requested_cmds_max;
  2477. check:
  2478. if (!total_cmds)
  2479. total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
  2480. /*
  2481. * The iscsi layer needs some tasks for nop handling and tmfs,
  2482. * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
  2483. * + 1 command for scsi IO.
  2484. */
  2485. if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
  2486. printk(KERN_ERR "iscsi: invalid max cmds of %d. Must be a power of two that is at least %d.\n",
  2487. total_cmds, ISCSI_TOTAL_CMDS_MIN);
  2488. return -EINVAL;
  2489. }
  2490. if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
  2491. printk(KERN_INFO "iscsi: invalid max cmds of %d. Must be a power of 2 less than or equal to %d. Using %d.\n",
  2492. requested_cmds_max, ISCSI_TOTAL_CMDS_MAX,
  2493. ISCSI_TOTAL_CMDS_MAX);
  2494. total_cmds = ISCSI_TOTAL_CMDS_MAX;
  2495. }
  2496. if (!is_power_of_2(total_cmds)) {
  2497. total_cmds = rounddown_pow_of_two(total_cmds);
  2498. if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
  2499. printk(KERN_ERR "iscsi: invalid max cmds of %d. Must be a power of 2 greater than %d.\n", requested_cmds_max, ISCSI_TOTAL_CMDS_MIN);
  2500. return -EINVAL;
  2501. }
  2502. printk(KERN_INFO "iscsi: invalid max cmds %d. Must be a power of 2. Rounding max cmds down to %d.\n",
  2503. requested_cmds_max, total_cmds);
  2504. }
  2505. scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
  2506. if (shost->can_queue && scsi_cmds > shost->can_queue) {
  2507. total_cmds = shost->can_queue;
  2508. printk(KERN_INFO "iscsi: requested max cmds %u is higher than driver limit. Using driver limit %u\n",
  2509. requested_cmds_max, shost->can_queue);
  2510. goto check;
  2511. }
  2512. return scsi_cmds;
  2513. }
  2514. EXPORT_SYMBOL_GPL(iscsi_host_get_max_scsi_cmds);
  2515. /**
  2516. * iscsi_host_add - add host to system
  2517. * @shost: scsi host
  2518. * @pdev: parent device
  2519. *
  2520. * This should be called by partial offload and software iscsi drivers
  2521. * to add a host to the system.
  2522. */
  2523. int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
  2524. {
  2525. if (!shost->can_queue)
  2526. shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
  2527. if (!shost->cmd_per_lun)
  2528. shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
  2529. return scsi_add_host(shost, pdev);
  2530. }
  2531. EXPORT_SYMBOL_GPL(iscsi_host_add);
  2532. /**
  2533. * iscsi_host_alloc - allocate a host and driver data
  2534. * @sht: scsi host template
  2535. * @dd_data_size: driver host data size
  2536. * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
  2537. *
  2538. * This should be called by partial offload and software iscsi drivers.
  2539. * To access the driver specific memory use the iscsi_host_priv() macro.
  2540. */
  2541. struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
  2542. int dd_data_size, bool xmit_can_sleep)
  2543. {
  2544. struct Scsi_Host *shost;
  2545. struct iscsi_host *ihost;
  2546. shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
  2547. if (!shost)
  2548. return NULL;
  2549. ihost = shost_priv(shost);
  2550. if (xmit_can_sleep) {
  2551. ihost->workq = alloc_workqueue("iscsi_q_%d",
  2552. WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND,
  2553. 1, shost->host_no);
  2554. if (!ihost->workq)
  2555. goto free_host;
  2556. }
  2557. spin_lock_init(&ihost->lock);
  2558. ihost->state = ISCSI_HOST_SETUP;
  2559. ihost->num_sessions = 0;
  2560. init_waitqueue_head(&ihost->session_removal_wq);
  2561. return shost;
  2562. free_host:
  2563. scsi_host_put(shost);
  2564. return NULL;
  2565. }
  2566. EXPORT_SYMBOL_GPL(iscsi_host_alloc);
  2567. static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
  2568. {
  2569. iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
  2570. }
  2571. /**
  2572. * iscsi_host_remove - remove host and sessions
  2573. * @shost: scsi host
  2574. * @is_shutdown: true if called from a driver shutdown callout
  2575. *
  2576. * If there are any sessions left, this will initiate the removal and wait
  2577. * for the completion.
  2578. */
  2579. void iscsi_host_remove(struct Scsi_Host *shost, bool is_shutdown)
  2580. {
  2581. struct iscsi_host *ihost = shost_priv(shost);
  2582. unsigned long flags;
  2583. spin_lock_irqsave(&ihost->lock, flags);
  2584. ihost->state = ISCSI_HOST_REMOVED;
  2585. spin_unlock_irqrestore(&ihost->lock, flags);
  2586. if (!is_shutdown)
  2587. iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
  2588. else
  2589. iscsi_host_for_each_session(shost, iscsi_force_destroy_session);
  2590. wait_event_interruptible(ihost->session_removal_wq,
  2591. ihost->num_sessions == 0);
  2592. if (signal_pending(current))
  2593. flush_signals(current);
  2594. scsi_remove_host(shost);
  2595. }
  2596. EXPORT_SYMBOL_GPL(iscsi_host_remove);
  2597. void iscsi_host_free(struct Scsi_Host *shost)
  2598. {
  2599. struct iscsi_host *ihost = shost_priv(shost);
  2600. if (ihost->workq)
  2601. destroy_workqueue(ihost->workq);
  2602. kfree(ihost->netdev);
  2603. kfree(ihost->hwaddress);
  2604. kfree(ihost->initiatorname);
  2605. scsi_host_put(shost);
  2606. }
  2607. EXPORT_SYMBOL_GPL(iscsi_host_free);
  2608. static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
  2609. {
  2610. struct iscsi_host *ihost = shost_priv(shost);
  2611. unsigned long flags;
  2612. shost = scsi_host_get(shost);
  2613. if (!shost) {
  2614. printk(KERN_ERR "Invalid state. Cannot notify host removal "
  2615. "of session teardown event because host already "
  2616. "removed.\n");
  2617. return;
  2618. }
  2619. spin_lock_irqsave(&ihost->lock, flags);
  2620. ihost->num_sessions--;
  2621. if (ihost->num_sessions == 0)
  2622. wake_up(&ihost->session_removal_wq);
  2623. spin_unlock_irqrestore(&ihost->lock, flags);
  2624. scsi_host_put(shost);
  2625. }
  2626. /**
  2627. * iscsi_session_setup - create iscsi cls session and host and session
  2628. * @iscsit: iscsi transport template
  2629. * @shost: scsi host
  2630. * @cmds_max: session can queue
  2631. * @dd_size: private driver data size, added to session allocation size
  2632. * @cmd_task_size: LLD task private data size
  2633. * @initial_cmdsn: initial CmdSN
  2634. * @id: target ID to add to this session
  2635. *
  2636. * This can be used by software iscsi_transports that allocate
  2637. * a session per scsi host.
  2638. *
  2639. * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
  2640. * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
  2641. * for nop handling and login/logout requests.
  2642. */
  2643. struct iscsi_cls_session *
  2644. iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
  2645. uint16_t cmds_max, int dd_size, int cmd_task_size,
  2646. uint32_t initial_cmdsn, unsigned int id)
  2647. {
  2648. struct iscsi_host *ihost = shost_priv(shost);
  2649. struct iscsi_session *session;
  2650. struct iscsi_cls_session *cls_session;
  2651. int cmd_i, scsi_cmds;
  2652. unsigned long flags;
  2653. spin_lock_irqsave(&ihost->lock, flags);
  2654. if (ihost->state == ISCSI_HOST_REMOVED) {
  2655. spin_unlock_irqrestore(&ihost->lock, flags);
  2656. return NULL;
  2657. }
  2658. ihost->num_sessions++;
  2659. spin_unlock_irqrestore(&ihost->lock, flags);
  2660. scsi_cmds = iscsi_host_get_max_scsi_cmds(shost, cmds_max);
  2661. if (scsi_cmds < 0)
  2662. goto dec_session_count;
  2663. cls_session = iscsi_alloc_session(shost, iscsit,
  2664. sizeof(struct iscsi_session) +
  2665. dd_size);
  2666. if (!cls_session)
  2667. goto dec_session_count;
  2668. session = cls_session->dd_data;
  2669. session->cls_session = cls_session;
  2670. session->host = shost;
  2671. session->state = ISCSI_STATE_FREE;
  2672. session->fast_abort = 1;
  2673. session->tgt_reset_timeout = 30;
  2674. session->lu_reset_timeout = 15;
  2675. session->abort_timeout = 10;
  2676. session->scsi_cmds_max = scsi_cmds;
  2677. session->cmds_max = scsi_cmds + ISCSI_MGMT_CMDS_MAX;
  2678. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  2679. session->exp_cmdsn = initial_cmdsn + 1;
  2680. session->max_cmdsn = initial_cmdsn + 1;
  2681. session->max_r2t = 1;
  2682. session->tt = iscsit;
  2683. session->dd_data = cls_session->dd_data + sizeof(*session);
  2684. session->tmf_state = TMF_INITIAL;
  2685. timer_setup(&session->tmf_timer, iscsi_tmf_timedout, 0);
  2686. mutex_init(&session->eh_mutex);
  2687. init_waitqueue_head(&session->ehwait);
  2688. spin_lock_init(&session->frwd_lock);
  2689. spin_lock_init(&session->back_lock);
  2690. /* initialize SCSI PDU commands pool */
  2691. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  2692. (void***)&session->cmds,
  2693. cmd_task_size + sizeof(struct iscsi_task)))
  2694. goto cmdpool_alloc_fail;
  2695. /* pre-format cmds pool with ITT */
  2696. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  2697. struct iscsi_task *task = session->cmds[cmd_i];
  2698. if (cmd_task_size)
  2699. task->dd_data = &task[1];
  2700. task->itt = cmd_i;
  2701. task->state = ISCSI_TASK_FREE;
  2702. INIT_LIST_HEAD(&task->running);
  2703. }
  2704. if (!try_module_get(iscsit->owner))
  2705. goto module_get_fail;
  2706. if (iscsi_add_session(cls_session, id))
  2707. goto cls_session_fail;
  2708. return cls_session;
  2709. cls_session_fail:
  2710. module_put(iscsit->owner);
  2711. module_get_fail:
  2712. iscsi_pool_free(&session->cmdpool);
  2713. cmdpool_alloc_fail:
  2714. iscsi_free_session(cls_session);
  2715. dec_session_count:
  2716. iscsi_host_dec_session_cnt(shost);
  2717. return NULL;
  2718. }
  2719. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  2720. /*
  2721. * issi_session_remove - Remove session from iSCSI class.
  2722. */
  2723. void iscsi_session_remove(struct iscsi_cls_session *cls_session)
  2724. {
  2725. struct iscsi_session *session = cls_session->dd_data;
  2726. struct Scsi_Host *shost = session->host;
  2727. iscsi_remove_session(cls_session);
  2728. /*
  2729. * host removal only has to wait for its children to be removed from
  2730. * sysfs, and iscsi_tcp needs to do iscsi_host_remove before freeing
  2731. * the session, so drop the session count here.
  2732. */
  2733. iscsi_host_dec_session_cnt(shost);
  2734. }
  2735. EXPORT_SYMBOL_GPL(iscsi_session_remove);
  2736. /**
  2737. * iscsi_session_free - Free iscsi session and it's resources
  2738. * @cls_session: iscsi session
  2739. */
  2740. void iscsi_session_free(struct iscsi_cls_session *cls_session)
  2741. {
  2742. struct iscsi_session *session = cls_session->dd_data;
  2743. struct module *owner = cls_session->transport->owner;
  2744. iscsi_pool_free(&session->cmdpool);
  2745. kfree(session->password);
  2746. kfree(session->password_in);
  2747. kfree(session->username);
  2748. kfree(session->username_in);
  2749. kfree(session->targetname);
  2750. kfree(session->targetalias);
  2751. kfree(session->initiatorname);
  2752. kfree(session->boot_root);
  2753. kfree(session->boot_nic);
  2754. kfree(session->boot_target);
  2755. kfree(session->ifacename);
  2756. kfree(session->portal_type);
  2757. kfree(session->discovery_parent_type);
  2758. iscsi_free_session(cls_session);
  2759. module_put(owner);
  2760. }
  2761. EXPORT_SYMBOL_GPL(iscsi_session_free);
  2762. /**
  2763. * iscsi_session_teardown - destroy session and cls_session
  2764. * @cls_session: iscsi session
  2765. */
  2766. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  2767. {
  2768. iscsi_session_remove(cls_session);
  2769. iscsi_session_free(cls_session);
  2770. }
  2771. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  2772. /**
  2773. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  2774. * @cls_session: iscsi_cls_session
  2775. * @dd_size: private driver data size
  2776. * @conn_idx: cid
  2777. */
  2778. struct iscsi_cls_conn *
  2779. iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
  2780. uint32_t conn_idx)
  2781. {
  2782. struct iscsi_session *session = cls_session->dd_data;
  2783. struct iscsi_conn *conn;
  2784. struct iscsi_cls_conn *cls_conn;
  2785. char *data;
  2786. int err;
  2787. cls_conn = iscsi_alloc_conn(cls_session, sizeof(*conn) + dd_size,
  2788. conn_idx);
  2789. if (!cls_conn)
  2790. return NULL;
  2791. conn = cls_conn->dd_data;
  2792. conn->dd_data = cls_conn->dd_data + sizeof(*conn);
  2793. conn->session = session;
  2794. conn->cls_conn = cls_conn;
  2795. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  2796. conn->id = conn_idx;
  2797. conn->exp_statsn = 0;
  2798. timer_setup(&conn->transport_timer, iscsi_check_transport_timeouts, 0);
  2799. INIT_LIST_HEAD(&conn->mgmtqueue);
  2800. INIT_LIST_HEAD(&conn->cmdqueue);
  2801. INIT_LIST_HEAD(&conn->requeue);
  2802. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  2803. /* allocate login_task used for the login/text sequences */
  2804. spin_lock_bh(&session->frwd_lock);
  2805. if (!kfifo_out(&session->cmdpool.queue,
  2806. (void*)&conn->login_task,
  2807. sizeof(void*))) {
  2808. spin_unlock_bh(&session->frwd_lock);
  2809. goto login_task_alloc_fail;
  2810. }
  2811. spin_unlock_bh(&session->frwd_lock);
  2812. data = (char *) __get_free_pages(GFP_KERNEL,
  2813. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2814. if (!data)
  2815. goto login_task_data_alloc_fail;
  2816. conn->login_task->data = conn->data = data;
  2817. err = iscsi_add_conn(cls_conn);
  2818. if (err)
  2819. goto login_task_add_dev_fail;
  2820. return cls_conn;
  2821. login_task_add_dev_fail:
  2822. free_pages((unsigned long) conn->data,
  2823. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2824. login_task_data_alloc_fail:
  2825. kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
  2826. sizeof(void*));
  2827. login_task_alloc_fail:
  2828. iscsi_put_conn(cls_conn);
  2829. return NULL;
  2830. }
  2831. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  2832. /**
  2833. * iscsi_conn_teardown - teardown iscsi connection
  2834. * @cls_conn: iscsi class connection
  2835. *
  2836. * TODO: we may need to make this into a two step process
  2837. * like scsi-mls remove + put host
  2838. */
  2839. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  2840. {
  2841. struct iscsi_conn *conn = cls_conn->dd_data;
  2842. struct iscsi_session *session = conn->session;
  2843. iscsi_remove_conn(cls_conn);
  2844. del_timer_sync(&conn->transport_timer);
  2845. mutex_lock(&session->eh_mutex);
  2846. spin_lock_bh(&session->frwd_lock);
  2847. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  2848. if (session->leadconn == conn) {
  2849. /*
  2850. * leading connection? then give up on recovery.
  2851. */
  2852. session->state = ISCSI_STATE_TERMINATE;
  2853. wake_up(&session->ehwait);
  2854. }
  2855. spin_unlock_bh(&session->frwd_lock);
  2856. /* flush queued up work because we free the connection below */
  2857. iscsi_suspend_tx(conn);
  2858. spin_lock_bh(&session->frwd_lock);
  2859. free_pages((unsigned long) conn->data,
  2860. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2861. kfree(conn->persistent_address);
  2862. kfree(conn->local_ipaddr);
  2863. /* regular RX path uses back_lock */
  2864. spin_lock_bh(&session->back_lock);
  2865. kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
  2866. sizeof(void*));
  2867. spin_unlock_bh(&session->back_lock);
  2868. if (session->leadconn == conn)
  2869. session->leadconn = NULL;
  2870. spin_unlock_bh(&session->frwd_lock);
  2871. mutex_unlock(&session->eh_mutex);
  2872. iscsi_put_conn(cls_conn);
  2873. }
  2874. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  2875. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  2876. {
  2877. struct iscsi_conn *conn = cls_conn->dd_data;
  2878. struct iscsi_session *session = conn->session;
  2879. if (!session) {
  2880. iscsi_conn_printk(KERN_ERR, conn,
  2881. "can't start unbound connection\n");
  2882. return -EPERM;
  2883. }
  2884. if ((session->imm_data_en || !session->initial_r2t_en) &&
  2885. session->first_burst > session->max_burst) {
  2886. iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
  2887. "first_burst %d max_burst %d\n",
  2888. session->first_burst, session->max_burst);
  2889. return -EINVAL;
  2890. }
  2891. if (conn->ping_timeout && !conn->recv_timeout) {
  2892. iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
  2893. "zero. Using 5 seconds\n.");
  2894. conn->recv_timeout = 5;
  2895. }
  2896. if (conn->recv_timeout && !conn->ping_timeout) {
  2897. iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
  2898. "zero. Using 5 seconds.\n");
  2899. conn->ping_timeout = 5;
  2900. }
  2901. spin_lock_bh(&session->frwd_lock);
  2902. conn->c_stage = ISCSI_CONN_STARTED;
  2903. session->state = ISCSI_STATE_LOGGED_IN;
  2904. session->queued_cmdsn = session->cmdsn;
  2905. conn->last_recv = jiffies;
  2906. conn->last_ping = jiffies;
  2907. if (conn->recv_timeout && conn->ping_timeout)
  2908. mod_timer(&conn->transport_timer,
  2909. jiffies + (conn->recv_timeout * HZ));
  2910. switch(conn->stop_stage) {
  2911. case STOP_CONN_RECOVER:
  2912. /*
  2913. * unblock eh_abort() if it is blocked. re-try all
  2914. * commands after successful recovery
  2915. */
  2916. conn->stop_stage = 0;
  2917. session->tmf_state = TMF_INITIAL;
  2918. session->age++;
  2919. if (session->age == 16)
  2920. session->age = 0;
  2921. break;
  2922. case STOP_CONN_TERM:
  2923. conn->stop_stage = 0;
  2924. break;
  2925. default:
  2926. break;
  2927. }
  2928. spin_unlock_bh(&session->frwd_lock);
  2929. iscsi_unblock_session(session->cls_session);
  2930. wake_up(&session->ehwait);
  2931. return 0;
  2932. }
  2933. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  2934. static void
  2935. fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
  2936. {
  2937. struct iscsi_task *task;
  2938. int i, state;
  2939. for (i = 0; i < conn->session->cmds_max; i++) {
  2940. task = conn->session->cmds[i];
  2941. if (task->sc)
  2942. continue;
  2943. if (task->state == ISCSI_TASK_FREE)
  2944. continue;
  2945. ISCSI_DBG_SESSION(conn->session,
  2946. "failing mgmt itt 0x%x state %d\n",
  2947. task->itt, task->state);
  2948. spin_lock_bh(&session->back_lock);
  2949. if (cleanup_queued_task(task)) {
  2950. spin_unlock_bh(&session->back_lock);
  2951. continue;
  2952. }
  2953. state = ISCSI_TASK_ABRT_SESS_RECOV;
  2954. if (task->state == ISCSI_TASK_PENDING)
  2955. state = ISCSI_TASK_COMPLETED;
  2956. iscsi_complete_task(task, state);
  2957. spin_unlock_bh(&session->back_lock);
  2958. }
  2959. }
  2960. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  2961. {
  2962. struct iscsi_conn *conn = cls_conn->dd_data;
  2963. struct iscsi_session *session = conn->session;
  2964. int old_stop_stage;
  2965. mutex_lock(&session->eh_mutex);
  2966. spin_lock_bh(&session->frwd_lock);
  2967. if (conn->stop_stage == STOP_CONN_TERM) {
  2968. spin_unlock_bh(&session->frwd_lock);
  2969. mutex_unlock(&session->eh_mutex);
  2970. return;
  2971. }
  2972. /*
  2973. * When this is called for the in_login state, we only want to clean
  2974. * up the login task and connection. We do not need to block and set
  2975. * the recovery state again
  2976. */
  2977. if (flag == STOP_CONN_TERM)
  2978. session->state = ISCSI_STATE_TERMINATE;
  2979. else if (conn->stop_stage != STOP_CONN_RECOVER)
  2980. session->state = ISCSI_STATE_IN_RECOVERY;
  2981. old_stop_stage = conn->stop_stage;
  2982. conn->stop_stage = flag;
  2983. spin_unlock_bh(&session->frwd_lock);
  2984. del_timer_sync(&conn->transport_timer);
  2985. iscsi_suspend_tx(conn);
  2986. spin_lock_bh(&session->frwd_lock);
  2987. conn->c_stage = ISCSI_CONN_STOPPED;
  2988. spin_unlock_bh(&session->frwd_lock);
  2989. /*
  2990. * for connection level recovery we should not calculate
  2991. * header digest. conn->hdr_size used for optimization
  2992. * in hdr_extract() and will be re-negotiated at
  2993. * set_param() time.
  2994. */
  2995. if (flag == STOP_CONN_RECOVER) {
  2996. conn->hdrdgst_en = 0;
  2997. conn->datadgst_en = 0;
  2998. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  2999. old_stop_stage != STOP_CONN_RECOVER) {
  3000. ISCSI_DBG_SESSION(session, "blocking session\n");
  3001. iscsi_block_session(session->cls_session);
  3002. }
  3003. }
  3004. /*
  3005. * flush queues.
  3006. */
  3007. spin_lock_bh(&session->frwd_lock);
  3008. fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
  3009. fail_mgmt_tasks(session, conn);
  3010. memset(&session->tmhdr, 0, sizeof(session->tmhdr));
  3011. spin_unlock_bh(&session->frwd_lock);
  3012. mutex_unlock(&session->eh_mutex);
  3013. }
  3014. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  3015. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  3016. struct iscsi_cls_conn *cls_conn, int is_leading)
  3017. {
  3018. struct iscsi_session *session = cls_session->dd_data;
  3019. struct iscsi_conn *conn = cls_conn->dd_data;
  3020. spin_lock_bh(&session->frwd_lock);
  3021. if (is_leading)
  3022. session->leadconn = conn;
  3023. set_bit(ISCSI_CONN_FLAG_BOUND, &conn->flags);
  3024. spin_unlock_bh(&session->frwd_lock);
  3025. /*
  3026. * The target could have reduced it's window size between logins, so
  3027. * we have to reset max/exp cmdsn so we can see the new values.
  3028. */
  3029. spin_lock_bh(&session->back_lock);
  3030. session->max_cmdsn = session->exp_cmdsn = session->cmdsn + 1;
  3031. spin_unlock_bh(&session->back_lock);
  3032. /*
  3033. * Unblock xmitworker(), Login Phase will pass through.
  3034. */
  3035. clear_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags);
  3036. clear_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags);
  3037. return 0;
  3038. }
  3039. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  3040. int iscsi_switch_str_param(char **param, char *new_val_buf)
  3041. {
  3042. char *new_val;
  3043. if (*param) {
  3044. if (!strcmp(*param, new_val_buf))
  3045. return 0;
  3046. }
  3047. new_val = kstrdup(new_val_buf, GFP_NOIO);
  3048. if (!new_val)
  3049. return -ENOMEM;
  3050. kfree(*param);
  3051. *param = new_val;
  3052. return 0;
  3053. }
  3054. EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
  3055. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  3056. enum iscsi_param param, char *buf, int buflen)
  3057. {
  3058. struct iscsi_conn *conn = cls_conn->dd_data;
  3059. struct iscsi_session *session = conn->session;
  3060. int val;
  3061. switch(param) {
  3062. case ISCSI_PARAM_FAST_ABORT:
  3063. sscanf(buf, "%d", &session->fast_abort);
  3064. break;
  3065. case ISCSI_PARAM_ABORT_TMO:
  3066. sscanf(buf, "%d", &session->abort_timeout);
  3067. break;
  3068. case ISCSI_PARAM_LU_RESET_TMO:
  3069. sscanf(buf, "%d", &session->lu_reset_timeout);
  3070. break;
  3071. case ISCSI_PARAM_TGT_RESET_TMO:
  3072. sscanf(buf, "%d", &session->tgt_reset_timeout);
  3073. break;
  3074. case ISCSI_PARAM_PING_TMO:
  3075. sscanf(buf, "%d", &conn->ping_timeout);
  3076. break;
  3077. case ISCSI_PARAM_RECV_TMO:
  3078. sscanf(buf, "%d", &conn->recv_timeout);
  3079. break;
  3080. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  3081. sscanf(buf, "%d", &conn->max_recv_dlength);
  3082. break;
  3083. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  3084. sscanf(buf, "%d", &conn->max_xmit_dlength);
  3085. break;
  3086. case ISCSI_PARAM_HDRDGST_EN:
  3087. sscanf(buf, "%d", &conn->hdrdgst_en);
  3088. break;
  3089. case ISCSI_PARAM_DATADGST_EN:
  3090. sscanf(buf, "%d", &conn->datadgst_en);
  3091. break;
  3092. case ISCSI_PARAM_INITIAL_R2T_EN:
  3093. sscanf(buf, "%d", &session->initial_r2t_en);
  3094. break;
  3095. case ISCSI_PARAM_MAX_R2T:
  3096. sscanf(buf, "%hu", &session->max_r2t);
  3097. break;
  3098. case ISCSI_PARAM_IMM_DATA_EN:
  3099. sscanf(buf, "%d", &session->imm_data_en);
  3100. break;
  3101. case ISCSI_PARAM_FIRST_BURST:
  3102. sscanf(buf, "%d", &session->first_burst);
  3103. break;
  3104. case ISCSI_PARAM_MAX_BURST:
  3105. sscanf(buf, "%d", &session->max_burst);
  3106. break;
  3107. case ISCSI_PARAM_PDU_INORDER_EN:
  3108. sscanf(buf, "%d", &session->pdu_inorder_en);
  3109. break;
  3110. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  3111. sscanf(buf, "%d", &session->dataseq_inorder_en);
  3112. break;
  3113. case ISCSI_PARAM_ERL:
  3114. sscanf(buf, "%d", &session->erl);
  3115. break;
  3116. case ISCSI_PARAM_EXP_STATSN:
  3117. sscanf(buf, "%u", &conn->exp_statsn);
  3118. break;
  3119. case ISCSI_PARAM_USERNAME:
  3120. return iscsi_switch_str_param(&session->username, buf);
  3121. case ISCSI_PARAM_USERNAME_IN:
  3122. return iscsi_switch_str_param(&session->username_in, buf);
  3123. case ISCSI_PARAM_PASSWORD:
  3124. return iscsi_switch_str_param(&session->password, buf);
  3125. case ISCSI_PARAM_PASSWORD_IN:
  3126. return iscsi_switch_str_param(&session->password_in, buf);
  3127. case ISCSI_PARAM_TARGET_NAME:
  3128. return iscsi_switch_str_param(&session->targetname, buf);
  3129. case ISCSI_PARAM_TARGET_ALIAS:
  3130. return iscsi_switch_str_param(&session->targetalias, buf);
  3131. case ISCSI_PARAM_TPGT:
  3132. sscanf(buf, "%d", &session->tpgt);
  3133. break;
  3134. case ISCSI_PARAM_PERSISTENT_PORT:
  3135. sscanf(buf, "%d", &conn->persistent_port);
  3136. break;
  3137. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  3138. return iscsi_switch_str_param(&conn->persistent_address, buf);
  3139. case ISCSI_PARAM_IFACE_NAME:
  3140. return iscsi_switch_str_param(&session->ifacename, buf);
  3141. case ISCSI_PARAM_INITIATOR_NAME:
  3142. return iscsi_switch_str_param(&session->initiatorname, buf);
  3143. case ISCSI_PARAM_BOOT_ROOT:
  3144. return iscsi_switch_str_param(&session->boot_root, buf);
  3145. case ISCSI_PARAM_BOOT_NIC:
  3146. return iscsi_switch_str_param(&session->boot_nic, buf);
  3147. case ISCSI_PARAM_BOOT_TARGET:
  3148. return iscsi_switch_str_param(&session->boot_target, buf);
  3149. case ISCSI_PARAM_PORTAL_TYPE:
  3150. return iscsi_switch_str_param(&session->portal_type, buf);
  3151. case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
  3152. return iscsi_switch_str_param(&session->discovery_parent_type,
  3153. buf);
  3154. case ISCSI_PARAM_DISCOVERY_SESS:
  3155. sscanf(buf, "%d", &val);
  3156. session->discovery_sess = !!val;
  3157. break;
  3158. case ISCSI_PARAM_LOCAL_IPADDR:
  3159. return iscsi_switch_str_param(&conn->local_ipaddr, buf);
  3160. default:
  3161. return -ENOSYS;
  3162. }
  3163. return 0;
  3164. }
  3165. EXPORT_SYMBOL_GPL(iscsi_set_param);
  3166. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  3167. enum iscsi_param param, char *buf)
  3168. {
  3169. struct iscsi_session *session = cls_session->dd_data;
  3170. int len;
  3171. switch(param) {
  3172. case ISCSI_PARAM_FAST_ABORT:
  3173. len = sysfs_emit(buf, "%d\n", session->fast_abort);
  3174. break;
  3175. case ISCSI_PARAM_ABORT_TMO:
  3176. len = sysfs_emit(buf, "%d\n", session->abort_timeout);
  3177. break;
  3178. case ISCSI_PARAM_LU_RESET_TMO:
  3179. len = sysfs_emit(buf, "%d\n", session->lu_reset_timeout);
  3180. break;
  3181. case ISCSI_PARAM_TGT_RESET_TMO:
  3182. len = sysfs_emit(buf, "%d\n", session->tgt_reset_timeout);
  3183. break;
  3184. case ISCSI_PARAM_INITIAL_R2T_EN:
  3185. len = sysfs_emit(buf, "%d\n", session->initial_r2t_en);
  3186. break;
  3187. case ISCSI_PARAM_MAX_R2T:
  3188. len = sysfs_emit(buf, "%hu\n", session->max_r2t);
  3189. break;
  3190. case ISCSI_PARAM_IMM_DATA_EN:
  3191. len = sysfs_emit(buf, "%d\n", session->imm_data_en);
  3192. break;
  3193. case ISCSI_PARAM_FIRST_BURST:
  3194. len = sysfs_emit(buf, "%u\n", session->first_burst);
  3195. break;
  3196. case ISCSI_PARAM_MAX_BURST:
  3197. len = sysfs_emit(buf, "%u\n", session->max_burst);
  3198. break;
  3199. case ISCSI_PARAM_PDU_INORDER_EN:
  3200. len = sysfs_emit(buf, "%d\n", session->pdu_inorder_en);
  3201. break;
  3202. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  3203. len = sysfs_emit(buf, "%d\n", session->dataseq_inorder_en);
  3204. break;
  3205. case ISCSI_PARAM_DEF_TASKMGMT_TMO:
  3206. len = sysfs_emit(buf, "%d\n", session->def_taskmgmt_tmo);
  3207. break;
  3208. case ISCSI_PARAM_ERL:
  3209. len = sysfs_emit(buf, "%d\n", session->erl);
  3210. break;
  3211. case ISCSI_PARAM_TARGET_NAME:
  3212. len = sysfs_emit(buf, "%s\n", session->targetname);
  3213. break;
  3214. case ISCSI_PARAM_TARGET_ALIAS:
  3215. len = sysfs_emit(buf, "%s\n", session->targetalias);
  3216. break;
  3217. case ISCSI_PARAM_TPGT:
  3218. len = sysfs_emit(buf, "%d\n", session->tpgt);
  3219. break;
  3220. case ISCSI_PARAM_USERNAME:
  3221. len = sysfs_emit(buf, "%s\n", session->username);
  3222. break;
  3223. case ISCSI_PARAM_USERNAME_IN:
  3224. len = sysfs_emit(buf, "%s\n", session->username_in);
  3225. break;
  3226. case ISCSI_PARAM_PASSWORD:
  3227. len = sysfs_emit(buf, "%s\n", session->password);
  3228. break;
  3229. case ISCSI_PARAM_PASSWORD_IN:
  3230. len = sysfs_emit(buf, "%s\n", session->password_in);
  3231. break;
  3232. case ISCSI_PARAM_IFACE_NAME:
  3233. len = sysfs_emit(buf, "%s\n", session->ifacename);
  3234. break;
  3235. case ISCSI_PARAM_INITIATOR_NAME:
  3236. len = sysfs_emit(buf, "%s\n", session->initiatorname);
  3237. break;
  3238. case ISCSI_PARAM_BOOT_ROOT:
  3239. len = sysfs_emit(buf, "%s\n", session->boot_root);
  3240. break;
  3241. case ISCSI_PARAM_BOOT_NIC:
  3242. len = sysfs_emit(buf, "%s\n", session->boot_nic);
  3243. break;
  3244. case ISCSI_PARAM_BOOT_TARGET:
  3245. len = sysfs_emit(buf, "%s\n", session->boot_target);
  3246. break;
  3247. case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
  3248. len = sysfs_emit(buf, "%u\n", session->auto_snd_tgt_disable);
  3249. break;
  3250. case ISCSI_PARAM_DISCOVERY_SESS:
  3251. len = sysfs_emit(buf, "%u\n", session->discovery_sess);
  3252. break;
  3253. case ISCSI_PARAM_PORTAL_TYPE:
  3254. len = sysfs_emit(buf, "%s\n", session->portal_type);
  3255. break;
  3256. case ISCSI_PARAM_CHAP_AUTH_EN:
  3257. len = sysfs_emit(buf, "%u\n", session->chap_auth_en);
  3258. break;
  3259. case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
  3260. len = sysfs_emit(buf, "%u\n", session->discovery_logout_en);
  3261. break;
  3262. case ISCSI_PARAM_BIDI_CHAP_EN:
  3263. len = sysfs_emit(buf, "%u\n", session->bidi_chap_en);
  3264. break;
  3265. case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
  3266. len = sysfs_emit(buf, "%u\n", session->discovery_auth_optional);
  3267. break;
  3268. case ISCSI_PARAM_DEF_TIME2WAIT:
  3269. len = sysfs_emit(buf, "%d\n", session->time2wait);
  3270. break;
  3271. case ISCSI_PARAM_DEF_TIME2RETAIN:
  3272. len = sysfs_emit(buf, "%d\n", session->time2retain);
  3273. break;
  3274. case ISCSI_PARAM_TSID:
  3275. len = sysfs_emit(buf, "%u\n", session->tsid);
  3276. break;
  3277. case ISCSI_PARAM_ISID:
  3278. len = sysfs_emit(buf, "%02x%02x%02x%02x%02x%02x\n",
  3279. session->isid[0], session->isid[1],
  3280. session->isid[2], session->isid[3],
  3281. session->isid[4], session->isid[5]);
  3282. break;
  3283. case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
  3284. len = sysfs_emit(buf, "%u\n", session->discovery_parent_idx);
  3285. break;
  3286. case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
  3287. if (session->discovery_parent_type)
  3288. len = sysfs_emit(buf, "%s\n",
  3289. session->discovery_parent_type);
  3290. else
  3291. len = sysfs_emit(buf, "\n");
  3292. break;
  3293. default:
  3294. return -ENOSYS;
  3295. }
  3296. return len;
  3297. }
  3298. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  3299. int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
  3300. enum iscsi_param param, char *buf)
  3301. {
  3302. struct sockaddr_in6 *sin6 = NULL;
  3303. struct sockaddr_in *sin = NULL;
  3304. int len;
  3305. switch (addr->ss_family) {
  3306. case AF_INET:
  3307. sin = (struct sockaddr_in *)addr;
  3308. break;
  3309. case AF_INET6:
  3310. sin6 = (struct sockaddr_in6 *)addr;
  3311. break;
  3312. default:
  3313. return -EINVAL;
  3314. }
  3315. switch (param) {
  3316. case ISCSI_PARAM_CONN_ADDRESS:
  3317. case ISCSI_HOST_PARAM_IPADDRESS:
  3318. if (sin)
  3319. len = sysfs_emit(buf, "%pI4\n", &sin->sin_addr.s_addr);
  3320. else
  3321. len = sysfs_emit(buf, "%pI6\n", &sin6->sin6_addr);
  3322. break;
  3323. case ISCSI_PARAM_CONN_PORT:
  3324. case ISCSI_PARAM_LOCAL_PORT:
  3325. if (sin)
  3326. len = sysfs_emit(buf, "%hu\n", be16_to_cpu(sin->sin_port));
  3327. else
  3328. len = sysfs_emit(buf, "%hu\n",
  3329. be16_to_cpu(sin6->sin6_port));
  3330. break;
  3331. default:
  3332. return -EINVAL;
  3333. }
  3334. return len;
  3335. }
  3336. EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
  3337. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  3338. enum iscsi_param param, char *buf)
  3339. {
  3340. struct iscsi_conn *conn = cls_conn->dd_data;
  3341. int len;
  3342. switch(param) {
  3343. case ISCSI_PARAM_PING_TMO:
  3344. len = sysfs_emit(buf, "%u\n", conn->ping_timeout);
  3345. break;
  3346. case ISCSI_PARAM_RECV_TMO:
  3347. len = sysfs_emit(buf, "%u\n", conn->recv_timeout);
  3348. break;
  3349. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  3350. len = sysfs_emit(buf, "%u\n", conn->max_recv_dlength);
  3351. break;
  3352. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  3353. len = sysfs_emit(buf, "%u\n", conn->max_xmit_dlength);
  3354. break;
  3355. case ISCSI_PARAM_HDRDGST_EN:
  3356. len = sysfs_emit(buf, "%d\n", conn->hdrdgst_en);
  3357. break;
  3358. case ISCSI_PARAM_DATADGST_EN:
  3359. len = sysfs_emit(buf, "%d\n", conn->datadgst_en);
  3360. break;
  3361. case ISCSI_PARAM_IFMARKER_EN:
  3362. len = sysfs_emit(buf, "%d\n", conn->ifmarker_en);
  3363. break;
  3364. case ISCSI_PARAM_OFMARKER_EN:
  3365. len = sysfs_emit(buf, "%d\n", conn->ofmarker_en);
  3366. break;
  3367. case ISCSI_PARAM_EXP_STATSN:
  3368. len = sysfs_emit(buf, "%u\n", conn->exp_statsn);
  3369. break;
  3370. case ISCSI_PARAM_PERSISTENT_PORT:
  3371. len = sysfs_emit(buf, "%d\n", conn->persistent_port);
  3372. break;
  3373. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  3374. len = sysfs_emit(buf, "%s\n", conn->persistent_address);
  3375. break;
  3376. case ISCSI_PARAM_STATSN:
  3377. len = sysfs_emit(buf, "%u\n", conn->statsn);
  3378. break;
  3379. case ISCSI_PARAM_MAX_SEGMENT_SIZE:
  3380. len = sysfs_emit(buf, "%u\n", conn->max_segment_size);
  3381. break;
  3382. case ISCSI_PARAM_KEEPALIVE_TMO:
  3383. len = sysfs_emit(buf, "%u\n", conn->keepalive_tmo);
  3384. break;
  3385. case ISCSI_PARAM_LOCAL_PORT:
  3386. len = sysfs_emit(buf, "%u\n", conn->local_port);
  3387. break;
  3388. case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
  3389. len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_stat);
  3390. break;
  3391. case ISCSI_PARAM_TCP_NAGLE_DISABLE:
  3392. len = sysfs_emit(buf, "%u\n", conn->tcp_nagle_disable);
  3393. break;
  3394. case ISCSI_PARAM_TCP_WSF_DISABLE:
  3395. len = sysfs_emit(buf, "%u\n", conn->tcp_wsf_disable);
  3396. break;
  3397. case ISCSI_PARAM_TCP_TIMER_SCALE:
  3398. len = sysfs_emit(buf, "%u\n", conn->tcp_timer_scale);
  3399. break;
  3400. case ISCSI_PARAM_TCP_TIMESTAMP_EN:
  3401. len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_en);
  3402. break;
  3403. case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
  3404. len = sysfs_emit(buf, "%u\n", conn->fragment_disable);
  3405. break;
  3406. case ISCSI_PARAM_IPV4_TOS:
  3407. len = sysfs_emit(buf, "%u\n", conn->ipv4_tos);
  3408. break;
  3409. case ISCSI_PARAM_IPV6_TC:
  3410. len = sysfs_emit(buf, "%u\n", conn->ipv6_traffic_class);
  3411. break;
  3412. case ISCSI_PARAM_IPV6_FLOW_LABEL:
  3413. len = sysfs_emit(buf, "%u\n", conn->ipv6_flow_label);
  3414. break;
  3415. case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
  3416. len = sysfs_emit(buf, "%u\n", conn->is_fw_assigned_ipv6);
  3417. break;
  3418. case ISCSI_PARAM_TCP_XMIT_WSF:
  3419. len = sysfs_emit(buf, "%u\n", conn->tcp_xmit_wsf);
  3420. break;
  3421. case ISCSI_PARAM_TCP_RECV_WSF:
  3422. len = sysfs_emit(buf, "%u\n", conn->tcp_recv_wsf);
  3423. break;
  3424. case ISCSI_PARAM_LOCAL_IPADDR:
  3425. len = sysfs_emit(buf, "%s\n", conn->local_ipaddr);
  3426. break;
  3427. default:
  3428. return -ENOSYS;
  3429. }
  3430. return len;
  3431. }
  3432. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  3433. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  3434. char *buf)
  3435. {
  3436. struct iscsi_host *ihost = shost_priv(shost);
  3437. int len;
  3438. switch (param) {
  3439. case ISCSI_HOST_PARAM_NETDEV_NAME:
  3440. len = sysfs_emit(buf, "%s\n", ihost->netdev);
  3441. break;
  3442. case ISCSI_HOST_PARAM_HWADDRESS:
  3443. len = sysfs_emit(buf, "%s\n", ihost->hwaddress);
  3444. break;
  3445. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  3446. len = sysfs_emit(buf, "%s\n", ihost->initiatorname);
  3447. break;
  3448. default:
  3449. return -ENOSYS;
  3450. }
  3451. return len;
  3452. }
  3453. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  3454. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  3455. char *buf, int buflen)
  3456. {
  3457. struct iscsi_host *ihost = shost_priv(shost);
  3458. switch (param) {
  3459. case ISCSI_HOST_PARAM_NETDEV_NAME:
  3460. return iscsi_switch_str_param(&ihost->netdev, buf);
  3461. case ISCSI_HOST_PARAM_HWADDRESS:
  3462. return iscsi_switch_str_param(&ihost->hwaddress, buf);
  3463. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  3464. return iscsi_switch_str_param(&ihost->initiatorname, buf);
  3465. default:
  3466. return -ENOSYS;
  3467. }
  3468. return 0;
  3469. }
  3470. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  3471. MODULE_AUTHOR("Mike Christie");
  3472. MODULE_DESCRIPTION("iSCSI library functions");
  3473. MODULE_LICENSE("GPL");