libiscsi.c 91 KB

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