libiscsi.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980
  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. /*
  1212. * ISCSI_STATE_FAILED is a temp. state. The recovery
  1213. * code will decide what is best to do with command queued
  1214. * during this time
  1215. */
  1216. if (session->state != ISCSI_STATE_LOGGED_IN &&
  1217. session->state != ISCSI_STATE_FAILED) {
  1218. /*
  1219. * to handle the race between when we set the recovery state
  1220. * and block the session we requeue here (commands could
  1221. * be entering our queuecommand while a block is starting
  1222. * up because the block code is not locked)
  1223. */
  1224. switch (session->state) {
  1225. case ISCSI_STATE_IN_RECOVERY:
  1226. reason = FAILURE_SESSION_IN_RECOVERY;
  1227. goto reject;
  1228. case ISCSI_STATE_LOGGING_OUT:
  1229. reason = FAILURE_SESSION_LOGGING_OUT;
  1230. goto reject;
  1231. case ISCSI_STATE_RECOVERY_FAILED:
  1232. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  1233. sc->result = DID_TRANSPORT_FAILFAST << 16;
  1234. break;
  1235. case ISCSI_STATE_TERMINATE:
  1236. reason = FAILURE_SESSION_TERMINATE;
  1237. sc->result = DID_NO_CONNECT << 16;
  1238. break;
  1239. default:
  1240. reason = FAILURE_SESSION_FREED;
  1241. sc->result = DID_NO_CONNECT << 16;
  1242. }
  1243. goto fault;
  1244. }
  1245. conn = session->leadconn;
  1246. if (!conn) {
  1247. reason = FAILURE_SESSION_FREED;
  1248. sc->result = DID_NO_CONNECT << 16;
  1249. goto fault;
  1250. }
  1251. if (iscsi_check_cmdsn_window_closed(conn)) {
  1252. reason = FAILURE_WINDOW_CLOSED;
  1253. goto reject;
  1254. }
  1255. task = iscsi_alloc_task(conn, sc);
  1256. if (!task) {
  1257. reason = FAILURE_OOM;
  1258. goto reject;
  1259. }
  1260. if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
  1261. reason = iscsi_prep_scsi_cmd_pdu(task);
  1262. if (reason) {
  1263. if (reason == -ENOMEM) {
  1264. reason = FAILURE_OOM;
  1265. goto prepd_reject;
  1266. } else {
  1267. sc->result = DID_ABORT << 16;
  1268. goto prepd_fault;
  1269. }
  1270. }
  1271. if (session->tt->xmit_task(task)) {
  1272. reason = FAILURE_SESSION_NOT_READY;
  1273. goto prepd_reject;
  1274. }
  1275. } else {
  1276. list_add_tail(&task->running, &conn->cmdqueue);
  1277. iscsi_conn_queue_work(conn);
  1278. }
  1279. session->queued_cmdsn++;
  1280. spin_unlock(&session->lock);
  1281. spin_lock(host->host_lock);
  1282. return 0;
  1283. prepd_reject:
  1284. sc->scsi_done = NULL;
  1285. iscsi_complete_task(task);
  1286. reject:
  1287. spin_unlock(&session->lock);
  1288. ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
  1289. sc->cmnd[0], reason);
  1290. spin_lock(host->host_lock);
  1291. return SCSI_MLQUEUE_TARGET_BUSY;
  1292. prepd_fault:
  1293. sc->scsi_done = NULL;
  1294. iscsi_complete_task(task);
  1295. fault:
  1296. spin_unlock(&session->lock);
  1297. ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
  1298. sc->cmnd[0], reason);
  1299. if (!scsi_bidi_cmnd(sc))
  1300. scsi_set_resid(sc, scsi_bufflen(sc));
  1301. else {
  1302. scsi_out(sc)->resid = scsi_out(sc)->length;
  1303. scsi_in(sc)->resid = scsi_in(sc)->length;
  1304. }
  1305. done(sc);
  1306. spin_lock(host->host_lock);
  1307. return 0;
  1308. }
  1309. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  1310. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  1311. {
  1312. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  1313. return sdev->queue_depth;
  1314. }
  1315. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  1316. int iscsi_target_alloc(struct scsi_target *starget)
  1317. {
  1318. struct iscsi_cls_session *cls_session = starget_to_session(starget);
  1319. struct iscsi_session *session = cls_session->dd_data;
  1320. starget->can_queue = session->scsi_cmds_max;
  1321. return 0;
  1322. }
  1323. EXPORT_SYMBOL_GPL(iscsi_target_alloc);
  1324. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  1325. {
  1326. struct iscsi_session *session = cls_session->dd_data;
  1327. spin_lock_bh(&session->lock);
  1328. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1329. session->state = ISCSI_STATE_RECOVERY_FAILED;
  1330. if (session->leadconn)
  1331. wake_up(&session->leadconn->ehwait);
  1332. }
  1333. spin_unlock_bh(&session->lock);
  1334. }
  1335. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  1336. int iscsi_eh_target_reset(struct scsi_cmnd *sc)
  1337. {
  1338. struct iscsi_cls_session *cls_session;
  1339. struct iscsi_session *session;
  1340. struct iscsi_conn *conn;
  1341. cls_session = starget_to_session(scsi_target(sc->device));
  1342. session = cls_session->dd_data;
  1343. conn = session->leadconn;
  1344. mutex_lock(&session->eh_mutex);
  1345. spin_lock_bh(&session->lock);
  1346. if (session->state == ISCSI_STATE_TERMINATE) {
  1347. failed:
  1348. iscsi_session_printk(KERN_INFO, session,
  1349. "failing target reset: Could not log "
  1350. "back into target [age %d]\n",
  1351. session->age);
  1352. spin_unlock_bh(&session->lock);
  1353. mutex_unlock(&session->eh_mutex);
  1354. return FAILED;
  1355. }
  1356. spin_unlock_bh(&session->lock);
  1357. mutex_unlock(&session->eh_mutex);
  1358. /*
  1359. * we drop the lock here but the leadconn cannot be destoyed while
  1360. * we are in the scsi eh
  1361. */
  1362. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1363. ISCSI_DBG_SESSION(session, "wait for relogin\n");
  1364. wait_event_interruptible(conn->ehwait,
  1365. session->state == ISCSI_STATE_TERMINATE ||
  1366. session->state == ISCSI_STATE_LOGGED_IN ||
  1367. session->state == ISCSI_STATE_RECOVERY_FAILED);
  1368. if (signal_pending(current))
  1369. flush_signals(current);
  1370. mutex_lock(&session->eh_mutex);
  1371. spin_lock_bh(&session->lock);
  1372. if (session->state == ISCSI_STATE_LOGGED_IN)
  1373. iscsi_session_printk(KERN_INFO, session,
  1374. "target reset succeeded\n");
  1375. else
  1376. goto failed;
  1377. spin_unlock_bh(&session->lock);
  1378. mutex_unlock(&session->eh_mutex);
  1379. return SUCCESS;
  1380. }
  1381. EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
  1382. static void iscsi_tmf_timedout(unsigned long data)
  1383. {
  1384. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1385. struct iscsi_session *session = conn->session;
  1386. spin_lock(&session->lock);
  1387. if (conn->tmf_state == TMF_QUEUED) {
  1388. conn->tmf_state = TMF_TIMEDOUT;
  1389. ISCSI_DBG_SESSION(session, "tmf timedout\n");
  1390. /* unblock eh_abort() */
  1391. wake_up(&conn->ehwait);
  1392. }
  1393. spin_unlock(&session->lock);
  1394. }
  1395. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  1396. struct iscsi_tm *hdr, int age,
  1397. int timeout)
  1398. {
  1399. struct iscsi_session *session = conn->session;
  1400. struct iscsi_task *task;
  1401. task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  1402. NULL, 0);
  1403. if (!task) {
  1404. spin_unlock_bh(&session->lock);
  1405. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1406. spin_lock_bh(&session->lock);
  1407. ISCSI_DBG_SESSION(session, "tmf exec failure\n");
  1408. return -EPERM;
  1409. }
  1410. conn->tmfcmd_pdus_cnt++;
  1411. conn->tmf_timer.expires = timeout * HZ + jiffies;
  1412. conn->tmf_timer.function = iscsi_tmf_timedout;
  1413. conn->tmf_timer.data = (unsigned long)conn;
  1414. add_timer(&conn->tmf_timer);
  1415. ISCSI_DBG_SESSION(session, "tmf set timeout\n");
  1416. spin_unlock_bh(&session->lock);
  1417. mutex_unlock(&session->eh_mutex);
  1418. /*
  1419. * block eh thread until:
  1420. *
  1421. * 1) tmf response
  1422. * 2) tmf timeout
  1423. * 3) session is terminated or restarted or userspace has
  1424. * given up on recovery
  1425. */
  1426. wait_event_interruptible(conn->ehwait, age != session->age ||
  1427. session->state != ISCSI_STATE_LOGGED_IN ||
  1428. conn->tmf_state != TMF_QUEUED);
  1429. if (signal_pending(current))
  1430. flush_signals(current);
  1431. del_timer_sync(&conn->tmf_timer);
  1432. mutex_lock(&session->eh_mutex);
  1433. spin_lock_bh(&session->lock);
  1434. /* if the session drops it will clean up the task */
  1435. if (age != session->age ||
  1436. session->state != ISCSI_STATE_LOGGED_IN)
  1437. return -ENOTCONN;
  1438. return 0;
  1439. }
  1440. /*
  1441. * Fail commands. session lock held and recv side suspended and xmit
  1442. * thread flushed
  1443. */
  1444. static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
  1445. int error)
  1446. {
  1447. struct iscsi_task *task;
  1448. int i;
  1449. for (i = 0; i < conn->session->cmds_max; i++) {
  1450. task = conn->session->cmds[i];
  1451. if (!task->sc || task->state == ISCSI_TASK_FREE)
  1452. continue;
  1453. if (lun != -1 && lun != task->sc->device->lun)
  1454. continue;
  1455. ISCSI_DBG_SESSION(conn->session,
  1456. "failing sc %p itt 0x%x state %d\n",
  1457. task->sc, task->itt, task->state);
  1458. fail_scsi_task(task, error << 16);
  1459. }
  1460. }
  1461. void iscsi_suspend_tx(struct iscsi_conn *conn)
  1462. {
  1463. struct Scsi_Host *shost = conn->session->host;
  1464. struct iscsi_host *ihost = shost_priv(shost);
  1465. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1466. if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
  1467. flush_workqueue(ihost->workq);
  1468. }
  1469. EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
  1470. static void iscsi_start_tx(struct iscsi_conn *conn)
  1471. {
  1472. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1473. if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
  1474. iscsi_conn_queue_work(conn);
  1475. }
  1476. /*
  1477. * We want to make sure a ping is in flight. It has timed out.
  1478. * And we are not busy processing a pdu that is making
  1479. * progress but got started before the ping and is taking a while
  1480. * to complete so the ping is just stuck behind it in a queue.
  1481. */
  1482. static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
  1483. {
  1484. if (conn->ping_task &&
  1485. time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
  1486. (conn->ping_timeout * HZ), jiffies))
  1487. return 1;
  1488. else
  1489. return 0;
  1490. }
  1491. static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
  1492. {
  1493. struct iscsi_cls_session *cls_session;
  1494. struct iscsi_session *session;
  1495. struct iscsi_conn *conn;
  1496. enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
  1497. cls_session = starget_to_session(scsi_target(scmd->device));
  1498. session = cls_session->dd_data;
  1499. ISCSI_DBG_SESSION(session, "scsi cmd %p timedout\n", scmd);
  1500. spin_lock(&session->lock);
  1501. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1502. /*
  1503. * We are probably in the middle of iscsi recovery so let
  1504. * that complete and handle the error.
  1505. */
  1506. rc = BLK_EH_RESET_TIMER;
  1507. goto done;
  1508. }
  1509. conn = session->leadconn;
  1510. if (!conn) {
  1511. /* In the middle of shuting down */
  1512. rc = BLK_EH_RESET_TIMER;
  1513. goto done;
  1514. }
  1515. if (!conn->recv_timeout && !conn->ping_timeout)
  1516. goto done;
  1517. /*
  1518. * if the ping timedout then we are in the middle of cleaning up
  1519. * and can let the iscsi eh handle it
  1520. */
  1521. if (iscsi_has_ping_timed_out(conn)) {
  1522. rc = BLK_EH_RESET_TIMER;
  1523. goto done;
  1524. }
  1525. /*
  1526. * if we are about to check the transport then give the command
  1527. * more time
  1528. */
  1529. if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
  1530. jiffies)) {
  1531. rc = BLK_EH_RESET_TIMER;
  1532. goto done;
  1533. }
  1534. /* if in the middle of checking the transport then give us more time */
  1535. if (conn->ping_task)
  1536. rc = BLK_EH_RESET_TIMER;
  1537. done:
  1538. spin_unlock(&session->lock);
  1539. ISCSI_DBG_SESSION(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
  1540. "timer reset" : "nh");
  1541. return rc;
  1542. }
  1543. static void iscsi_check_transport_timeouts(unsigned long data)
  1544. {
  1545. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1546. struct iscsi_session *session = conn->session;
  1547. unsigned long recv_timeout, next_timeout = 0, last_recv;
  1548. spin_lock(&session->lock);
  1549. if (session->state != ISCSI_STATE_LOGGED_IN)
  1550. goto done;
  1551. recv_timeout = conn->recv_timeout;
  1552. if (!recv_timeout)
  1553. goto done;
  1554. recv_timeout *= HZ;
  1555. last_recv = conn->last_recv;
  1556. if (iscsi_has_ping_timed_out(conn)) {
  1557. iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
  1558. "expired, recv timeout %d, last rx %lu, "
  1559. "last ping %lu, now %lu\n",
  1560. conn->ping_timeout, conn->recv_timeout,
  1561. last_recv, conn->last_ping, jiffies);
  1562. spin_unlock(&session->lock);
  1563. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1564. return;
  1565. }
  1566. if (time_before_eq(last_recv + recv_timeout, jiffies)) {
  1567. /* send a ping to try to provoke some traffic */
  1568. ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
  1569. iscsi_send_nopout(conn, NULL);
  1570. next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
  1571. } else
  1572. next_timeout = last_recv + recv_timeout;
  1573. ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
  1574. mod_timer(&conn->transport_timer, next_timeout);
  1575. done:
  1576. spin_unlock(&session->lock);
  1577. }
  1578. static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
  1579. struct iscsi_tm *hdr)
  1580. {
  1581. memset(hdr, 0, sizeof(*hdr));
  1582. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1583. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  1584. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1585. memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
  1586. hdr->rtt = task->hdr_itt;
  1587. hdr->refcmdsn = task->cmdsn;
  1588. }
  1589. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1590. {
  1591. struct iscsi_cls_session *cls_session;
  1592. struct iscsi_session *session;
  1593. struct iscsi_conn *conn;
  1594. struct iscsi_task *task;
  1595. struct iscsi_tm *hdr;
  1596. int rc, age;
  1597. cls_session = starget_to_session(scsi_target(sc->device));
  1598. session = cls_session->dd_data;
  1599. mutex_lock(&session->eh_mutex);
  1600. spin_lock_bh(&session->lock);
  1601. /*
  1602. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1603. * got the command.
  1604. */
  1605. if (!sc->SCp.ptr) {
  1606. ISCSI_DBG_SESSION(session, "sc never reached iscsi layer or "
  1607. "it completed.\n");
  1608. spin_unlock_bh(&session->lock);
  1609. mutex_unlock(&session->eh_mutex);
  1610. return SUCCESS;
  1611. }
  1612. /*
  1613. * If we are not logged in or we have started a new session
  1614. * then let the host reset code handle this
  1615. */
  1616. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  1617. sc->SCp.phase != session->age) {
  1618. spin_unlock_bh(&session->lock);
  1619. mutex_unlock(&session->eh_mutex);
  1620. return FAILED;
  1621. }
  1622. conn = session->leadconn;
  1623. conn->eh_abort_cnt++;
  1624. age = session->age;
  1625. task = (struct iscsi_task *)sc->SCp.ptr;
  1626. ISCSI_DBG_SESSION(session, "aborting [sc %p itt 0x%x]\n",
  1627. sc, task->itt);
  1628. /* task completed before time out */
  1629. if (!task->sc) {
  1630. ISCSI_DBG_SESSION(session, "sc completed while abort in "
  1631. "progress\n");
  1632. goto success;
  1633. }
  1634. if (task->state == ISCSI_TASK_PENDING) {
  1635. fail_scsi_task(task, DID_ABORT << 16);
  1636. goto success;
  1637. }
  1638. /* only have one tmf outstanding at a time */
  1639. if (conn->tmf_state != TMF_INITIAL)
  1640. goto failed;
  1641. conn->tmf_state = TMF_QUEUED;
  1642. hdr = &conn->tmhdr;
  1643. iscsi_prep_abort_task_pdu(task, hdr);
  1644. if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
  1645. rc = FAILED;
  1646. goto failed;
  1647. }
  1648. switch (conn->tmf_state) {
  1649. case TMF_SUCCESS:
  1650. spin_unlock_bh(&session->lock);
  1651. /*
  1652. * stop tx side incase the target had sent a abort rsp but
  1653. * the initiator was still writing out data.
  1654. */
  1655. iscsi_suspend_tx(conn);
  1656. /*
  1657. * we do not stop the recv side because targets have been
  1658. * good and have never sent us a successful tmf response
  1659. * then sent more data for the cmd.
  1660. */
  1661. spin_lock(&session->lock);
  1662. fail_scsi_task(task, DID_ABORT << 16);
  1663. conn->tmf_state = TMF_INITIAL;
  1664. spin_unlock(&session->lock);
  1665. iscsi_start_tx(conn);
  1666. goto success_unlocked;
  1667. case TMF_TIMEDOUT:
  1668. spin_unlock_bh(&session->lock);
  1669. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1670. goto failed_unlocked;
  1671. case TMF_NOT_FOUND:
  1672. if (!sc->SCp.ptr) {
  1673. conn->tmf_state = TMF_INITIAL;
  1674. /* task completed before tmf abort response */
  1675. ISCSI_DBG_SESSION(session, "sc completed while abort "
  1676. "in progress\n");
  1677. goto success;
  1678. }
  1679. /* fall through */
  1680. default:
  1681. conn->tmf_state = TMF_INITIAL;
  1682. goto failed;
  1683. }
  1684. success:
  1685. spin_unlock_bh(&session->lock);
  1686. success_unlocked:
  1687. ISCSI_DBG_SESSION(session, "abort success [sc %p itt 0x%x]\n",
  1688. sc, task->itt);
  1689. mutex_unlock(&session->eh_mutex);
  1690. return SUCCESS;
  1691. failed:
  1692. spin_unlock_bh(&session->lock);
  1693. failed_unlocked:
  1694. ISCSI_DBG_SESSION(session, "abort failed [sc %p itt 0x%x]\n", sc,
  1695. task ? task->itt : 0);
  1696. mutex_unlock(&session->eh_mutex);
  1697. return FAILED;
  1698. }
  1699. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1700. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  1701. {
  1702. memset(hdr, 0, sizeof(*hdr));
  1703. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1704. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  1705. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1706. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  1707. hdr->rtt = RESERVED_ITT;
  1708. }
  1709. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  1710. {
  1711. struct iscsi_cls_session *cls_session;
  1712. struct iscsi_session *session;
  1713. struct iscsi_conn *conn;
  1714. struct iscsi_tm *hdr;
  1715. int rc = FAILED;
  1716. cls_session = starget_to_session(scsi_target(sc->device));
  1717. session = cls_session->dd_data;
  1718. ISCSI_DBG_SESSION(session, "LU Reset [sc %p lun %u]\n",
  1719. sc, sc->device->lun);
  1720. mutex_lock(&session->eh_mutex);
  1721. spin_lock_bh(&session->lock);
  1722. /*
  1723. * Just check if we are not logged in. We cannot check for
  1724. * the phase because the reset could come from a ioctl.
  1725. */
  1726. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  1727. goto unlock;
  1728. conn = session->leadconn;
  1729. /* only have one tmf outstanding at a time */
  1730. if (conn->tmf_state != TMF_INITIAL)
  1731. goto unlock;
  1732. conn->tmf_state = TMF_QUEUED;
  1733. hdr = &conn->tmhdr;
  1734. iscsi_prep_lun_reset_pdu(sc, hdr);
  1735. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  1736. session->lu_reset_timeout)) {
  1737. rc = FAILED;
  1738. goto unlock;
  1739. }
  1740. switch (conn->tmf_state) {
  1741. case TMF_SUCCESS:
  1742. break;
  1743. case TMF_TIMEDOUT:
  1744. spin_unlock_bh(&session->lock);
  1745. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1746. goto done;
  1747. default:
  1748. conn->tmf_state = TMF_INITIAL;
  1749. goto unlock;
  1750. }
  1751. rc = SUCCESS;
  1752. spin_unlock_bh(&session->lock);
  1753. iscsi_suspend_tx(conn);
  1754. spin_lock_bh(&session->lock);
  1755. fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
  1756. conn->tmf_state = TMF_INITIAL;
  1757. spin_unlock_bh(&session->lock);
  1758. iscsi_start_tx(conn);
  1759. goto done;
  1760. unlock:
  1761. spin_unlock_bh(&session->lock);
  1762. done:
  1763. ISCSI_DBG_SESSION(session, "dev reset result = %s\n",
  1764. rc == SUCCESS ? "SUCCESS" : "FAILED");
  1765. mutex_unlock(&session->eh_mutex);
  1766. return rc;
  1767. }
  1768. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  1769. /*
  1770. * Pre-allocate a pool of @max items of @item_size. By default, the pool
  1771. * should be accessed via kfifo_{get,put} on q->queue.
  1772. * Optionally, the caller can obtain the array of object pointers
  1773. * by passing in a non-NULL @items pointer
  1774. */
  1775. int
  1776. iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
  1777. {
  1778. int i, num_arrays = 1;
  1779. memset(q, 0, sizeof(*q));
  1780. q->max = max;
  1781. /* If the user passed an items pointer, he wants a copy of
  1782. * the array. */
  1783. if (items)
  1784. num_arrays++;
  1785. q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
  1786. if (q->pool == NULL)
  1787. return -ENOMEM;
  1788. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1789. GFP_KERNEL, NULL);
  1790. if (IS_ERR(q->queue)) {
  1791. q->queue = NULL;
  1792. goto enomem;
  1793. }
  1794. for (i = 0; i < max; i++) {
  1795. q->pool[i] = kzalloc(item_size, GFP_KERNEL);
  1796. if (q->pool[i] == NULL) {
  1797. q->max = i;
  1798. goto enomem;
  1799. }
  1800. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1801. }
  1802. if (items) {
  1803. *items = q->pool + max;
  1804. memcpy(*items, q->pool, max * sizeof(void *));
  1805. }
  1806. return 0;
  1807. enomem:
  1808. iscsi_pool_free(q);
  1809. return -ENOMEM;
  1810. }
  1811. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1812. void iscsi_pool_free(struct iscsi_pool *q)
  1813. {
  1814. int i;
  1815. for (i = 0; i < q->max; i++)
  1816. kfree(q->pool[i]);
  1817. kfree(q->pool);
  1818. kfree(q->queue);
  1819. }
  1820. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1821. /**
  1822. * iscsi_host_add - add host to system
  1823. * @shost: scsi host
  1824. * @pdev: parent device
  1825. *
  1826. * This should be called by partial offload and software iscsi drivers
  1827. * to add a host to the system.
  1828. */
  1829. int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
  1830. {
  1831. if (!shost->can_queue)
  1832. shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
  1833. if (!shost->cmd_per_lun)
  1834. shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
  1835. if (!shost->transportt->eh_timed_out)
  1836. shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
  1837. return scsi_add_host(shost, pdev);
  1838. }
  1839. EXPORT_SYMBOL_GPL(iscsi_host_add);
  1840. /**
  1841. * iscsi_host_alloc - allocate a host and driver data
  1842. * @sht: scsi host template
  1843. * @dd_data_size: driver host data size
  1844. * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
  1845. *
  1846. * This should be called by partial offload and software iscsi drivers.
  1847. * To access the driver specific memory use the iscsi_host_priv() macro.
  1848. */
  1849. struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
  1850. int dd_data_size, bool xmit_can_sleep)
  1851. {
  1852. struct Scsi_Host *shost;
  1853. struct iscsi_host *ihost;
  1854. shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
  1855. if (!shost)
  1856. return NULL;
  1857. ihost = shost_priv(shost);
  1858. if (xmit_can_sleep) {
  1859. snprintf(ihost->workq_name, sizeof(ihost->workq_name),
  1860. "iscsi_q_%d", shost->host_no);
  1861. ihost->workq = create_singlethread_workqueue(ihost->workq_name);
  1862. if (!ihost->workq)
  1863. goto free_host;
  1864. }
  1865. spin_lock_init(&ihost->lock);
  1866. ihost->state = ISCSI_HOST_SETUP;
  1867. ihost->num_sessions = 0;
  1868. init_waitqueue_head(&ihost->session_removal_wq);
  1869. return shost;
  1870. free_host:
  1871. scsi_host_put(shost);
  1872. return NULL;
  1873. }
  1874. EXPORT_SYMBOL_GPL(iscsi_host_alloc);
  1875. static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
  1876. {
  1877. iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
  1878. }
  1879. /**
  1880. * iscsi_host_remove - remove host and sessions
  1881. * @shost: scsi host
  1882. *
  1883. * If there are any sessions left, this will initiate the removal and wait
  1884. * for the completion.
  1885. */
  1886. void iscsi_host_remove(struct Scsi_Host *shost)
  1887. {
  1888. struct iscsi_host *ihost = shost_priv(shost);
  1889. unsigned long flags;
  1890. spin_lock_irqsave(&ihost->lock, flags);
  1891. ihost->state = ISCSI_HOST_REMOVED;
  1892. spin_unlock_irqrestore(&ihost->lock, flags);
  1893. iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
  1894. wait_event_interruptible(ihost->session_removal_wq,
  1895. ihost->num_sessions == 0);
  1896. if (signal_pending(current))
  1897. flush_signals(current);
  1898. scsi_remove_host(shost);
  1899. if (ihost->workq)
  1900. destroy_workqueue(ihost->workq);
  1901. }
  1902. EXPORT_SYMBOL_GPL(iscsi_host_remove);
  1903. void iscsi_host_free(struct Scsi_Host *shost)
  1904. {
  1905. struct iscsi_host *ihost = shost_priv(shost);
  1906. kfree(ihost->netdev);
  1907. kfree(ihost->hwaddress);
  1908. kfree(ihost->initiatorname);
  1909. scsi_host_put(shost);
  1910. }
  1911. EXPORT_SYMBOL_GPL(iscsi_host_free);
  1912. static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
  1913. {
  1914. struct iscsi_host *ihost = shost_priv(shost);
  1915. unsigned long flags;
  1916. shost = scsi_host_get(shost);
  1917. if (!shost) {
  1918. printk(KERN_ERR "Invalid state. Cannot notify host removal "
  1919. "of session teardown event because host already "
  1920. "removed.\n");
  1921. return;
  1922. }
  1923. spin_lock_irqsave(&ihost->lock, flags);
  1924. ihost->num_sessions--;
  1925. if (ihost->num_sessions == 0)
  1926. wake_up(&ihost->session_removal_wq);
  1927. spin_unlock_irqrestore(&ihost->lock, flags);
  1928. scsi_host_put(shost);
  1929. }
  1930. /**
  1931. * iscsi_session_setup - create iscsi cls session and host and session
  1932. * @iscsit: iscsi transport template
  1933. * @shost: scsi host
  1934. * @cmds_max: session can queue
  1935. * @cmd_task_size: LLD task private data size
  1936. * @initial_cmdsn: initial CmdSN
  1937. *
  1938. * This can be used by software iscsi_transports that allocate
  1939. * a session per scsi host.
  1940. *
  1941. * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
  1942. * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
  1943. * for nop handling and login/logout requests.
  1944. */
  1945. struct iscsi_cls_session *
  1946. iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
  1947. uint16_t cmds_max, int cmd_task_size,
  1948. uint32_t initial_cmdsn, unsigned int id)
  1949. {
  1950. struct iscsi_host *ihost = shost_priv(shost);
  1951. struct iscsi_session *session;
  1952. struct iscsi_cls_session *cls_session;
  1953. int cmd_i, scsi_cmds, total_cmds = cmds_max;
  1954. unsigned long flags;
  1955. spin_lock_irqsave(&ihost->lock, flags);
  1956. if (ihost->state == ISCSI_HOST_REMOVED) {
  1957. spin_unlock_irqrestore(&ihost->lock, flags);
  1958. return NULL;
  1959. }
  1960. ihost->num_sessions++;
  1961. spin_unlock_irqrestore(&ihost->lock, flags);
  1962. if (!total_cmds)
  1963. total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
  1964. /*
  1965. * The iscsi layer needs some tasks for nop handling and tmfs,
  1966. * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
  1967. * + 1 command for scsi IO.
  1968. */
  1969. if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
  1970. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  1971. "must be a power of two that is at least %d.\n",
  1972. total_cmds, ISCSI_TOTAL_CMDS_MIN);
  1973. goto dec_session_count;
  1974. }
  1975. if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
  1976. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  1977. "must be a power of 2 less than or equal to %d.\n",
  1978. cmds_max, ISCSI_TOTAL_CMDS_MAX);
  1979. total_cmds = ISCSI_TOTAL_CMDS_MAX;
  1980. }
  1981. if (!is_power_of_2(total_cmds)) {
  1982. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  1983. "must be a power of 2.\n", total_cmds);
  1984. total_cmds = rounddown_pow_of_two(total_cmds);
  1985. if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
  1986. return NULL;
  1987. printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
  1988. total_cmds);
  1989. }
  1990. scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
  1991. cls_session = iscsi_alloc_session(shost, iscsit,
  1992. sizeof(struct iscsi_session));
  1993. if (!cls_session)
  1994. goto dec_session_count;
  1995. session = cls_session->dd_data;
  1996. session->cls_session = cls_session;
  1997. session->host = shost;
  1998. session->state = ISCSI_STATE_FREE;
  1999. session->fast_abort = 1;
  2000. session->lu_reset_timeout = 15;
  2001. session->abort_timeout = 10;
  2002. session->scsi_cmds_max = scsi_cmds;
  2003. session->cmds_max = total_cmds;
  2004. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  2005. session->exp_cmdsn = initial_cmdsn + 1;
  2006. session->max_cmdsn = initial_cmdsn + 1;
  2007. session->max_r2t = 1;
  2008. session->tt = iscsit;
  2009. mutex_init(&session->eh_mutex);
  2010. spin_lock_init(&session->lock);
  2011. /* initialize SCSI PDU commands pool */
  2012. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  2013. (void***)&session->cmds,
  2014. cmd_task_size + sizeof(struct iscsi_task)))
  2015. goto cmdpool_alloc_fail;
  2016. /* pre-format cmds pool with ITT */
  2017. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  2018. struct iscsi_task *task = session->cmds[cmd_i];
  2019. if (cmd_task_size)
  2020. task->dd_data = &task[1];
  2021. task->itt = cmd_i;
  2022. task->state = ISCSI_TASK_FREE;
  2023. INIT_LIST_HEAD(&task->running);
  2024. }
  2025. if (!try_module_get(iscsit->owner))
  2026. goto module_get_fail;
  2027. if (iscsi_add_session(cls_session, id))
  2028. goto cls_session_fail;
  2029. return cls_session;
  2030. cls_session_fail:
  2031. module_put(iscsit->owner);
  2032. module_get_fail:
  2033. iscsi_pool_free(&session->cmdpool);
  2034. cmdpool_alloc_fail:
  2035. iscsi_free_session(cls_session);
  2036. dec_session_count:
  2037. iscsi_host_dec_session_cnt(shost);
  2038. return NULL;
  2039. }
  2040. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  2041. /**
  2042. * iscsi_session_teardown - destroy session, host, and cls_session
  2043. * @cls_session: iscsi session
  2044. *
  2045. * The driver must have called iscsi_remove_session before
  2046. * calling this.
  2047. */
  2048. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  2049. {
  2050. struct iscsi_session *session = cls_session->dd_data;
  2051. struct module *owner = cls_session->transport->owner;
  2052. struct Scsi_Host *shost = session->host;
  2053. iscsi_pool_free(&session->cmdpool);
  2054. kfree(session->password);
  2055. kfree(session->password_in);
  2056. kfree(session->username);
  2057. kfree(session->username_in);
  2058. kfree(session->targetname);
  2059. kfree(session->initiatorname);
  2060. kfree(session->ifacename);
  2061. iscsi_destroy_session(cls_session);
  2062. iscsi_host_dec_session_cnt(shost);
  2063. module_put(owner);
  2064. }
  2065. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  2066. /**
  2067. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  2068. * @cls_session: iscsi_cls_session
  2069. * @dd_size: private driver data size
  2070. * @conn_idx: cid
  2071. */
  2072. struct iscsi_cls_conn *
  2073. iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
  2074. uint32_t conn_idx)
  2075. {
  2076. struct iscsi_session *session = cls_session->dd_data;
  2077. struct iscsi_conn *conn;
  2078. struct iscsi_cls_conn *cls_conn;
  2079. char *data;
  2080. cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
  2081. conn_idx);
  2082. if (!cls_conn)
  2083. return NULL;
  2084. conn = cls_conn->dd_data;
  2085. memset(conn, 0, sizeof(*conn) + dd_size);
  2086. conn->dd_data = cls_conn->dd_data + sizeof(*conn);
  2087. conn->session = session;
  2088. conn->cls_conn = cls_conn;
  2089. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  2090. conn->id = conn_idx;
  2091. conn->exp_statsn = 0;
  2092. conn->tmf_state = TMF_INITIAL;
  2093. init_timer(&conn->transport_timer);
  2094. conn->transport_timer.data = (unsigned long)conn;
  2095. conn->transport_timer.function = iscsi_check_transport_timeouts;
  2096. INIT_LIST_HEAD(&conn->mgmtqueue);
  2097. INIT_LIST_HEAD(&conn->cmdqueue);
  2098. INIT_LIST_HEAD(&conn->requeue);
  2099. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  2100. /* allocate login_task used for the login/text sequences */
  2101. spin_lock_bh(&session->lock);
  2102. if (!__kfifo_get(session->cmdpool.queue,
  2103. (void*)&conn->login_task,
  2104. sizeof(void*))) {
  2105. spin_unlock_bh(&session->lock);
  2106. goto login_task_alloc_fail;
  2107. }
  2108. spin_unlock_bh(&session->lock);
  2109. data = (char *) __get_free_pages(GFP_KERNEL,
  2110. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2111. if (!data)
  2112. goto login_task_data_alloc_fail;
  2113. conn->login_task->data = conn->data = data;
  2114. init_timer(&conn->tmf_timer);
  2115. init_waitqueue_head(&conn->ehwait);
  2116. return cls_conn;
  2117. login_task_data_alloc_fail:
  2118. __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
  2119. sizeof(void*));
  2120. login_task_alloc_fail:
  2121. iscsi_destroy_conn(cls_conn);
  2122. return NULL;
  2123. }
  2124. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  2125. /**
  2126. * iscsi_conn_teardown - teardown iscsi connection
  2127. * cls_conn: iscsi class connection
  2128. *
  2129. * TODO: we may need to make this into a two step process
  2130. * like scsi-mls remove + put host
  2131. */
  2132. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  2133. {
  2134. struct iscsi_conn *conn = cls_conn->dd_data;
  2135. struct iscsi_session *session = conn->session;
  2136. unsigned long flags;
  2137. del_timer_sync(&conn->transport_timer);
  2138. spin_lock_bh(&session->lock);
  2139. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  2140. if (session->leadconn == conn) {
  2141. /*
  2142. * leading connection? then give up on recovery.
  2143. */
  2144. session->state = ISCSI_STATE_TERMINATE;
  2145. wake_up(&conn->ehwait);
  2146. }
  2147. spin_unlock_bh(&session->lock);
  2148. /*
  2149. * Block until all in-progress commands for this connection
  2150. * time out or fail.
  2151. */
  2152. for (;;) {
  2153. spin_lock_irqsave(session->host->host_lock, flags);
  2154. if (!session->host->host_busy) { /* OK for ERL == 0 */
  2155. spin_unlock_irqrestore(session->host->host_lock, flags);
  2156. break;
  2157. }
  2158. spin_unlock_irqrestore(session->host->host_lock, flags);
  2159. msleep_interruptible(500);
  2160. iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
  2161. "host_busy %d host_failed %d\n",
  2162. session->host->host_busy,
  2163. session->host->host_failed);
  2164. /*
  2165. * force eh_abort() to unblock
  2166. */
  2167. wake_up(&conn->ehwait);
  2168. }
  2169. /* flush queued up work because we free the connection below */
  2170. iscsi_suspend_tx(conn);
  2171. spin_lock_bh(&session->lock);
  2172. free_pages((unsigned long) conn->data,
  2173. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2174. kfree(conn->persistent_address);
  2175. __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
  2176. sizeof(void*));
  2177. if (session->leadconn == conn)
  2178. session->leadconn = NULL;
  2179. spin_unlock_bh(&session->lock);
  2180. iscsi_destroy_conn(cls_conn);
  2181. }
  2182. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  2183. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  2184. {
  2185. struct iscsi_conn *conn = cls_conn->dd_data;
  2186. struct iscsi_session *session = conn->session;
  2187. if (!session) {
  2188. iscsi_conn_printk(KERN_ERR, conn,
  2189. "can't start unbound connection\n");
  2190. return -EPERM;
  2191. }
  2192. if ((session->imm_data_en || !session->initial_r2t_en) &&
  2193. session->first_burst > session->max_burst) {
  2194. iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
  2195. "first_burst %d max_burst %d\n",
  2196. session->first_burst, session->max_burst);
  2197. return -EINVAL;
  2198. }
  2199. if (conn->ping_timeout && !conn->recv_timeout) {
  2200. iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
  2201. "zero. Using 5 seconds\n.");
  2202. conn->recv_timeout = 5;
  2203. }
  2204. if (conn->recv_timeout && !conn->ping_timeout) {
  2205. iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
  2206. "zero. Using 5 seconds.\n");
  2207. conn->ping_timeout = 5;
  2208. }
  2209. spin_lock_bh(&session->lock);
  2210. conn->c_stage = ISCSI_CONN_STARTED;
  2211. session->state = ISCSI_STATE_LOGGED_IN;
  2212. session->queued_cmdsn = session->cmdsn;
  2213. conn->last_recv = jiffies;
  2214. conn->last_ping = jiffies;
  2215. if (conn->recv_timeout && conn->ping_timeout)
  2216. mod_timer(&conn->transport_timer,
  2217. jiffies + (conn->recv_timeout * HZ));
  2218. switch(conn->stop_stage) {
  2219. case STOP_CONN_RECOVER:
  2220. /*
  2221. * unblock eh_abort() if it is blocked. re-try all
  2222. * commands after successful recovery
  2223. */
  2224. conn->stop_stage = 0;
  2225. conn->tmf_state = TMF_INITIAL;
  2226. session->age++;
  2227. if (session->age == 16)
  2228. session->age = 0;
  2229. break;
  2230. case STOP_CONN_TERM:
  2231. conn->stop_stage = 0;
  2232. break;
  2233. default:
  2234. break;
  2235. }
  2236. spin_unlock_bh(&session->lock);
  2237. iscsi_unblock_session(session->cls_session);
  2238. wake_up(&conn->ehwait);
  2239. return 0;
  2240. }
  2241. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  2242. static void
  2243. fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
  2244. {
  2245. struct iscsi_task *task;
  2246. int i;
  2247. for (i = 0; i < conn->session->cmds_max; i++) {
  2248. task = conn->session->cmds[i];
  2249. if (task->sc)
  2250. continue;
  2251. if (task->state == ISCSI_TASK_FREE)
  2252. continue;
  2253. ISCSI_DBG_SESSION(conn->session,
  2254. "failing mgmt itt 0x%x state %d\n",
  2255. task->itt, task->state);
  2256. iscsi_complete_task(task);
  2257. }
  2258. }
  2259. static void iscsi_start_session_recovery(struct iscsi_session *session,
  2260. struct iscsi_conn *conn, int flag)
  2261. {
  2262. int old_stop_stage;
  2263. mutex_lock(&session->eh_mutex);
  2264. spin_lock_bh(&session->lock);
  2265. if (conn->stop_stage == STOP_CONN_TERM) {
  2266. spin_unlock_bh(&session->lock);
  2267. mutex_unlock(&session->eh_mutex);
  2268. return;
  2269. }
  2270. /*
  2271. * When this is called for the in_login state, we only want to clean
  2272. * up the login task and connection. We do not need to block and set
  2273. * the recovery state again
  2274. */
  2275. if (flag == STOP_CONN_TERM)
  2276. session->state = ISCSI_STATE_TERMINATE;
  2277. else if (conn->stop_stage != STOP_CONN_RECOVER)
  2278. session->state = ISCSI_STATE_IN_RECOVERY;
  2279. spin_unlock_bh(&session->lock);
  2280. del_timer_sync(&conn->transport_timer);
  2281. iscsi_suspend_tx(conn);
  2282. spin_lock_bh(&session->lock);
  2283. old_stop_stage = conn->stop_stage;
  2284. conn->stop_stage = flag;
  2285. conn->c_stage = ISCSI_CONN_STOPPED;
  2286. spin_unlock_bh(&session->lock);
  2287. /*
  2288. * for connection level recovery we should not calculate
  2289. * header digest. conn->hdr_size used for optimization
  2290. * in hdr_extract() and will be re-negotiated at
  2291. * set_param() time.
  2292. */
  2293. if (flag == STOP_CONN_RECOVER) {
  2294. conn->hdrdgst_en = 0;
  2295. conn->datadgst_en = 0;
  2296. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  2297. old_stop_stage != STOP_CONN_RECOVER) {
  2298. ISCSI_DBG_SESSION(session, "blocking session\n");
  2299. iscsi_block_session(session->cls_session);
  2300. }
  2301. }
  2302. /*
  2303. * flush queues.
  2304. */
  2305. spin_lock_bh(&session->lock);
  2306. if (flag == STOP_CONN_RECOVER)
  2307. fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
  2308. else
  2309. fail_scsi_tasks(conn, -1, DID_ERROR);
  2310. fail_mgmt_tasks(session, conn);
  2311. spin_unlock_bh(&session->lock);
  2312. mutex_unlock(&session->eh_mutex);
  2313. }
  2314. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  2315. {
  2316. struct iscsi_conn *conn = cls_conn->dd_data;
  2317. struct iscsi_session *session = conn->session;
  2318. switch (flag) {
  2319. case STOP_CONN_RECOVER:
  2320. case STOP_CONN_TERM:
  2321. iscsi_start_session_recovery(session, conn, flag);
  2322. break;
  2323. default:
  2324. iscsi_conn_printk(KERN_ERR, conn,
  2325. "invalid stop flag %d\n", flag);
  2326. }
  2327. }
  2328. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  2329. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  2330. struct iscsi_cls_conn *cls_conn, int is_leading)
  2331. {
  2332. struct iscsi_session *session = cls_session->dd_data;
  2333. struct iscsi_conn *conn = cls_conn->dd_data;
  2334. spin_lock_bh(&session->lock);
  2335. if (is_leading)
  2336. session->leadconn = conn;
  2337. spin_unlock_bh(&session->lock);
  2338. /*
  2339. * Unblock xmitworker(), Login Phase will pass through.
  2340. */
  2341. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  2342. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  2343. return 0;
  2344. }
  2345. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  2346. static int iscsi_switch_str_param(char **param, char *new_val_buf)
  2347. {
  2348. char *new_val;
  2349. if (*param) {
  2350. if (!strcmp(*param, new_val_buf))
  2351. return 0;
  2352. }
  2353. new_val = kstrdup(new_val_buf, GFP_NOIO);
  2354. if (!new_val)
  2355. return -ENOMEM;
  2356. kfree(*param);
  2357. *param = new_val;
  2358. return 0;
  2359. }
  2360. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  2361. enum iscsi_param param, char *buf, int buflen)
  2362. {
  2363. struct iscsi_conn *conn = cls_conn->dd_data;
  2364. struct iscsi_session *session = conn->session;
  2365. uint32_t value;
  2366. switch(param) {
  2367. case ISCSI_PARAM_FAST_ABORT:
  2368. sscanf(buf, "%d", &session->fast_abort);
  2369. break;
  2370. case ISCSI_PARAM_ABORT_TMO:
  2371. sscanf(buf, "%d", &session->abort_timeout);
  2372. break;
  2373. case ISCSI_PARAM_LU_RESET_TMO:
  2374. sscanf(buf, "%d", &session->lu_reset_timeout);
  2375. break;
  2376. case ISCSI_PARAM_PING_TMO:
  2377. sscanf(buf, "%d", &conn->ping_timeout);
  2378. break;
  2379. case ISCSI_PARAM_RECV_TMO:
  2380. sscanf(buf, "%d", &conn->recv_timeout);
  2381. break;
  2382. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2383. sscanf(buf, "%d", &conn->max_recv_dlength);
  2384. break;
  2385. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2386. sscanf(buf, "%d", &conn->max_xmit_dlength);
  2387. break;
  2388. case ISCSI_PARAM_HDRDGST_EN:
  2389. sscanf(buf, "%d", &conn->hdrdgst_en);
  2390. break;
  2391. case ISCSI_PARAM_DATADGST_EN:
  2392. sscanf(buf, "%d", &conn->datadgst_en);
  2393. break;
  2394. case ISCSI_PARAM_INITIAL_R2T_EN:
  2395. sscanf(buf, "%d", &session->initial_r2t_en);
  2396. break;
  2397. case ISCSI_PARAM_MAX_R2T:
  2398. sscanf(buf, "%d", &session->max_r2t);
  2399. break;
  2400. case ISCSI_PARAM_IMM_DATA_EN:
  2401. sscanf(buf, "%d", &session->imm_data_en);
  2402. break;
  2403. case ISCSI_PARAM_FIRST_BURST:
  2404. sscanf(buf, "%d", &session->first_burst);
  2405. break;
  2406. case ISCSI_PARAM_MAX_BURST:
  2407. sscanf(buf, "%d", &session->max_burst);
  2408. break;
  2409. case ISCSI_PARAM_PDU_INORDER_EN:
  2410. sscanf(buf, "%d", &session->pdu_inorder_en);
  2411. break;
  2412. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2413. sscanf(buf, "%d", &session->dataseq_inorder_en);
  2414. break;
  2415. case ISCSI_PARAM_ERL:
  2416. sscanf(buf, "%d", &session->erl);
  2417. break;
  2418. case ISCSI_PARAM_IFMARKER_EN:
  2419. sscanf(buf, "%d", &value);
  2420. BUG_ON(value);
  2421. break;
  2422. case ISCSI_PARAM_OFMARKER_EN:
  2423. sscanf(buf, "%d", &value);
  2424. BUG_ON(value);
  2425. break;
  2426. case ISCSI_PARAM_EXP_STATSN:
  2427. sscanf(buf, "%u", &conn->exp_statsn);
  2428. break;
  2429. case ISCSI_PARAM_USERNAME:
  2430. return iscsi_switch_str_param(&session->username, buf);
  2431. case ISCSI_PARAM_USERNAME_IN:
  2432. return iscsi_switch_str_param(&session->username_in, buf);
  2433. case ISCSI_PARAM_PASSWORD:
  2434. return iscsi_switch_str_param(&session->password, buf);
  2435. case ISCSI_PARAM_PASSWORD_IN:
  2436. return iscsi_switch_str_param(&session->password_in, buf);
  2437. case ISCSI_PARAM_TARGET_NAME:
  2438. return iscsi_switch_str_param(&session->targetname, buf);
  2439. case ISCSI_PARAM_TPGT:
  2440. sscanf(buf, "%d", &session->tpgt);
  2441. break;
  2442. case ISCSI_PARAM_PERSISTENT_PORT:
  2443. sscanf(buf, "%d", &conn->persistent_port);
  2444. break;
  2445. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2446. return iscsi_switch_str_param(&conn->persistent_address, buf);
  2447. case ISCSI_PARAM_IFACE_NAME:
  2448. return iscsi_switch_str_param(&session->ifacename, buf);
  2449. case ISCSI_PARAM_INITIATOR_NAME:
  2450. return iscsi_switch_str_param(&session->initiatorname, buf);
  2451. default:
  2452. return -ENOSYS;
  2453. }
  2454. return 0;
  2455. }
  2456. EXPORT_SYMBOL_GPL(iscsi_set_param);
  2457. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  2458. enum iscsi_param param, char *buf)
  2459. {
  2460. struct iscsi_session *session = cls_session->dd_data;
  2461. int len;
  2462. switch(param) {
  2463. case ISCSI_PARAM_FAST_ABORT:
  2464. len = sprintf(buf, "%d\n", session->fast_abort);
  2465. break;
  2466. case ISCSI_PARAM_ABORT_TMO:
  2467. len = sprintf(buf, "%d\n", session->abort_timeout);
  2468. break;
  2469. case ISCSI_PARAM_LU_RESET_TMO:
  2470. len = sprintf(buf, "%d\n", session->lu_reset_timeout);
  2471. break;
  2472. case ISCSI_PARAM_INITIAL_R2T_EN:
  2473. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  2474. break;
  2475. case ISCSI_PARAM_MAX_R2T:
  2476. len = sprintf(buf, "%hu\n", session->max_r2t);
  2477. break;
  2478. case ISCSI_PARAM_IMM_DATA_EN:
  2479. len = sprintf(buf, "%d\n", session->imm_data_en);
  2480. break;
  2481. case ISCSI_PARAM_FIRST_BURST:
  2482. len = sprintf(buf, "%u\n", session->first_burst);
  2483. break;
  2484. case ISCSI_PARAM_MAX_BURST:
  2485. len = sprintf(buf, "%u\n", session->max_burst);
  2486. break;
  2487. case ISCSI_PARAM_PDU_INORDER_EN:
  2488. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  2489. break;
  2490. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2491. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  2492. break;
  2493. case ISCSI_PARAM_ERL:
  2494. len = sprintf(buf, "%d\n", session->erl);
  2495. break;
  2496. case ISCSI_PARAM_TARGET_NAME:
  2497. len = sprintf(buf, "%s\n", session->targetname);
  2498. break;
  2499. case ISCSI_PARAM_TPGT:
  2500. len = sprintf(buf, "%d\n", session->tpgt);
  2501. break;
  2502. case ISCSI_PARAM_USERNAME:
  2503. len = sprintf(buf, "%s\n", session->username);
  2504. break;
  2505. case ISCSI_PARAM_USERNAME_IN:
  2506. len = sprintf(buf, "%s\n", session->username_in);
  2507. break;
  2508. case ISCSI_PARAM_PASSWORD:
  2509. len = sprintf(buf, "%s\n", session->password);
  2510. break;
  2511. case ISCSI_PARAM_PASSWORD_IN:
  2512. len = sprintf(buf, "%s\n", session->password_in);
  2513. break;
  2514. case ISCSI_PARAM_IFACE_NAME:
  2515. len = sprintf(buf, "%s\n", session->ifacename);
  2516. break;
  2517. case ISCSI_PARAM_INITIATOR_NAME:
  2518. len = sprintf(buf, "%s\n", session->initiatorname);
  2519. break;
  2520. default:
  2521. return -ENOSYS;
  2522. }
  2523. return len;
  2524. }
  2525. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  2526. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  2527. enum iscsi_param param, char *buf)
  2528. {
  2529. struct iscsi_conn *conn = cls_conn->dd_data;
  2530. int len;
  2531. switch(param) {
  2532. case ISCSI_PARAM_PING_TMO:
  2533. len = sprintf(buf, "%u\n", conn->ping_timeout);
  2534. break;
  2535. case ISCSI_PARAM_RECV_TMO:
  2536. len = sprintf(buf, "%u\n", conn->recv_timeout);
  2537. break;
  2538. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2539. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  2540. break;
  2541. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2542. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  2543. break;
  2544. case ISCSI_PARAM_HDRDGST_EN:
  2545. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  2546. break;
  2547. case ISCSI_PARAM_DATADGST_EN:
  2548. len = sprintf(buf, "%d\n", conn->datadgst_en);
  2549. break;
  2550. case ISCSI_PARAM_IFMARKER_EN:
  2551. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  2552. break;
  2553. case ISCSI_PARAM_OFMARKER_EN:
  2554. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  2555. break;
  2556. case ISCSI_PARAM_EXP_STATSN:
  2557. len = sprintf(buf, "%u\n", conn->exp_statsn);
  2558. break;
  2559. case ISCSI_PARAM_PERSISTENT_PORT:
  2560. len = sprintf(buf, "%d\n", conn->persistent_port);
  2561. break;
  2562. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2563. len = sprintf(buf, "%s\n", conn->persistent_address);
  2564. break;
  2565. default:
  2566. return -ENOSYS;
  2567. }
  2568. return len;
  2569. }
  2570. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  2571. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2572. char *buf)
  2573. {
  2574. struct iscsi_host *ihost = shost_priv(shost);
  2575. int len;
  2576. switch (param) {
  2577. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2578. len = sprintf(buf, "%s\n", ihost->netdev);
  2579. break;
  2580. case ISCSI_HOST_PARAM_HWADDRESS:
  2581. len = sprintf(buf, "%s\n", ihost->hwaddress);
  2582. break;
  2583. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2584. len = sprintf(buf, "%s\n", ihost->initiatorname);
  2585. break;
  2586. case ISCSI_HOST_PARAM_IPADDRESS:
  2587. len = sprintf(buf, "%s\n", ihost->local_address);
  2588. break;
  2589. default:
  2590. return -ENOSYS;
  2591. }
  2592. return len;
  2593. }
  2594. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  2595. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2596. char *buf, int buflen)
  2597. {
  2598. struct iscsi_host *ihost = shost_priv(shost);
  2599. switch (param) {
  2600. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2601. return iscsi_switch_str_param(&ihost->netdev, buf);
  2602. case ISCSI_HOST_PARAM_HWADDRESS:
  2603. return iscsi_switch_str_param(&ihost->hwaddress, buf);
  2604. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2605. return iscsi_switch_str_param(&ihost->initiatorname, buf);
  2606. default:
  2607. return -ENOSYS;
  2608. }
  2609. return 0;
  2610. }
  2611. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  2612. MODULE_AUTHOR("Mike Christie");
  2613. MODULE_DESCRIPTION("iSCSI library functions");
  2614. MODULE_LICENSE("GPL");