libiscsi.c 91 KB

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