libiscsi.c 99 KB

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