libiscsi.c 75 KB

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