libiscsi.c 83 KB

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