libiscsi.c 91 KB

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