libiscsi.c 98 KB

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