libiscsi.c 96 KB

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