libiscsi.c 98 KB

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