libiscsi.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267
  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 <asm/unaligned.h>
  28. #include <net/tcp.h>
  29. #include <scsi/scsi_cmnd.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_eh.h>
  32. #include <scsi/scsi_tcq.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi.h>
  35. #include <scsi/iscsi_proto.h>
  36. #include <scsi/scsi_transport.h>
  37. #include <scsi/scsi_transport_iscsi.h>
  38. #include <scsi/libiscsi.h>
  39. struct iscsi_session *
  40. class_to_transport_session(struct iscsi_cls_session *cls_session)
  41. {
  42. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  43. return iscsi_hostdata(shost->hostdata);
  44. }
  45. EXPORT_SYMBOL_GPL(class_to_transport_session);
  46. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  47. #define SNA32_CHECK 2147483648UL
  48. static int iscsi_sna_lt(u32 n1, u32 n2)
  49. {
  50. return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  51. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  52. }
  53. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  54. static int iscsi_sna_lte(u32 n1, u32 n2)
  55. {
  56. return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  57. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  58. }
  59. void
  60. iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  61. {
  62. uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
  63. uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
  64. /*
  65. * standard specifies this check for when to update expected and
  66. * max sequence numbers
  67. */
  68. if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
  69. return;
  70. if (exp_cmdsn != session->exp_cmdsn &&
  71. !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
  72. session->exp_cmdsn = exp_cmdsn;
  73. if (max_cmdsn != session->max_cmdsn &&
  74. !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
  75. session->max_cmdsn = max_cmdsn;
  76. /*
  77. * if the window closed with IO queued, then kick the
  78. * xmit thread
  79. */
  80. if (!list_empty(&session->leadconn->xmitqueue) ||
  81. !list_empty(&session->leadconn->mgmtqueue))
  82. scsi_queue_work(session->host,
  83. &session->leadconn->xmitwork);
  84. }
  85. }
  86. EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
  87. void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
  88. struct iscsi_data *hdr)
  89. {
  90. struct iscsi_conn *conn = ctask->conn;
  91. memset(hdr, 0, sizeof(struct iscsi_data));
  92. hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  93. hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
  94. ctask->unsol_datasn++;
  95. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  96. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  97. hdr->itt = ctask->hdr->itt;
  98. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  99. hdr->offset = cpu_to_be32(ctask->unsol_offset);
  100. if (ctask->unsol_count > conn->max_xmit_dlength) {
  101. hton24(hdr->dlength, conn->max_xmit_dlength);
  102. ctask->data_count = conn->max_xmit_dlength;
  103. ctask->unsol_offset += ctask->data_count;
  104. hdr->flags = 0;
  105. } else {
  106. hton24(hdr->dlength, ctask->unsol_count);
  107. ctask->data_count = ctask->unsol_count;
  108. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  109. }
  110. }
  111. EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
  112. static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
  113. {
  114. unsigned exp_len = ctask->hdr_len + len;
  115. if (exp_len > ctask->hdr_max) {
  116. WARN_ON(1);
  117. return -EINVAL;
  118. }
  119. WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
  120. ctask->hdr_len = exp_len;
  121. return 0;
  122. }
  123. /**
  124. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  125. * @ctask: iscsi cmd task
  126. *
  127. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  128. * fields like dlength or final based on how much data it sends
  129. */
  130. static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
  131. {
  132. struct iscsi_conn *conn = ctask->conn;
  133. struct iscsi_session *session = conn->session;
  134. struct iscsi_cmd *hdr = ctask->hdr;
  135. struct scsi_cmnd *sc = ctask->sc;
  136. unsigned hdrlength;
  137. int rc;
  138. ctask->hdr_len = 0;
  139. rc = iscsi_add_hdr(ctask, sizeof(*hdr));
  140. if (rc)
  141. return rc;
  142. hdr->opcode = ISCSI_OP_SCSI_CMD;
  143. hdr->flags = ISCSI_ATTR_SIMPLE;
  144. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  145. hdr->itt = build_itt(ctask->itt, conn->id, session->age);
  146. hdr->data_length = cpu_to_be32(scsi_bufflen(sc));
  147. hdr->cmdsn = cpu_to_be32(session->cmdsn);
  148. session->cmdsn++;
  149. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  150. memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
  151. if (sc->cmd_len < MAX_COMMAND_SIZE)
  152. memset(&hdr->cdb[sc->cmd_len], 0,
  153. MAX_COMMAND_SIZE - sc->cmd_len);
  154. ctask->data_count = 0;
  155. ctask->imm_count = 0;
  156. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  157. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  158. /*
  159. * Write counters:
  160. *
  161. * imm_count bytes to be sent right after
  162. * SCSI PDU Header
  163. *
  164. * unsol_count bytes(as Data-Out) to be sent
  165. * without R2T ack right after
  166. * immediate data
  167. *
  168. * r2t_data_count bytes to be sent via R2T ack's
  169. *
  170. * pad_count bytes to be sent as zero-padding
  171. */
  172. ctask->unsol_count = 0;
  173. ctask->unsol_offset = 0;
  174. ctask->unsol_datasn = 0;
  175. if (session->imm_data_en) {
  176. if (scsi_bufflen(sc) >= session->first_burst)
  177. ctask->imm_count = min(session->first_burst,
  178. conn->max_xmit_dlength);
  179. else
  180. ctask->imm_count = min(scsi_bufflen(sc),
  181. conn->max_xmit_dlength);
  182. hton24(ctask->hdr->dlength, ctask->imm_count);
  183. } else
  184. zero_data(ctask->hdr->dlength);
  185. if (!session->initial_r2t_en) {
  186. ctask->unsol_count = min((session->first_burst),
  187. (scsi_bufflen(sc))) - ctask->imm_count;
  188. ctask->unsol_offset = ctask->imm_count;
  189. }
  190. if (!ctask->unsol_count)
  191. /* No unsolicit Data-Out's */
  192. ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  193. } else {
  194. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  195. zero_data(hdr->dlength);
  196. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  197. hdr->flags |= ISCSI_FLAG_CMD_READ;
  198. }
  199. /* calculate size of additional header segments (AHSs) */
  200. hdrlength = ctask->hdr_len - sizeof(*hdr);
  201. WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
  202. hdrlength /= ISCSI_PAD_LEN;
  203. WARN_ON(hdrlength >= 256);
  204. hdr->hlength = hdrlength & 0xFF;
  205. conn->scsicmd_pdus_cnt++;
  206. debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
  207. "cmdsn %d win %d]\n",
  208. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  209. conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc),
  210. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  211. return 0;
  212. }
  213. /**
  214. * iscsi_complete_command - return command back to scsi-ml
  215. * @ctask: iscsi cmd task
  216. *
  217. * Must be called with session lock.
  218. * This function returns the scsi command to scsi-ml and returns
  219. * the cmd task to the pool of available cmd tasks.
  220. */
  221. static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
  222. {
  223. struct iscsi_session *session = ctask->conn->session;
  224. struct scsi_cmnd *sc = ctask->sc;
  225. ctask->state = ISCSI_TASK_COMPLETED;
  226. ctask->sc = NULL;
  227. /* SCSI eh reuses commands to verify us */
  228. sc->SCp.ptr = NULL;
  229. list_del_init(&ctask->running);
  230. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  231. sc->scsi_done(sc);
  232. }
  233. static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  234. {
  235. atomic_inc(&ctask->refcount);
  236. }
  237. static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  238. {
  239. if (atomic_dec_and_test(&ctask->refcount))
  240. iscsi_complete_command(ctask);
  241. }
  242. /*
  243. * session lock must be held
  244. */
  245. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  246. int err)
  247. {
  248. struct scsi_cmnd *sc;
  249. sc = ctask->sc;
  250. if (!sc)
  251. return;
  252. if (ctask->state == ISCSI_TASK_PENDING)
  253. /*
  254. * cmd never made it to the xmit thread, so we should not count
  255. * the cmd in the sequencing
  256. */
  257. conn->session->queued_cmdsn--;
  258. else
  259. conn->session->tt->cleanup_cmd_task(conn, ctask);
  260. sc->result = err;
  261. scsi_set_resid(sc, scsi_bufflen(sc));
  262. if (conn->ctask == ctask)
  263. conn->ctask = NULL;
  264. /* release ref from queuecommand */
  265. __iscsi_put_ctask(ctask);
  266. }
  267. /**
  268. * iscsi_free_mgmt_task - return mgmt task back to pool
  269. * @conn: iscsi connection
  270. * @mtask: mtask
  271. *
  272. * Must be called with session lock.
  273. */
  274. void iscsi_free_mgmt_task(struct iscsi_conn *conn,
  275. struct iscsi_mgmt_task *mtask)
  276. {
  277. list_del_init(&mtask->running);
  278. if (conn->login_mtask == mtask)
  279. return;
  280. __kfifo_put(conn->session->mgmtpool.queue,
  281. (void*)&mtask, sizeof(void*));
  282. }
  283. EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
  284. /**
  285. * iscsi_cmd_rsp - SCSI Command Response processing
  286. * @conn: iscsi connection
  287. * @hdr: iscsi header
  288. * @ctask: scsi command task
  289. * @data: cmd data buffer
  290. * @datalen: len of buffer
  291. *
  292. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  293. * then completes the command and task.
  294. **/
  295. static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  296. struct iscsi_cmd_task *ctask, char *data,
  297. int datalen)
  298. {
  299. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  300. struct iscsi_session *session = conn->session;
  301. struct scsi_cmnd *sc = ctask->sc;
  302. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  303. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  304. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  305. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  306. sc->result = DID_ERROR << 16;
  307. goto out;
  308. }
  309. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  310. uint16_t senselen;
  311. if (datalen < 2) {
  312. invalid_datalen:
  313. printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
  314. "invalid data buffer size of %d\n", datalen);
  315. sc->result = DID_BAD_TARGET << 16;
  316. goto out;
  317. }
  318. senselen = be16_to_cpu(get_unaligned((__be16 *) data));
  319. if (datalen < senselen)
  320. goto invalid_datalen;
  321. memcpy(sc->sense_buffer, data + 2,
  322. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  323. debug_scsi("copied %d bytes of sense\n",
  324. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  325. }
  326. if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
  327. ISCSI_FLAG_CMD_OVERFLOW)) {
  328. int res_count = be32_to_cpu(rhdr->residual_count);
  329. if (res_count > 0 &&
  330. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  331. res_count <= scsi_bufflen(sc)))
  332. scsi_set_resid(sc, res_count);
  333. else
  334. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  335. } else if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
  336. ISCSI_FLAG_CMD_BIDI_OVERFLOW))
  337. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  338. out:
  339. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  340. (long)sc, sc->result, ctask->itt);
  341. conn->scsirsp_pdus_cnt++;
  342. __iscsi_put_ctask(ctask);
  343. }
  344. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  345. {
  346. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  347. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  348. conn->tmfrsp_pdus_cnt++;
  349. if (conn->tmf_state != TMF_QUEUED)
  350. return;
  351. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  352. conn->tmf_state = TMF_SUCCESS;
  353. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  354. conn->tmf_state = TMF_NOT_FOUND;
  355. else
  356. conn->tmf_state = TMF_FAILED;
  357. wake_up(&conn->ehwait);
  358. }
  359. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  360. char *data, int datalen)
  361. {
  362. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  363. struct iscsi_hdr rejected_pdu;
  364. uint32_t itt;
  365. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  366. if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
  367. if (ntoh24(reject->dlength) > datalen)
  368. return ISCSI_ERR_PROTO;
  369. if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
  370. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  371. itt = get_itt(rejected_pdu.itt);
  372. printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
  373. "due to DataDigest error.\n", itt,
  374. rejected_pdu.opcode);
  375. }
  376. }
  377. return 0;
  378. }
  379. /**
  380. * __iscsi_complete_pdu - complete pdu
  381. * @conn: iscsi conn
  382. * @hdr: iscsi header
  383. * @data: data buffer
  384. * @datalen: len of data buffer
  385. *
  386. * Completes pdu processing by freeing any resources allocated at
  387. * queuecommand or send generic. session lock must be held and verify
  388. * itt must have been called.
  389. */
  390. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  391. char *data, int datalen)
  392. {
  393. struct iscsi_session *session = conn->session;
  394. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  395. struct iscsi_cmd_task *ctask;
  396. struct iscsi_mgmt_task *mtask;
  397. uint32_t itt;
  398. if (hdr->itt != RESERVED_ITT)
  399. itt = get_itt(hdr->itt);
  400. else
  401. itt = ~0U;
  402. if (itt < session->cmds_max) {
  403. ctask = session->cmds[itt];
  404. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  405. opcode, conn->id, ctask->itt, datalen);
  406. switch(opcode) {
  407. case ISCSI_OP_SCSI_CMD_RSP:
  408. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  409. iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  410. datalen);
  411. break;
  412. case ISCSI_OP_SCSI_DATA_IN:
  413. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  414. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  415. conn->scsirsp_pdus_cnt++;
  416. __iscsi_put_ctask(ctask);
  417. }
  418. break;
  419. case ISCSI_OP_R2T:
  420. /* LLD handles this for now */
  421. break;
  422. default:
  423. rc = ISCSI_ERR_BAD_OPCODE;
  424. break;
  425. }
  426. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  427. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  428. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  429. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  430. opcode, conn->id, mtask->itt, datalen);
  431. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  432. switch(opcode) {
  433. case ISCSI_OP_LOGOUT_RSP:
  434. if (datalen) {
  435. rc = ISCSI_ERR_PROTO;
  436. break;
  437. }
  438. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  439. /* fall through */
  440. case ISCSI_OP_LOGIN_RSP:
  441. case ISCSI_OP_TEXT_RSP:
  442. /*
  443. * login related PDU's exp_statsn is handled in
  444. * userspace
  445. */
  446. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  447. rc = ISCSI_ERR_CONN_FAILED;
  448. iscsi_free_mgmt_task(conn, mtask);
  449. break;
  450. case ISCSI_OP_SCSI_TMFUNC_RSP:
  451. if (datalen) {
  452. rc = ISCSI_ERR_PROTO;
  453. break;
  454. }
  455. iscsi_tmf_rsp(conn, hdr);
  456. iscsi_free_mgmt_task(conn, mtask);
  457. break;
  458. case ISCSI_OP_NOOP_IN:
  459. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
  460. rc = ISCSI_ERR_PROTO;
  461. break;
  462. }
  463. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  464. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  465. rc = ISCSI_ERR_CONN_FAILED;
  466. iscsi_free_mgmt_task(conn, mtask);
  467. break;
  468. default:
  469. rc = ISCSI_ERR_BAD_OPCODE;
  470. break;
  471. }
  472. } else if (itt == ~0U) {
  473. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  474. switch(opcode) {
  475. case ISCSI_OP_NOOP_IN:
  476. if (datalen) {
  477. rc = ISCSI_ERR_PROTO;
  478. break;
  479. }
  480. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  481. break;
  482. if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
  483. rc = ISCSI_ERR_CONN_FAILED;
  484. break;
  485. case ISCSI_OP_REJECT:
  486. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  487. break;
  488. case ISCSI_OP_ASYNC_EVENT:
  489. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  490. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  491. rc = ISCSI_ERR_CONN_FAILED;
  492. break;
  493. default:
  494. rc = ISCSI_ERR_BAD_OPCODE;
  495. break;
  496. }
  497. } else
  498. rc = ISCSI_ERR_BAD_ITT;
  499. return rc;
  500. }
  501. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  502. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  503. char *data, int datalen)
  504. {
  505. int rc;
  506. spin_lock(&conn->session->lock);
  507. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  508. spin_unlock(&conn->session->lock);
  509. return rc;
  510. }
  511. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  512. /* verify itt (itt encoding: age+cid+itt) */
  513. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  514. uint32_t *ret_itt)
  515. {
  516. struct iscsi_session *session = conn->session;
  517. struct iscsi_cmd_task *ctask;
  518. uint32_t itt;
  519. if (hdr->itt != RESERVED_ITT) {
  520. if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
  521. (session->age << ISCSI_AGE_SHIFT)) {
  522. printk(KERN_ERR "iscsi: received itt %x expected "
  523. "session age (%x)\n", (__force u32)hdr->itt,
  524. session->age & ISCSI_AGE_MASK);
  525. return ISCSI_ERR_BAD_ITT;
  526. }
  527. if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
  528. (conn->id << ISCSI_CID_SHIFT)) {
  529. printk(KERN_ERR "iscsi: received itt %x, expected "
  530. "CID (%x)\n", (__force u32)hdr->itt, conn->id);
  531. return ISCSI_ERR_BAD_ITT;
  532. }
  533. itt = get_itt(hdr->itt);
  534. } else
  535. itt = ~0U;
  536. if (itt < session->cmds_max) {
  537. ctask = session->cmds[itt];
  538. if (!ctask->sc) {
  539. printk(KERN_INFO "iscsi: dropping ctask with "
  540. "itt 0x%x\n", ctask->itt);
  541. /* force drop */
  542. return ISCSI_ERR_NO_SCSI_CMD;
  543. }
  544. if (ctask->sc->SCp.phase != session->age) {
  545. printk(KERN_ERR "iscsi: ctask's session age %d, "
  546. "expected %d\n", ctask->sc->SCp.phase,
  547. session->age);
  548. return ISCSI_ERR_SESSION_FAILED;
  549. }
  550. }
  551. *ret_itt = itt;
  552. return 0;
  553. }
  554. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  555. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  556. {
  557. struct iscsi_session *session = conn->session;
  558. unsigned long flags;
  559. spin_lock_irqsave(&session->lock, flags);
  560. if (session->state == ISCSI_STATE_FAILED) {
  561. spin_unlock_irqrestore(&session->lock, flags);
  562. return;
  563. }
  564. if (conn->stop_stage == 0)
  565. session->state = ISCSI_STATE_FAILED;
  566. spin_unlock_irqrestore(&session->lock, flags);
  567. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  568. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  569. iscsi_conn_error(conn->cls_conn, err);
  570. }
  571. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  572. static void iscsi_prep_mtask(struct iscsi_conn *conn,
  573. struct iscsi_mgmt_task *mtask)
  574. {
  575. struct iscsi_session *session = conn->session;
  576. struct iscsi_hdr *hdr = mtask->hdr;
  577. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  578. if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
  579. hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  580. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  581. /*
  582. * pre-format CmdSN for outgoing PDU.
  583. */
  584. nop->cmdsn = cpu_to_be32(session->cmdsn);
  585. if (hdr->itt != RESERVED_ITT) {
  586. hdr->itt = build_itt(mtask->itt, conn->id, session->age);
  587. /*
  588. * TODO: We always use immediate, so we never hit this.
  589. * If we start to send tmfs or nops as non-immediate then
  590. * we should start checking the cmdsn numbers for mgmt tasks.
  591. */
  592. if (conn->c_stage == ISCSI_CONN_STARTED &&
  593. !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
  594. session->queued_cmdsn++;
  595. session->cmdsn++;
  596. }
  597. }
  598. if (session->tt->init_mgmt_task)
  599. session->tt->init_mgmt_task(conn, mtask);
  600. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  601. hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
  602. mtask->data_count);
  603. }
  604. static int iscsi_xmit_mtask(struct iscsi_conn *conn)
  605. {
  606. struct iscsi_hdr *hdr = conn->mtask->hdr;
  607. int rc;
  608. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  609. conn->session->state = ISCSI_STATE_LOGGING_OUT;
  610. spin_unlock_bh(&conn->session->lock);
  611. rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
  612. spin_lock_bh(&conn->session->lock);
  613. if (rc)
  614. return rc;
  615. /* done with this in-progress mtask */
  616. conn->mtask = NULL;
  617. return 0;
  618. }
  619. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  620. {
  621. struct iscsi_session *session = conn->session;
  622. /*
  623. * Check for iSCSI window and take care of CmdSN wrap-around
  624. */
  625. if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
  626. debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
  627. "CmdSN %u/%u\n", session->exp_cmdsn,
  628. session->max_cmdsn, session->cmdsn,
  629. session->queued_cmdsn);
  630. return -ENOSPC;
  631. }
  632. return 0;
  633. }
  634. static int iscsi_xmit_ctask(struct iscsi_conn *conn)
  635. {
  636. struct iscsi_cmd_task *ctask = conn->ctask;
  637. int rc;
  638. __iscsi_get_ctask(ctask);
  639. spin_unlock_bh(&conn->session->lock);
  640. rc = conn->session->tt->xmit_cmd_task(conn, ctask);
  641. spin_lock_bh(&conn->session->lock);
  642. __iscsi_put_ctask(ctask);
  643. if (!rc)
  644. /* done with this ctask */
  645. conn->ctask = NULL;
  646. return rc;
  647. }
  648. /**
  649. * iscsi_requeue_ctask - requeue ctask to run from session workqueue
  650. * @ctask: ctask to requeue
  651. *
  652. * LLDs that need to run a ctask from the session workqueue should call
  653. * this. The session lock must be held.
  654. */
  655. void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
  656. {
  657. struct iscsi_conn *conn = ctask->conn;
  658. list_move_tail(&ctask->running, &conn->requeue);
  659. scsi_queue_work(conn->session->host, &conn->xmitwork);
  660. }
  661. EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
  662. /**
  663. * iscsi_data_xmit - xmit any command into the scheduled connection
  664. * @conn: iscsi connection
  665. *
  666. * Notes:
  667. * The function can return -EAGAIN in which case the caller must
  668. * re-schedule it again later or recover. '0' return code means
  669. * successful xmit.
  670. **/
  671. static int iscsi_data_xmit(struct iscsi_conn *conn)
  672. {
  673. int rc = 0;
  674. spin_lock_bh(&conn->session->lock);
  675. if (unlikely(conn->suspend_tx)) {
  676. debug_scsi("conn %d Tx suspended!\n", conn->id);
  677. spin_unlock_bh(&conn->session->lock);
  678. return -ENODATA;
  679. }
  680. if (conn->ctask) {
  681. rc = iscsi_xmit_ctask(conn);
  682. if (rc)
  683. goto again;
  684. }
  685. if (conn->mtask) {
  686. rc = iscsi_xmit_mtask(conn);
  687. if (rc)
  688. goto again;
  689. }
  690. /*
  691. * process mgmt pdus like nops before commands since we should
  692. * only have one nop-out as a ping from us and targets should not
  693. * overflow us with nop-ins
  694. */
  695. check_mgmt:
  696. while (!list_empty(&conn->mgmtqueue)) {
  697. conn->mtask = list_entry(conn->mgmtqueue.next,
  698. struct iscsi_mgmt_task, running);
  699. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  700. iscsi_free_mgmt_task(conn, conn->mtask);
  701. conn->mtask = NULL;
  702. continue;
  703. }
  704. iscsi_prep_mtask(conn, conn->mtask);
  705. list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
  706. rc = iscsi_xmit_mtask(conn);
  707. if (rc)
  708. goto again;
  709. }
  710. /* process pending command queue */
  711. while (!list_empty(&conn->xmitqueue)) {
  712. if (conn->tmf_state == TMF_QUEUED)
  713. break;
  714. conn->ctask = list_entry(conn->xmitqueue.next,
  715. struct iscsi_cmd_task, running);
  716. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  717. fail_command(conn, conn->ctask, DID_NO_CONNECT << 16);
  718. continue;
  719. }
  720. if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
  721. fail_command(conn, conn->ctask, DID_ABORT << 16);
  722. continue;
  723. }
  724. conn->session->tt->init_cmd_task(conn->ctask);
  725. conn->ctask->state = ISCSI_TASK_RUNNING;
  726. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  727. rc = iscsi_xmit_ctask(conn);
  728. if (rc)
  729. goto again;
  730. /*
  731. * we could continuously get new ctask requests so
  732. * we need to check the mgmt queue for nops that need to
  733. * be sent to aviod starvation
  734. */
  735. if (!list_empty(&conn->mgmtqueue))
  736. goto check_mgmt;
  737. }
  738. while (!list_empty(&conn->requeue)) {
  739. if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
  740. break;
  741. /*
  742. * we always do fastlogout - conn stop code will clean up.
  743. */
  744. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  745. break;
  746. conn->ctask = list_entry(conn->requeue.next,
  747. struct iscsi_cmd_task, running);
  748. conn->ctask->state = ISCSI_TASK_RUNNING;
  749. list_move_tail(conn->requeue.next, &conn->run_list);
  750. rc = iscsi_xmit_ctask(conn);
  751. if (rc)
  752. goto again;
  753. if (!list_empty(&conn->mgmtqueue))
  754. goto check_mgmt;
  755. }
  756. spin_unlock_bh(&conn->session->lock);
  757. return -ENODATA;
  758. again:
  759. if (unlikely(conn->suspend_tx))
  760. rc = -ENODATA;
  761. spin_unlock_bh(&conn->session->lock);
  762. return rc;
  763. }
  764. static void iscsi_xmitworker(struct work_struct *work)
  765. {
  766. struct iscsi_conn *conn =
  767. container_of(work, struct iscsi_conn, xmitwork);
  768. int rc;
  769. /*
  770. * serialize Xmit worker on a per-connection basis.
  771. */
  772. do {
  773. rc = iscsi_data_xmit(conn);
  774. } while (rc >= 0 || rc == -EAGAIN);
  775. }
  776. enum {
  777. FAILURE_BAD_HOST = 1,
  778. FAILURE_SESSION_FAILED,
  779. FAILURE_SESSION_FREED,
  780. FAILURE_WINDOW_CLOSED,
  781. FAILURE_OOM,
  782. FAILURE_SESSION_TERMINATE,
  783. FAILURE_SESSION_IN_RECOVERY,
  784. FAILURE_SESSION_RECOVERY_TIMEOUT,
  785. FAILURE_SESSION_LOGGING_OUT,
  786. };
  787. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  788. {
  789. struct Scsi_Host *host;
  790. int reason = 0;
  791. struct iscsi_session *session;
  792. struct iscsi_conn *conn;
  793. struct iscsi_cmd_task *ctask = NULL;
  794. sc->scsi_done = done;
  795. sc->result = 0;
  796. sc->SCp.ptr = NULL;
  797. host = sc->device->host;
  798. session = iscsi_hostdata(host->hostdata);
  799. spin_lock(&session->lock);
  800. /*
  801. * ISCSI_STATE_FAILED is a temp. state. The recovery
  802. * code will decide what is best to do with command queued
  803. * during this time
  804. */
  805. if (session->state != ISCSI_STATE_LOGGED_IN &&
  806. session->state != ISCSI_STATE_FAILED) {
  807. /*
  808. * to handle the race between when we set the recovery state
  809. * and block the session we requeue here (commands could
  810. * be entering our queuecommand while a block is starting
  811. * up because the block code is not locked)
  812. */
  813. if (session->state == ISCSI_STATE_IN_RECOVERY) {
  814. reason = FAILURE_SESSION_IN_RECOVERY;
  815. goto reject;
  816. }
  817. switch (session->state) {
  818. case ISCSI_STATE_RECOVERY_FAILED:
  819. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  820. break;
  821. case ISCSI_STATE_TERMINATE:
  822. reason = FAILURE_SESSION_TERMINATE;
  823. break;
  824. case ISCSI_STATE_LOGGING_OUT:
  825. reason = FAILURE_SESSION_LOGGING_OUT;
  826. break;
  827. default:
  828. reason = FAILURE_SESSION_FREED;
  829. }
  830. goto fault;
  831. }
  832. conn = session->leadconn;
  833. if (!conn) {
  834. reason = FAILURE_SESSION_FREED;
  835. goto fault;
  836. }
  837. if (iscsi_check_cmdsn_window_closed(conn)) {
  838. reason = FAILURE_WINDOW_CLOSED;
  839. goto reject;
  840. }
  841. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  842. sizeof(void*))) {
  843. reason = FAILURE_OOM;
  844. goto reject;
  845. }
  846. session->queued_cmdsn++;
  847. sc->SCp.phase = session->age;
  848. sc->SCp.ptr = (char *)ctask;
  849. atomic_set(&ctask->refcount, 1);
  850. ctask->state = ISCSI_TASK_PENDING;
  851. ctask->conn = conn;
  852. ctask->sc = sc;
  853. INIT_LIST_HEAD(&ctask->running);
  854. list_add_tail(&ctask->running, &conn->xmitqueue);
  855. spin_unlock(&session->lock);
  856. scsi_queue_work(host, &conn->xmitwork);
  857. return 0;
  858. reject:
  859. spin_unlock(&session->lock);
  860. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  861. return SCSI_MLQUEUE_HOST_BUSY;
  862. fault:
  863. spin_unlock(&session->lock);
  864. printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
  865. sc->cmnd[0], reason);
  866. sc->result = (DID_NO_CONNECT << 16);
  867. scsi_set_resid(sc, scsi_bufflen(sc));
  868. sc->scsi_done(sc);
  869. return 0;
  870. }
  871. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  872. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  873. {
  874. if (depth > ISCSI_MAX_CMD_PER_LUN)
  875. depth = ISCSI_MAX_CMD_PER_LUN;
  876. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  877. return sdev->queue_depth;
  878. }
  879. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  880. static struct iscsi_mgmt_task *
  881. __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  882. char *data, uint32_t data_size)
  883. {
  884. struct iscsi_session *session = conn->session;
  885. struct iscsi_mgmt_task *mtask;
  886. if (session->state == ISCSI_STATE_TERMINATE)
  887. return NULL;
  888. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  889. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  890. /*
  891. * Login and Text are sent serially, in
  892. * request-followed-by-response sequence.
  893. * Same mtask can be used. Same ITT must be used.
  894. * Note that login_mtask is preallocated at conn_create().
  895. */
  896. mtask = conn->login_mtask;
  897. else {
  898. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  899. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  900. if (!__kfifo_get(session->mgmtpool.queue,
  901. (void*)&mtask, sizeof(void*)))
  902. return NULL;
  903. }
  904. if (data_size) {
  905. memcpy(mtask->data, data, data_size);
  906. mtask->data_count = data_size;
  907. } else
  908. mtask->data_count = 0;
  909. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  910. INIT_LIST_HEAD(&mtask->running);
  911. list_add_tail(&mtask->running, &conn->mgmtqueue);
  912. return mtask;
  913. }
  914. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  915. char *data, uint32_t data_size)
  916. {
  917. struct iscsi_conn *conn = cls_conn->dd_data;
  918. struct iscsi_session *session = conn->session;
  919. int err = 0;
  920. spin_lock_bh(&session->lock);
  921. if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
  922. err = -EPERM;
  923. spin_unlock_bh(&session->lock);
  924. scsi_queue_work(session->host, &conn->xmitwork);
  925. return err;
  926. }
  927. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  928. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  929. {
  930. struct iscsi_session *session = class_to_transport_session(cls_session);
  931. spin_lock_bh(&session->lock);
  932. if (session->state != ISCSI_STATE_LOGGED_IN) {
  933. session->state = ISCSI_STATE_RECOVERY_FAILED;
  934. if (session->leadconn)
  935. wake_up(&session->leadconn->ehwait);
  936. }
  937. spin_unlock_bh(&session->lock);
  938. }
  939. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  940. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  941. {
  942. struct Scsi_Host *host = sc->device->host;
  943. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  944. struct iscsi_conn *conn = session->leadconn;
  945. mutex_lock(&session->eh_mutex);
  946. spin_lock_bh(&session->lock);
  947. if (session->state == ISCSI_STATE_TERMINATE) {
  948. failed:
  949. debug_scsi("failing host reset: session terminated "
  950. "[CID %d age %d]\n", conn->id, session->age);
  951. spin_unlock_bh(&session->lock);
  952. mutex_unlock(&session->eh_mutex);
  953. return FAILED;
  954. }
  955. spin_unlock_bh(&session->lock);
  956. mutex_unlock(&session->eh_mutex);
  957. /*
  958. * we drop the lock here but the leadconn cannot be destoyed while
  959. * we are in the scsi eh
  960. */
  961. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  962. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  963. wait_event_interruptible(conn->ehwait,
  964. session->state == ISCSI_STATE_TERMINATE ||
  965. session->state == ISCSI_STATE_LOGGED_IN ||
  966. session->state == ISCSI_STATE_RECOVERY_FAILED);
  967. if (signal_pending(current))
  968. flush_signals(current);
  969. mutex_lock(&session->eh_mutex);
  970. spin_lock_bh(&session->lock);
  971. if (session->state == ISCSI_STATE_LOGGED_IN)
  972. printk(KERN_INFO "iscsi: host reset succeeded\n");
  973. else
  974. goto failed;
  975. spin_unlock_bh(&session->lock);
  976. mutex_unlock(&session->eh_mutex);
  977. return SUCCESS;
  978. }
  979. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  980. static void iscsi_tmf_timedout(unsigned long data)
  981. {
  982. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  983. struct iscsi_session *session = conn->session;
  984. spin_lock(&session->lock);
  985. if (conn->tmf_state == TMF_QUEUED) {
  986. conn->tmf_state = TMF_TIMEDOUT;
  987. debug_scsi("tmf timedout\n");
  988. /* unblock eh_abort() */
  989. wake_up(&conn->ehwait);
  990. }
  991. spin_unlock(&session->lock);
  992. }
  993. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  994. struct iscsi_tm *hdr, int age)
  995. {
  996. struct iscsi_session *session = conn->session;
  997. struct iscsi_mgmt_task *mtask;
  998. mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  999. NULL, 0);
  1000. if (!mtask) {
  1001. spin_unlock_bh(&session->lock);
  1002. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1003. spin_lock_bh(&session->lock);
  1004. debug_scsi("tmf exec failure\n");
  1005. return -EPERM;
  1006. }
  1007. conn->tmfcmd_pdus_cnt++;
  1008. conn->tmf_timer.expires = 30 * HZ + jiffies;
  1009. conn->tmf_timer.function = iscsi_tmf_timedout;
  1010. conn->tmf_timer.data = (unsigned long)conn;
  1011. add_timer(&conn->tmf_timer);
  1012. debug_scsi("tmf set timeout\n");
  1013. spin_unlock_bh(&session->lock);
  1014. mutex_unlock(&session->eh_mutex);
  1015. scsi_queue_work(session->host, &conn->xmitwork);
  1016. /*
  1017. * block eh thread until:
  1018. *
  1019. * 1) tmf response
  1020. * 2) tmf timeout
  1021. * 3) session is terminated or restarted or userspace has
  1022. * given up on recovery
  1023. */
  1024. wait_event_interruptible(conn->ehwait, age != session->age ||
  1025. session->state != ISCSI_STATE_LOGGED_IN ||
  1026. conn->tmf_state != TMF_QUEUED);
  1027. if (signal_pending(current))
  1028. flush_signals(current);
  1029. del_timer_sync(&conn->tmf_timer);
  1030. mutex_lock(&session->eh_mutex);
  1031. spin_lock_bh(&session->lock);
  1032. /* if the session drops it will clean up the mtask */
  1033. if (age != session->age ||
  1034. session->state != ISCSI_STATE_LOGGED_IN)
  1035. return -ENOTCONN;
  1036. return 0;
  1037. }
  1038. /*
  1039. * Fail commands. session lock held and recv side suspended and xmit
  1040. * thread flushed
  1041. */
  1042. static void fail_all_commands(struct iscsi_conn *conn, unsigned lun)
  1043. {
  1044. struct iscsi_cmd_task *ctask, *tmp;
  1045. if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
  1046. conn->ctask = NULL;
  1047. /* flush pending */
  1048. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1049. if (lun == ctask->sc->device->lun || lun == -1) {
  1050. debug_scsi("failing pending sc %p itt 0x%x\n",
  1051. ctask->sc, ctask->itt);
  1052. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1053. }
  1054. }
  1055. list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
  1056. if (lun == ctask->sc->device->lun || lun == -1) {
  1057. debug_scsi("failing requeued sc %p itt 0x%x\n",
  1058. ctask->sc, ctask->itt);
  1059. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1060. }
  1061. }
  1062. /* fail all other running */
  1063. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1064. if (lun == ctask->sc->device->lun || lun == -1) {
  1065. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1066. ctask->sc, ctask->itt);
  1067. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1068. }
  1069. }
  1070. }
  1071. static void iscsi_suspend_tx(struct iscsi_conn *conn)
  1072. {
  1073. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1074. scsi_flush_work(conn->session->host);
  1075. }
  1076. static void iscsi_start_tx(struct iscsi_conn *conn)
  1077. {
  1078. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1079. scsi_queue_work(conn->session->host, &conn->xmitwork);
  1080. }
  1081. static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
  1082. struct iscsi_tm *hdr)
  1083. {
  1084. memset(hdr, 0, sizeof(*hdr));
  1085. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1086. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  1087. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1088. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  1089. hdr->rtt = ctask->hdr->itt;
  1090. hdr->refcmdsn = ctask->hdr->cmdsn;
  1091. }
  1092. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1093. {
  1094. struct Scsi_Host *host = sc->device->host;
  1095. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  1096. struct iscsi_conn *conn;
  1097. struct iscsi_cmd_task *ctask;
  1098. struct iscsi_tm *hdr;
  1099. int rc, age;
  1100. mutex_lock(&session->eh_mutex);
  1101. spin_lock_bh(&session->lock);
  1102. /*
  1103. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1104. * got the command.
  1105. */
  1106. if (!sc->SCp.ptr) {
  1107. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1108. spin_unlock_bh(&session->lock);
  1109. mutex_unlock(&session->eh_mutex);
  1110. return SUCCESS;
  1111. }
  1112. /*
  1113. * If we are not logged in or we have started a new session
  1114. * then let the host reset code handle this
  1115. */
  1116. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  1117. sc->SCp.phase != session->age) {
  1118. spin_unlock_bh(&session->lock);
  1119. mutex_unlock(&session->eh_mutex);
  1120. return FAILED;
  1121. }
  1122. conn = session->leadconn;
  1123. conn->eh_abort_cnt++;
  1124. age = session->age;
  1125. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1126. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1127. /* ctask completed before time out */
  1128. if (!ctask->sc) {
  1129. debug_scsi("sc completed while abort in progress\n");
  1130. goto success;
  1131. }
  1132. if (ctask->state == ISCSI_TASK_PENDING) {
  1133. fail_command(conn, ctask, DID_ABORT << 16);
  1134. goto success;
  1135. }
  1136. /* only have one tmf outstanding at a time */
  1137. if (conn->tmf_state != TMF_INITIAL)
  1138. goto failed;
  1139. conn->tmf_state = TMF_QUEUED;
  1140. hdr = &conn->tmhdr;
  1141. iscsi_prep_abort_task_pdu(ctask, hdr);
  1142. if (iscsi_exec_task_mgmt_fn(conn, hdr, age)) {
  1143. rc = FAILED;
  1144. goto failed;
  1145. }
  1146. switch (conn->tmf_state) {
  1147. case TMF_SUCCESS:
  1148. spin_unlock_bh(&session->lock);
  1149. iscsi_suspend_tx(conn);
  1150. /*
  1151. * clean up task if aborted. grab the recv lock as a writer
  1152. */
  1153. write_lock_bh(conn->recv_lock);
  1154. spin_lock(&session->lock);
  1155. fail_command(conn, ctask, DID_ABORT << 16);
  1156. conn->tmf_state = TMF_INITIAL;
  1157. spin_unlock(&session->lock);
  1158. write_unlock_bh(conn->recv_lock);
  1159. iscsi_start_tx(conn);
  1160. goto success_unlocked;
  1161. case TMF_TIMEDOUT:
  1162. spin_unlock_bh(&session->lock);
  1163. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1164. goto failed_unlocked;
  1165. case TMF_NOT_FOUND:
  1166. if (!sc->SCp.ptr) {
  1167. conn->tmf_state = TMF_INITIAL;
  1168. /* ctask completed before tmf abort response */
  1169. debug_scsi("sc completed while abort in progress\n");
  1170. goto success;
  1171. }
  1172. /* fall through */
  1173. default:
  1174. conn->tmf_state = TMF_INITIAL;
  1175. goto failed;
  1176. }
  1177. success:
  1178. spin_unlock_bh(&session->lock);
  1179. success_unlocked:
  1180. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1181. mutex_unlock(&session->eh_mutex);
  1182. return SUCCESS;
  1183. failed:
  1184. spin_unlock_bh(&session->lock);
  1185. failed_unlocked:
  1186. debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
  1187. ctask ? ctask->itt : 0);
  1188. mutex_unlock(&session->eh_mutex);
  1189. return FAILED;
  1190. }
  1191. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1192. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  1193. {
  1194. memset(hdr, 0, sizeof(*hdr));
  1195. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1196. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  1197. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1198. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  1199. hdr->rtt = ISCSI_RESERVED_TAG;
  1200. }
  1201. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  1202. {
  1203. struct Scsi_Host *host = sc->device->host;
  1204. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  1205. struct iscsi_conn *conn;
  1206. struct iscsi_tm *hdr;
  1207. int rc = FAILED;
  1208. debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
  1209. mutex_lock(&session->eh_mutex);
  1210. spin_lock_bh(&session->lock);
  1211. /*
  1212. * Just check if we are not logged in. We cannot check for
  1213. * the phase because the reset could come from a ioctl.
  1214. */
  1215. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  1216. goto unlock;
  1217. conn = session->leadconn;
  1218. /* only have one tmf outstanding at a time */
  1219. if (conn->tmf_state != TMF_INITIAL)
  1220. goto unlock;
  1221. conn->tmf_state = TMF_QUEUED;
  1222. hdr = &conn->tmhdr;
  1223. iscsi_prep_lun_reset_pdu(sc, hdr);
  1224. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age)) {
  1225. rc = FAILED;
  1226. goto unlock;
  1227. }
  1228. switch (conn->tmf_state) {
  1229. case TMF_SUCCESS:
  1230. break;
  1231. case TMF_TIMEDOUT:
  1232. spin_unlock_bh(&session->lock);
  1233. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1234. goto done;
  1235. default:
  1236. conn->tmf_state = TMF_INITIAL;
  1237. goto unlock;
  1238. }
  1239. rc = SUCCESS;
  1240. spin_unlock_bh(&session->lock);
  1241. iscsi_suspend_tx(conn);
  1242. /* need to grab the recv lock then session lock */
  1243. write_lock_bh(conn->recv_lock);
  1244. spin_lock(&session->lock);
  1245. fail_all_commands(conn, sc->device->lun);
  1246. conn->tmf_state = TMF_INITIAL;
  1247. spin_unlock(&session->lock);
  1248. write_unlock_bh(conn->recv_lock);
  1249. iscsi_start_tx(conn);
  1250. goto done;
  1251. unlock:
  1252. spin_unlock_bh(&session->lock);
  1253. done:
  1254. debug_scsi("iscsi_eh_device_reset %s\n",
  1255. rc == SUCCESS ? "SUCCESS" : "FAILED");
  1256. mutex_unlock(&session->eh_mutex);
  1257. return rc;
  1258. }
  1259. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  1260. /*
  1261. * Pre-allocate a pool of @max items of @item_size. By default, the pool
  1262. * should be accessed via kfifo_{get,put} on q->queue.
  1263. * Optionally, the caller can obtain the array of object pointers
  1264. * by passing in a non-NULL @items pointer
  1265. */
  1266. int
  1267. iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
  1268. {
  1269. int i, num_arrays = 1;
  1270. memset(q, 0, sizeof(*q));
  1271. q->max = max;
  1272. /* If the user passed an items pointer, he wants a copy of
  1273. * the array. */
  1274. if (items)
  1275. num_arrays++;
  1276. q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
  1277. if (q->pool == NULL)
  1278. goto enomem;
  1279. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1280. GFP_KERNEL, NULL);
  1281. if (q->queue == ERR_PTR(-ENOMEM))
  1282. goto enomem;
  1283. for (i = 0; i < max; i++) {
  1284. q->pool[i] = kzalloc(item_size, GFP_KERNEL);
  1285. if (q->pool[i] == NULL) {
  1286. q->max = i;
  1287. goto enomem;
  1288. }
  1289. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1290. }
  1291. if (items) {
  1292. *items = q->pool + max;
  1293. memcpy(*items, q->pool, max * sizeof(void *));
  1294. }
  1295. return 0;
  1296. enomem:
  1297. iscsi_pool_free(q);
  1298. return -ENOMEM;
  1299. }
  1300. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1301. void iscsi_pool_free(struct iscsi_pool *q)
  1302. {
  1303. int i;
  1304. for (i = 0; i < q->max; i++)
  1305. kfree(q->pool[i]);
  1306. if (q->pool)
  1307. kfree(q->pool);
  1308. }
  1309. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1310. /*
  1311. * iSCSI Session's hostdata organization:
  1312. *
  1313. * *------------------* <== hostdata_session(host->hostdata)
  1314. * | ptr to class sess|
  1315. * |------------------| <== iscsi_hostdata(host->hostdata)
  1316. * | iscsi_session |
  1317. * *------------------*
  1318. */
  1319. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1320. _sz % sizeof(unsigned long))
  1321. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1322. /**
  1323. * iscsi_session_setup - create iscsi cls session and host and session
  1324. * @scsit: scsi transport template
  1325. * @iscsit: iscsi transport template
  1326. * @cmds_max: scsi host can queue
  1327. * @qdepth: scsi host cmds per lun
  1328. * @cmd_task_size: LLD ctask private data size
  1329. * @mgmt_task_size: LLD mtask private data size
  1330. * @initial_cmdsn: initial CmdSN
  1331. * @hostno: host no allocated
  1332. *
  1333. * This can be used by software iscsi_transports that allocate
  1334. * a session per scsi host.
  1335. **/
  1336. struct iscsi_cls_session *
  1337. iscsi_session_setup(struct iscsi_transport *iscsit,
  1338. struct scsi_transport_template *scsit,
  1339. uint16_t cmds_max, uint16_t qdepth,
  1340. int cmd_task_size, int mgmt_task_size,
  1341. uint32_t initial_cmdsn, uint32_t *hostno)
  1342. {
  1343. struct Scsi_Host *shost;
  1344. struct iscsi_session *session;
  1345. struct iscsi_cls_session *cls_session;
  1346. int cmd_i;
  1347. if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
  1348. if (qdepth != 0)
  1349. printk(KERN_ERR "iscsi: invalid queue depth of %d. "
  1350. "Queue depth must be between 1 and %d.\n",
  1351. qdepth, ISCSI_MAX_CMD_PER_LUN);
  1352. qdepth = ISCSI_DEF_CMD_PER_LUN;
  1353. }
  1354. if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
  1355. cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
  1356. if (cmds_max != 0)
  1357. printk(KERN_ERR "iscsi: invalid can_queue of %d. "
  1358. "can_queue must be a power of 2 and between "
  1359. "2 and %d - setting to %d.\n", cmds_max,
  1360. ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
  1361. cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
  1362. }
  1363. shost = scsi_host_alloc(iscsit->host_template,
  1364. hostdata_privsize(sizeof(*session)));
  1365. if (!shost)
  1366. return NULL;
  1367. /* the iscsi layer takes one task for reserve */
  1368. shost->can_queue = cmds_max - 1;
  1369. shost->cmd_per_lun = qdepth;
  1370. shost->max_id = 1;
  1371. shost->max_channel = 0;
  1372. shost->max_lun = iscsit->max_lun;
  1373. shost->max_cmd_len = iscsit->max_cmd_len;
  1374. shost->transportt = scsit;
  1375. shost->transportt->create_work_queue = 1;
  1376. *hostno = shost->host_no;
  1377. session = iscsi_hostdata(shost->hostdata);
  1378. memset(session, 0, sizeof(struct iscsi_session));
  1379. session->host = shost;
  1380. session->state = ISCSI_STATE_FREE;
  1381. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1382. session->cmds_max = cmds_max;
  1383. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  1384. session->exp_cmdsn = initial_cmdsn + 1;
  1385. session->max_cmdsn = initial_cmdsn + 1;
  1386. session->max_r2t = 1;
  1387. session->tt = iscsit;
  1388. mutex_init(&session->eh_mutex);
  1389. /* initialize SCSI PDU commands pool */
  1390. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1391. (void***)&session->cmds,
  1392. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1393. goto cmdpool_alloc_fail;
  1394. /* pre-format cmds pool with ITT */
  1395. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1396. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1397. if (cmd_task_size)
  1398. ctask->dd_data = &ctask[1];
  1399. ctask->itt = cmd_i;
  1400. INIT_LIST_HEAD(&ctask->running);
  1401. }
  1402. spin_lock_init(&session->lock);
  1403. /* initialize immediate command pool */
  1404. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1405. (void***)&session->mgmt_cmds,
  1406. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1407. goto mgmtpool_alloc_fail;
  1408. /* pre-format immediate cmds pool with ITT */
  1409. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1410. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1411. if (mgmt_task_size)
  1412. mtask->dd_data = &mtask[1];
  1413. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1414. INIT_LIST_HEAD(&mtask->running);
  1415. }
  1416. if (scsi_add_host(shost, NULL))
  1417. goto add_host_fail;
  1418. if (!try_module_get(iscsit->owner))
  1419. goto cls_session_fail;
  1420. cls_session = iscsi_create_session(shost, iscsit, 0);
  1421. if (!cls_session)
  1422. goto module_put;
  1423. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1424. return cls_session;
  1425. module_put:
  1426. module_put(iscsit->owner);
  1427. cls_session_fail:
  1428. scsi_remove_host(shost);
  1429. add_host_fail:
  1430. iscsi_pool_free(&session->mgmtpool);
  1431. mgmtpool_alloc_fail:
  1432. iscsi_pool_free(&session->cmdpool);
  1433. cmdpool_alloc_fail:
  1434. scsi_host_put(shost);
  1435. return NULL;
  1436. }
  1437. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1438. /**
  1439. * iscsi_session_teardown - destroy session, host, and cls_session
  1440. * shost: scsi host
  1441. *
  1442. * This can be used by software iscsi_transports that allocate
  1443. * a session per scsi host.
  1444. **/
  1445. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1446. {
  1447. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1448. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1449. struct module *owner = cls_session->transport->owner;
  1450. iscsi_remove_session(cls_session);
  1451. scsi_remove_host(shost);
  1452. iscsi_pool_free(&session->mgmtpool);
  1453. iscsi_pool_free(&session->cmdpool);
  1454. kfree(session->password);
  1455. kfree(session->password_in);
  1456. kfree(session->username);
  1457. kfree(session->username_in);
  1458. kfree(session->targetname);
  1459. kfree(session->netdev);
  1460. kfree(session->hwaddress);
  1461. kfree(session->initiatorname);
  1462. iscsi_free_session(cls_session);
  1463. scsi_host_put(shost);
  1464. module_put(owner);
  1465. }
  1466. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1467. /**
  1468. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1469. * @cls_session: iscsi_cls_session
  1470. * @conn_idx: cid
  1471. **/
  1472. struct iscsi_cls_conn *
  1473. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1474. {
  1475. struct iscsi_session *session = class_to_transport_session(cls_session);
  1476. struct iscsi_conn *conn;
  1477. struct iscsi_cls_conn *cls_conn;
  1478. char *data;
  1479. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1480. if (!cls_conn)
  1481. return NULL;
  1482. conn = cls_conn->dd_data;
  1483. memset(conn, 0, sizeof(*conn));
  1484. conn->session = session;
  1485. conn->cls_conn = cls_conn;
  1486. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1487. conn->id = conn_idx;
  1488. conn->exp_statsn = 0;
  1489. conn->tmf_state = TMF_INITIAL;
  1490. INIT_LIST_HEAD(&conn->run_list);
  1491. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1492. INIT_LIST_HEAD(&conn->mgmtqueue);
  1493. INIT_LIST_HEAD(&conn->xmitqueue);
  1494. INIT_LIST_HEAD(&conn->requeue);
  1495. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  1496. /* allocate login_mtask used for the login/text sequences */
  1497. spin_lock_bh(&session->lock);
  1498. if (!__kfifo_get(session->mgmtpool.queue,
  1499. (void*)&conn->login_mtask,
  1500. sizeof(void*))) {
  1501. spin_unlock_bh(&session->lock);
  1502. goto login_mtask_alloc_fail;
  1503. }
  1504. spin_unlock_bh(&session->lock);
  1505. data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
  1506. if (!data)
  1507. goto login_mtask_data_alloc_fail;
  1508. conn->login_mtask->data = conn->data = data;
  1509. init_timer(&conn->tmf_timer);
  1510. init_waitqueue_head(&conn->ehwait);
  1511. return cls_conn;
  1512. login_mtask_data_alloc_fail:
  1513. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1514. sizeof(void*));
  1515. login_mtask_alloc_fail:
  1516. iscsi_destroy_conn(cls_conn);
  1517. return NULL;
  1518. }
  1519. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1520. /**
  1521. * iscsi_conn_teardown - teardown iscsi connection
  1522. * cls_conn: iscsi class connection
  1523. *
  1524. * TODO: we may need to make this into a two step process
  1525. * like scsi-mls remove + put host
  1526. */
  1527. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1528. {
  1529. struct iscsi_conn *conn = cls_conn->dd_data;
  1530. struct iscsi_session *session = conn->session;
  1531. unsigned long flags;
  1532. spin_lock_bh(&session->lock);
  1533. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1534. if (session->leadconn == conn) {
  1535. /*
  1536. * leading connection? then give up on recovery.
  1537. */
  1538. session->state = ISCSI_STATE_TERMINATE;
  1539. wake_up(&conn->ehwait);
  1540. }
  1541. spin_unlock_bh(&session->lock);
  1542. /*
  1543. * Block until all in-progress commands for this connection
  1544. * time out or fail.
  1545. */
  1546. for (;;) {
  1547. spin_lock_irqsave(session->host->host_lock, flags);
  1548. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1549. spin_unlock_irqrestore(session->host->host_lock, flags);
  1550. break;
  1551. }
  1552. spin_unlock_irqrestore(session->host->host_lock, flags);
  1553. msleep_interruptible(500);
  1554. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1555. "host_failed %d\n", session->host->host_busy,
  1556. session->host->host_failed);
  1557. /*
  1558. * force eh_abort() to unblock
  1559. */
  1560. wake_up(&conn->ehwait);
  1561. }
  1562. /* flush queued up work because we free the connection below */
  1563. iscsi_suspend_tx(conn);
  1564. spin_lock_bh(&session->lock);
  1565. kfree(conn->data);
  1566. kfree(conn->persistent_address);
  1567. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1568. sizeof(void*));
  1569. if (session->leadconn == conn)
  1570. session->leadconn = NULL;
  1571. spin_unlock_bh(&session->lock);
  1572. iscsi_destroy_conn(cls_conn);
  1573. }
  1574. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1575. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1576. {
  1577. struct iscsi_conn *conn = cls_conn->dd_data;
  1578. struct iscsi_session *session = conn->session;
  1579. if (!session) {
  1580. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1581. return -EPERM;
  1582. }
  1583. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1584. session->first_burst > session->max_burst) {
  1585. printk("iscsi: invalid burst lengths: "
  1586. "first_burst %d max_burst %d\n",
  1587. session->first_burst, session->max_burst);
  1588. return -EINVAL;
  1589. }
  1590. spin_lock_bh(&session->lock);
  1591. conn->c_stage = ISCSI_CONN_STARTED;
  1592. session->state = ISCSI_STATE_LOGGED_IN;
  1593. session->queued_cmdsn = session->cmdsn;
  1594. switch(conn->stop_stage) {
  1595. case STOP_CONN_RECOVER:
  1596. /*
  1597. * unblock eh_abort() if it is blocked. re-try all
  1598. * commands after successful recovery
  1599. */
  1600. conn->stop_stage = 0;
  1601. conn->tmf_state = TMF_INITIAL;
  1602. session->age++;
  1603. spin_unlock_bh(&session->lock);
  1604. iscsi_unblock_session(session_to_cls(session));
  1605. wake_up(&conn->ehwait);
  1606. return 0;
  1607. case STOP_CONN_TERM:
  1608. conn->stop_stage = 0;
  1609. break;
  1610. default:
  1611. break;
  1612. }
  1613. spin_unlock_bh(&session->lock);
  1614. return 0;
  1615. }
  1616. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1617. static void
  1618. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1619. {
  1620. struct iscsi_mgmt_task *mtask, *tmp;
  1621. /* handle pending */
  1622. list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
  1623. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1624. iscsi_free_mgmt_task(conn, mtask);
  1625. }
  1626. /* handle running */
  1627. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1628. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1629. iscsi_free_mgmt_task(conn, mtask);
  1630. }
  1631. conn->mtask = NULL;
  1632. }
  1633. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1634. struct iscsi_conn *conn, int flag)
  1635. {
  1636. int old_stop_stage;
  1637. mutex_lock(&session->eh_mutex);
  1638. spin_lock_bh(&session->lock);
  1639. if (conn->stop_stage == STOP_CONN_TERM) {
  1640. spin_unlock_bh(&session->lock);
  1641. mutex_unlock(&session->eh_mutex);
  1642. return;
  1643. }
  1644. /*
  1645. * The LLD either freed/unset the lock on us, or userspace called
  1646. * stop but did not create a proper connection (connection was never
  1647. * bound or it was unbound then stop was called).
  1648. */
  1649. if (!conn->recv_lock) {
  1650. spin_unlock_bh(&session->lock);
  1651. mutex_unlock(&session->eh_mutex);
  1652. return;
  1653. }
  1654. /*
  1655. * When this is called for the in_login state, we only want to clean
  1656. * up the login task and connection. We do not need to block and set
  1657. * the recovery state again
  1658. */
  1659. if (flag == STOP_CONN_TERM)
  1660. session->state = ISCSI_STATE_TERMINATE;
  1661. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1662. session->state = ISCSI_STATE_IN_RECOVERY;
  1663. old_stop_stage = conn->stop_stage;
  1664. conn->stop_stage = flag;
  1665. conn->c_stage = ISCSI_CONN_STOPPED;
  1666. spin_unlock_bh(&session->lock);
  1667. iscsi_suspend_tx(conn);
  1668. write_lock_bh(conn->recv_lock);
  1669. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1670. write_unlock_bh(conn->recv_lock);
  1671. /*
  1672. * for connection level recovery we should not calculate
  1673. * header digest. conn->hdr_size used for optimization
  1674. * in hdr_extract() and will be re-negotiated at
  1675. * set_param() time.
  1676. */
  1677. if (flag == STOP_CONN_RECOVER) {
  1678. conn->hdrdgst_en = 0;
  1679. conn->datadgst_en = 0;
  1680. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1681. old_stop_stage != STOP_CONN_RECOVER) {
  1682. debug_scsi("blocking session\n");
  1683. iscsi_block_session(session_to_cls(session));
  1684. }
  1685. }
  1686. /*
  1687. * flush queues.
  1688. */
  1689. spin_lock_bh(&session->lock);
  1690. fail_all_commands(conn, -1);
  1691. flush_control_queues(session, conn);
  1692. spin_unlock_bh(&session->lock);
  1693. mutex_unlock(&session->eh_mutex);
  1694. }
  1695. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1696. {
  1697. struct iscsi_conn *conn = cls_conn->dd_data;
  1698. struct iscsi_session *session = conn->session;
  1699. switch (flag) {
  1700. case STOP_CONN_RECOVER:
  1701. case STOP_CONN_TERM:
  1702. iscsi_start_session_recovery(session, conn, flag);
  1703. break;
  1704. default:
  1705. printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
  1706. }
  1707. }
  1708. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1709. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1710. struct iscsi_cls_conn *cls_conn, int is_leading)
  1711. {
  1712. struct iscsi_session *session = class_to_transport_session(cls_session);
  1713. struct iscsi_conn *conn = cls_conn->dd_data;
  1714. spin_lock_bh(&session->lock);
  1715. if (is_leading)
  1716. session->leadconn = conn;
  1717. spin_unlock_bh(&session->lock);
  1718. /*
  1719. * Unblock xmitworker(), Login Phase will pass through.
  1720. */
  1721. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1722. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1723. return 0;
  1724. }
  1725. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1726. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1727. enum iscsi_param param, char *buf, int buflen)
  1728. {
  1729. struct iscsi_conn *conn = cls_conn->dd_data;
  1730. struct iscsi_session *session = conn->session;
  1731. uint32_t value;
  1732. switch(param) {
  1733. case ISCSI_PARAM_FAST_ABORT:
  1734. sscanf(buf, "%d", &session->fast_abort);
  1735. break;
  1736. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1737. sscanf(buf, "%d", &conn->max_recv_dlength);
  1738. break;
  1739. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1740. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1741. break;
  1742. case ISCSI_PARAM_HDRDGST_EN:
  1743. sscanf(buf, "%d", &conn->hdrdgst_en);
  1744. break;
  1745. case ISCSI_PARAM_DATADGST_EN:
  1746. sscanf(buf, "%d", &conn->datadgst_en);
  1747. break;
  1748. case ISCSI_PARAM_INITIAL_R2T_EN:
  1749. sscanf(buf, "%d", &session->initial_r2t_en);
  1750. break;
  1751. case ISCSI_PARAM_MAX_R2T:
  1752. sscanf(buf, "%d", &session->max_r2t);
  1753. break;
  1754. case ISCSI_PARAM_IMM_DATA_EN:
  1755. sscanf(buf, "%d", &session->imm_data_en);
  1756. break;
  1757. case ISCSI_PARAM_FIRST_BURST:
  1758. sscanf(buf, "%d", &session->first_burst);
  1759. break;
  1760. case ISCSI_PARAM_MAX_BURST:
  1761. sscanf(buf, "%d", &session->max_burst);
  1762. break;
  1763. case ISCSI_PARAM_PDU_INORDER_EN:
  1764. sscanf(buf, "%d", &session->pdu_inorder_en);
  1765. break;
  1766. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1767. sscanf(buf, "%d", &session->dataseq_inorder_en);
  1768. break;
  1769. case ISCSI_PARAM_ERL:
  1770. sscanf(buf, "%d", &session->erl);
  1771. break;
  1772. case ISCSI_PARAM_IFMARKER_EN:
  1773. sscanf(buf, "%d", &value);
  1774. BUG_ON(value);
  1775. break;
  1776. case ISCSI_PARAM_OFMARKER_EN:
  1777. sscanf(buf, "%d", &value);
  1778. BUG_ON(value);
  1779. break;
  1780. case ISCSI_PARAM_EXP_STATSN:
  1781. sscanf(buf, "%u", &conn->exp_statsn);
  1782. break;
  1783. case ISCSI_PARAM_USERNAME:
  1784. kfree(session->username);
  1785. session->username = kstrdup(buf, GFP_KERNEL);
  1786. if (!session->username)
  1787. return -ENOMEM;
  1788. break;
  1789. case ISCSI_PARAM_USERNAME_IN:
  1790. kfree(session->username_in);
  1791. session->username_in = kstrdup(buf, GFP_KERNEL);
  1792. if (!session->username_in)
  1793. return -ENOMEM;
  1794. break;
  1795. case ISCSI_PARAM_PASSWORD:
  1796. kfree(session->password);
  1797. session->password = kstrdup(buf, GFP_KERNEL);
  1798. if (!session->password)
  1799. return -ENOMEM;
  1800. break;
  1801. case ISCSI_PARAM_PASSWORD_IN:
  1802. kfree(session->password_in);
  1803. session->password_in = kstrdup(buf, GFP_KERNEL);
  1804. if (!session->password_in)
  1805. return -ENOMEM;
  1806. break;
  1807. case ISCSI_PARAM_TARGET_NAME:
  1808. /* this should not change between logins */
  1809. if (session->targetname)
  1810. break;
  1811. session->targetname = kstrdup(buf, GFP_KERNEL);
  1812. if (!session->targetname)
  1813. return -ENOMEM;
  1814. break;
  1815. case ISCSI_PARAM_TPGT:
  1816. sscanf(buf, "%d", &session->tpgt);
  1817. break;
  1818. case ISCSI_PARAM_PERSISTENT_PORT:
  1819. sscanf(buf, "%d", &conn->persistent_port);
  1820. break;
  1821. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1822. /*
  1823. * this is the address returned in discovery so it should
  1824. * not change between logins.
  1825. */
  1826. if (conn->persistent_address)
  1827. break;
  1828. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  1829. if (!conn->persistent_address)
  1830. return -ENOMEM;
  1831. break;
  1832. default:
  1833. return -ENOSYS;
  1834. }
  1835. return 0;
  1836. }
  1837. EXPORT_SYMBOL_GPL(iscsi_set_param);
  1838. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  1839. enum iscsi_param param, char *buf)
  1840. {
  1841. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1842. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1843. int len;
  1844. switch(param) {
  1845. case ISCSI_PARAM_FAST_ABORT:
  1846. len = sprintf(buf, "%d\n", session->fast_abort);
  1847. break;
  1848. case ISCSI_PARAM_INITIAL_R2T_EN:
  1849. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  1850. break;
  1851. case ISCSI_PARAM_MAX_R2T:
  1852. len = sprintf(buf, "%hu\n", session->max_r2t);
  1853. break;
  1854. case ISCSI_PARAM_IMM_DATA_EN:
  1855. len = sprintf(buf, "%d\n", session->imm_data_en);
  1856. break;
  1857. case ISCSI_PARAM_FIRST_BURST:
  1858. len = sprintf(buf, "%u\n", session->first_burst);
  1859. break;
  1860. case ISCSI_PARAM_MAX_BURST:
  1861. len = sprintf(buf, "%u\n", session->max_burst);
  1862. break;
  1863. case ISCSI_PARAM_PDU_INORDER_EN:
  1864. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  1865. break;
  1866. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1867. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  1868. break;
  1869. case ISCSI_PARAM_ERL:
  1870. len = sprintf(buf, "%d\n", session->erl);
  1871. break;
  1872. case ISCSI_PARAM_TARGET_NAME:
  1873. len = sprintf(buf, "%s\n", session->targetname);
  1874. break;
  1875. case ISCSI_PARAM_TPGT:
  1876. len = sprintf(buf, "%d\n", session->tpgt);
  1877. break;
  1878. case ISCSI_PARAM_USERNAME:
  1879. len = sprintf(buf, "%s\n", session->username);
  1880. break;
  1881. case ISCSI_PARAM_USERNAME_IN:
  1882. len = sprintf(buf, "%s\n", session->username_in);
  1883. break;
  1884. case ISCSI_PARAM_PASSWORD:
  1885. len = sprintf(buf, "%s\n", session->password);
  1886. break;
  1887. case ISCSI_PARAM_PASSWORD_IN:
  1888. len = sprintf(buf, "%s\n", session->password_in);
  1889. break;
  1890. default:
  1891. return -ENOSYS;
  1892. }
  1893. return len;
  1894. }
  1895. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  1896. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1897. enum iscsi_param param, char *buf)
  1898. {
  1899. struct iscsi_conn *conn = cls_conn->dd_data;
  1900. int len;
  1901. switch(param) {
  1902. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1903. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  1904. break;
  1905. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1906. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  1907. break;
  1908. case ISCSI_PARAM_HDRDGST_EN:
  1909. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  1910. break;
  1911. case ISCSI_PARAM_DATADGST_EN:
  1912. len = sprintf(buf, "%d\n", conn->datadgst_en);
  1913. break;
  1914. case ISCSI_PARAM_IFMARKER_EN:
  1915. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  1916. break;
  1917. case ISCSI_PARAM_OFMARKER_EN:
  1918. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  1919. break;
  1920. case ISCSI_PARAM_EXP_STATSN:
  1921. len = sprintf(buf, "%u\n", conn->exp_statsn);
  1922. break;
  1923. case ISCSI_PARAM_PERSISTENT_PORT:
  1924. len = sprintf(buf, "%d\n", conn->persistent_port);
  1925. break;
  1926. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1927. len = sprintf(buf, "%s\n", conn->persistent_address);
  1928. break;
  1929. default:
  1930. return -ENOSYS;
  1931. }
  1932. return len;
  1933. }
  1934. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  1935. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1936. char *buf)
  1937. {
  1938. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1939. int len;
  1940. switch (param) {
  1941. case ISCSI_HOST_PARAM_NETDEV_NAME:
  1942. if (!session->netdev)
  1943. len = sprintf(buf, "%s\n", "default");
  1944. else
  1945. len = sprintf(buf, "%s\n", session->netdev);
  1946. break;
  1947. case ISCSI_HOST_PARAM_HWADDRESS:
  1948. if (!session->hwaddress)
  1949. len = sprintf(buf, "%s\n", "default");
  1950. else
  1951. len = sprintf(buf, "%s\n", session->hwaddress);
  1952. break;
  1953. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  1954. if (!session->initiatorname)
  1955. len = sprintf(buf, "%s\n", "unknown");
  1956. else
  1957. len = sprintf(buf, "%s\n", session->initiatorname);
  1958. break;
  1959. default:
  1960. return -ENOSYS;
  1961. }
  1962. return len;
  1963. }
  1964. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  1965. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1966. char *buf, int buflen)
  1967. {
  1968. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1969. switch (param) {
  1970. case ISCSI_HOST_PARAM_NETDEV_NAME:
  1971. if (!session->netdev)
  1972. session->netdev = kstrdup(buf, GFP_KERNEL);
  1973. break;
  1974. case ISCSI_HOST_PARAM_HWADDRESS:
  1975. if (!session->hwaddress)
  1976. session->hwaddress = kstrdup(buf, GFP_KERNEL);
  1977. break;
  1978. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  1979. if (!session->initiatorname)
  1980. session->initiatorname = kstrdup(buf, GFP_KERNEL);
  1981. break;
  1982. default:
  1983. return -ENOSYS;
  1984. }
  1985. return 0;
  1986. }
  1987. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  1988. MODULE_AUTHOR("Mike Christie");
  1989. MODULE_DESCRIPTION("iSCSI library functions");
  1990. MODULE_LICENSE("GPL");