libiscsi.c 77 KB

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