libiscsi.c 81 KB

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