libiscsi.c 98 KB

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