libiscsi.c 77 KB

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