libiscsi.c 96 KB

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