libiscsi.c 91 KB

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