zfcp_scsi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * This file is part of the zfcp device driver for
  3. * FCP adapters for IBM System z9 and zSeries.
  4. *
  5. * (C) Copyright IBM Corp. 2002, 2006
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_SCSI
  22. #include "zfcp_ext.h"
  23. static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
  24. static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
  25. static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
  26. static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
  27. void (*done) (struct scsi_cmnd *));
  28. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
  29. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
  30. static int zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *);
  31. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
  32. static int zfcp_task_management_function(struct zfcp_unit *, u8,
  33. struct scsi_cmnd *);
  34. static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
  35. unsigned int, unsigned int);
  36. static struct device_attribute *zfcp_sysfs_sdev_attrs[];
  37. struct scsi_transport_template *zfcp_transport_template;
  38. struct zfcp_data zfcp_data = {
  39. .scsi_host_template = {
  40. name: ZFCP_NAME,
  41. proc_name: "zfcp",
  42. proc_info: NULL,
  43. detect: NULL,
  44. slave_alloc: zfcp_scsi_slave_alloc,
  45. slave_configure: zfcp_scsi_slave_configure,
  46. slave_destroy: zfcp_scsi_slave_destroy,
  47. queuecommand: zfcp_scsi_queuecommand,
  48. eh_abort_handler: zfcp_scsi_eh_abort_handler,
  49. eh_device_reset_handler: zfcp_scsi_eh_device_reset_handler,
  50. eh_bus_reset_handler: zfcp_scsi_eh_bus_reset_handler,
  51. eh_host_reset_handler: zfcp_scsi_eh_host_reset_handler,
  52. /* FIXME(openfcp): Tune */
  53. can_queue: 4096,
  54. this_id: -1,
  55. /*
  56. * FIXME:
  57. * one less? can zfcp_create_sbale cope with it?
  58. */
  59. sg_tablesize: ZFCP_MAX_SBALES_PER_REQ,
  60. cmd_per_lun: 1,
  61. unchecked_isa_dma: 0,
  62. use_clustering: 1,
  63. sdev_attrs: zfcp_sysfs_sdev_attrs,
  64. },
  65. .driver_version = ZFCP_VERSION,
  66. /* rest initialised with zeros */
  67. };
  68. /* Find start of Response Information in FCP response unit*/
  69. char *
  70. zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  71. {
  72. char *fcp_rsp_info_ptr;
  73. fcp_rsp_info_ptr =
  74. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  75. return fcp_rsp_info_ptr;
  76. }
  77. /* Find start of Sense Information in FCP response unit*/
  78. char *
  79. zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  80. {
  81. char *fcp_sns_info_ptr;
  82. fcp_sns_info_ptr =
  83. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  84. if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
  85. fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
  86. fcp_rsp_iu->fcp_rsp_len;
  87. return fcp_sns_info_ptr;
  88. }
  89. fcp_dl_t *
  90. zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
  91. {
  92. int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
  93. fcp_dl_t *fcp_dl_addr;
  94. fcp_dl_addr = (fcp_dl_t *)
  95. ((unsigned char *) fcp_cmd +
  96. sizeof (struct fcp_cmnd_iu) + additional_length);
  97. /*
  98. * fcp_dl_addr = start address of fcp_cmnd structure +
  99. * size of fixed part + size of dynamically sized add_dcp_cdb field
  100. * SEE FCP-2 documentation
  101. */
  102. return fcp_dl_addr;
  103. }
  104. fcp_dl_t
  105. zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
  106. {
  107. return *zfcp_get_fcp_dl_ptr(fcp_cmd);
  108. }
  109. void
  110. zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
  111. {
  112. *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
  113. }
  114. /*
  115. * note: it's a bit-or operation not an assignment
  116. * regarding the specified byte
  117. */
  118. static inline void
  119. set_byte(u32 * result, char status, char pos)
  120. {
  121. *result |= status << (pos * 8);
  122. }
  123. void
  124. set_host_byte(u32 * result, char status)
  125. {
  126. set_byte(result, status, 2);
  127. }
  128. void
  129. set_driver_byte(u32 * result, char status)
  130. {
  131. set_byte(result, status, 3);
  132. }
  133. static int
  134. zfcp_scsi_slave_alloc(struct scsi_device *sdp)
  135. {
  136. struct zfcp_adapter *adapter;
  137. struct zfcp_unit *unit;
  138. unsigned long flags;
  139. int retval = -ENXIO;
  140. adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
  141. if (!adapter)
  142. goto out;
  143. read_lock_irqsave(&zfcp_data.config_lock, flags);
  144. unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
  145. if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
  146. &unit->status)) {
  147. sdp->hostdata = unit;
  148. unit->device = sdp;
  149. zfcp_unit_get(unit);
  150. retval = 0;
  151. }
  152. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  153. out:
  154. return retval;
  155. }
  156. static void
  157. zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
  158. {
  159. struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
  160. if (unit) {
  161. atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
  162. sdpnt->hostdata = NULL;
  163. unit->device = NULL;
  164. zfcp_unit_put(unit);
  165. } else {
  166. ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
  167. "address %p\n", sdpnt);
  168. }
  169. }
  170. /*
  171. * called from scsi midlayer to allow finetuning of a device.
  172. */
  173. static int
  174. zfcp_scsi_slave_configure(struct scsi_device *sdp)
  175. {
  176. if (sdp->tagged_supported)
  177. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
  178. else
  179. scsi_adjust_queue_depth(sdp, 0, 1);
  180. return 0;
  181. }
  182. /**
  183. * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
  184. * @scpnt: pointer to struct scsi_cmnd where result is set
  185. * @result: result to be set in scpnt (e.g. DID_ERROR)
  186. */
  187. static void
  188. zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
  189. {
  190. set_host_byte(&scpnt->result, result);
  191. if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
  192. zfcp_scsi_dbf_event_result("fail", 4,
  193. (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
  194. scpnt, NULL);
  195. /* return directly */
  196. scpnt->scsi_done(scpnt);
  197. }
  198. /**
  199. * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
  200. * zfcp_scsi_command_sync
  201. * @adapter: adapter where scsi command is issued
  202. * @unit: unit to which scsi command is sent
  203. * @scpnt: scsi command to be sent
  204. * @timer: timer to be started if request is successfully initiated
  205. *
  206. * Note: In scsi_done function must be set in scpnt.
  207. */
  208. int
  209. zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
  210. struct scsi_cmnd *scpnt, struct timer_list *timer)
  211. {
  212. int tmp;
  213. int retval;
  214. retval = 0;
  215. BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
  216. BUG_ON(scpnt->scsi_done == NULL);
  217. if (unlikely(NULL == unit)) {
  218. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  219. goto out;
  220. }
  221. if (unlikely(
  222. atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
  223. !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
  224. ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
  225. "0x%016Lx on adapter %s\n",
  226. unit->fcp_lun, unit->port->wwpn,
  227. zfcp_get_busid_by_adapter(adapter));
  228. zfcp_scsi_command_fail(scpnt, DID_ERROR);
  229. goto out;
  230. }
  231. if (unlikely(
  232. !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
  233. ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
  234. "on port 0x%016Lx in recovery\n",
  235. zfcp_get_busid_by_unit(unit),
  236. unit->fcp_lun, unit->port->wwpn);
  237. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  238. goto out;
  239. }
  240. tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, timer,
  241. ZFCP_REQ_AUTO_CLEANUP);
  242. if (unlikely(tmp < 0)) {
  243. ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
  244. retval = SCSI_MLQUEUE_HOST_BUSY;
  245. }
  246. out:
  247. return retval;
  248. }
  249. void
  250. zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
  251. {
  252. struct completion *wait = (struct completion *) scpnt->SCp.ptr;
  253. complete(wait);
  254. }
  255. /**
  256. * zfcp_scsi_command_sync - send a SCSI command and wait for completion
  257. * @unit: unit where command is sent to
  258. * @scpnt: scsi command to be sent
  259. * @timer: timer to be started if request is successfully initiated
  260. * Return: 0
  261. *
  262. * Errors are indicated in scpnt->result
  263. */
  264. int
  265. zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
  266. struct timer_list *timer)
  267. {
  268. int ret;
  269. DECLARE_COMPLETION(wait);
  270. scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
  271. scpnt->scsi_done = zfcp_scsi_command_sync_handler;
  272. ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt, timer);
  273. if (ret == 0)
  274. wait_for_completion(&wait);
  275. scpnt->SCp.ptr = NULL;
  276. return 0;
  277. }
  278. /*
  279. * function: zfcp_scsi_queuecommand
  280. *
  281. * purpose: enqueues a SCSI command to the specified target device
  282. *
  283. * returns: 0 - success, SCSI command enqueued
  284. * !0 - failure
  285. */
  286. int
  287. zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
  288. void (*done) (struct scsi_cmnd *))
  289. {
  290. struct zfcp_unit *unit;
  291. struct zfcp_adapter *adapter;
  292. /* reset the status for this request */
  293. scpnt->result = 0;
  294. scpnt->host_scribble = NULL;
  295. scpnt->scsi_done = done;
  296. /*
  297. * figure out adapter and target device
  298. * (stored there by zfcp_scsi_slave_alloc)
  299. */
  300. adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
  301. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  302. return zfcp_scsi_command_async(adapter, unit, scpnt, NULL);
  303. }
  304. static struct zfcp_unit *
  305. zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
  306. unsigned int lun)
  307. {
  308. struct zfcp_port *port;
  309. struct zfcp_unit *unit, *retval = NULL;
  310. list_for_each_entry(port, &adapter->port_list_head, list) {
  311. if (!port->rport || (id != port->rport->scsi_target_id))
  312. continue;
  313. list_for_each_entry(unit, &port->unit_list_head, list) {
  314. if (lun == unit->scsi_lun) {
  315. retval = unit;
  316. goto out;
  317. }
  318. }
  319. }
  320. out:
  321. return retval;
  322. }
  323. /**
  324. * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
  325. * @scpnt: pointer to scsi_cmnd to be aborted
  326. * Return: SUCCESS - command has been aborted and cleaned up in internal
  327. * bookkeeping, SCSI stack won't be called for aborted command
  328. * FAILED - otherwise
  329. *
  330. * We do not need to care for a SCSI command which completes normally
  331. * but late during this abort routine runs. We are allowed to return
  332. * late commands to the SCSI stack. It tracks the state of commands and
  333. * will handle late commands. (Usually, the normal completion of late
  334. * commands is ignored with respect to the running abort operation.)
  335. */
  336. int
  337. zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
  338. {
  339. struct Scsi_Host *scsi_host;
  340. struct zfcp_adapter *adapter;
  341. struct zfcp_unit *unit;
  342. int retval = SUCCESS;
  343. struct zfcp_fsf_req *new_fsf_req = NULL;
  344. struct zfcp_fsf_req *old_fsf_req;
  345. unsigned long flags;
  346. scsi_host = scpnt->device->host;
  347. adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
  348. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  349. ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
  350. scpnt, zfcp_get_busid_by_adapter(adapter));
  351. /* avoid race condition between late normal completion and abort */
  352. write_lock_irqsave(&adapter->abort_lock, flags);
  353. /*
  354. * Check whether command has just completed and can not be aborted.
  355. * Even if the command has just been completed late, we can access
  356. * scpnt since the SCSI stack does not release it at least until
  357. * this routine returns. (scpnt is parameter passed to this routine
  358. * and must not disappear during abort even on late completion.)
  359. */
  360. old_fsf_req = (struct zfcp_fsf_req *) scpnt->host_scribble;
  361. if (!old_fsf_req) {
  362. write_unlock_irqrestore(&adapter->abort_lock, flags);
  363. zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, NULL);
  364. retval = SUCCESS;
  365. goto out;
  366. }
  367. old_fsf_req->data = 0;
  368. old_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
  369. /* don't access old_fsf_req after releasing the abort_lock */
  370. write_unlock_irqrestore(&adapter->abort_lock, flags);
  371. /* call FSF routine which does the abort */
  372. new_fsf_req = zfcp_fsf_abort_fcp_command((unsigned long) old_fsf_req,
  373. adapter, unit, 0);
  374. if (!new_fsf_req) {
  375. ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
  376. zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
  377. old_fsf_req);
  378. retval = FAILED;
  379. goto out;
  380. }
  381. /* wait for completion of abort */
  382. __wait_event(new_fsf_req->completion_wq,
  383. new_fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  384. /* status should be valid since signals were not permitted */
  385. if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
  386. zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, new_fsf_req,
  387. NULL);
  388. retval = SUCCESS;
  389. } else if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
  390. zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, new_fsf_req,
  391. NULL);
  392. retval = SUCCESS;
  393. } else {
  394. zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, new_fsf_req,
  395. NULL);
  396. retval = FAILED;
  397. }
  398. zfcp_fsf_req_free(new_fsf_req);
  399. out:
  400. return retval;
  401. }
  402. int
  403. zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
  404. {
  405. int retval;
  406. struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
  407. if (!unit) {
  408. ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
  409. retval = SUCCESS;
  410. goto out;
  411. }
  412. ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
  413. /*
  414. * If we do not know whether the unit supports 'logical unit reset'
  415. * then try 'logical unit reset' and proceed with 'target reset'
  416. * if 'logical unit reset' fails.
  417. * If the unit is known not to support 'logical unit reset' then
  418. * skip 'logical unit reset' and try 'target reset' immediately.
  419. */
  420. if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  421. &unit->status)) {
  422. retval = zfcp_task_management_function(unit,
  423. FCP_LOGICAL_UNIT_RESET,
  424. scpnt);
  425. if (retval) {
  426. ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
  427. if (retval == -ENOTSUPP)
  428. atomic_set_mask
  429. (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  430. &unit->status);
  431. /* fall through and try 'target reset' next */
  432. } else {
  433. ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
  434. unit);
  435. /* avoid 'target reset' */
  436. retval = SUCCESS;
  437. goto out;
  438. }
  439. }
  440. retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
  441. if (retval) {
  442. ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
  443. retval = FAILED;
  444. } else {
  445. ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
  446. retval = SUCCESS;
  447. }
  448. out:
  449. return retval;
  450. }
  451. static int
  452. zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
  453. struct scsi_cmnd *scpnt)
  454. {
  455. struct zfcp_adapter *adapter = unit->port->adapter;
  456. struct zfcp_fsf_req *fsf_req;
  457. int retval = 0;
  458. /* issue task management function */
  459. fsf_req = zfcp_fsf_send_fcp_command_task_management
  460. (adapter, unit, tm_flags, 0);
  461. if (!fsf_req) {
  462. ZFCP_LOG_INFO("error: creation of task management request "
  463. "failed for unit 0x%016Lx on port 0x%016Lx on "
  464. "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
  465. zfcp_get_busid_by_adapter(adapter));
  466. zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
  467. retval = -ENOMEM;
  468. goto out;
  469. }
  470. __wait_event(fsf_req->completion_wq,
  471. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  472. /*
  473. * check completion status of task management function
  474. */
  475. if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
  476. zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
  477. retval = -EIO;
  478. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
  479. zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
  480. retval = -ENOTSUPP;
  481. } else
  482. zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
  483. zfcp_fsf_req_free(fsf_req);
  484. out:
  485. return retval;
  486. }
  487. /**
  488. * zfcp_scsi_eh_bus_reset_handler - reset bus (reopen adapter)
  489. */
  490. int
  491. zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *scpnt)
  492. {
  493. struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
  494. struct zfcp_adapter *adapter = unit->port->adapter;
  495. ZFCP_LOG_NORMAL("bus reset because of problems with "
  496. "unit 0x%016Lx\n", unit->fcp_lun);
  497. zfcp_erp_adapter_reopen(adapter, 0);
  498. zfcp_erp_wait(adapter);
  499. return SUCCESS;
  500. }
  501. /**
  502. * zfcp_scsi_eh_host_reset_handler - reset host (reopen adapter)
  503. */
  504. int
  505. zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
  506. {
  507. struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
  508. struct zfcp_adapter *adapter = unit->port->adapter;
  509. ZFCP_LOG_NORMAL("host reset because of problems with "
  510. "unit 0x%016Lx\n", unit->fcp_lun);
  511. zfcp_erp_adapter_reopen(adapter, 0);
  512. zfcp_erp_wait(adapter);
  513. return SUCCESS;
  514. }
  515. int
  516. zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
  517. {
  518. int retval = 0;
  519. static unsigned int unique_id = 0;
  520. /* register adapter as SCSI host with mid layer of SCSI stack */
  521. adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
  522. sizeof (struct zfcp_adapter *));
  523. if (!adapter->scsi_host) {
  524. ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
  525. "for adapter %s ",
  526. zfcp_get_busid_by_adapter(adapter));
  527. retval = -EIO;
  528. goto out;
  529. }
  530. ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
  531. /* tell the SCSI stack some characteristics of this adapter */
  532. adapter->scsi_host->max_id = 1;
  533. adapter->scsi_host->max_lun = 1;
  534. adapter->scsi_host->max_channel = 0;
  535. adapter->scsi_host->unique_id = unique_id++; /* FIXME */
  536. adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
  537. adapter->scsi_host->transportt = zfcp_transport_template;
  538. /*
  539. * save a pointer to our own adapter data structure within
  540. * hostdata field of SCSI host data structure
  541. */
  542. adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
  543. if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
  544. scsi_host_put(adapter->scsi_host);
  545. retval = -EIO;
  546. goto out;
  547. }
  548. atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  549. out:
  550. return retval;
  551. }
  552. void
  553. zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
  554. {
  555. struct Scsi_Host *shost;
  556. struct zfcp_port *port;
  557. shost = adapter->scsi_host;
  558. if (!shost)
  559. return;
  560. read_lock_irq(&zfcp_data.config_lock);
  561. list_for_each_entry(port, &adapter->port_list_head, list)
  562. if (port->rport)
  563. port->rport = NULL;
  564. read_unlock_irq(&zfcp_data.config_lock);
  565. fc_remove_host(shost);
  566. scsi_remove_host(shost);
  567. scsi_host_put(shost);
  568. adapter->scsi_host = NULL;
  569. atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  570. return;
  571. }
  572. void
  573. zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
  574. {
  575. adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
  576. adapter->scsi_er_timer.data = (unsigned long) adapter;
  577. adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
  578. add_timer(&adapter->scsi_er_timer);
  579. }
  580. /*
  581. * Support functions for FC transport class
  582. */
  583. static struct fc_host_statistics*
  584. zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
  585. {
  586. struct fc_host_statistics *fc_stats;
  587. if (!adapter->fc_stats) {
  588. fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
  589. if (!fc_stats)
  590. return NULL;
  591. adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
  592. }
  593. memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
  594. return adapter->fc_stats;
  595. }
  596. static void
  597. zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
  598. struct fsf_qtcb_bottom_port *data,
  599. struct fsf_qtcb_bottom_port *old)
  600. {
  601. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
  602. old->seconds_since_last_reset;
  603. fc_stats->tx_frames = data->tx_frames - old->tx_frames;
  604. fc_stats->tx_words = data->tx_words - old->tx_words;
  605. fc_stats->rx_frames = data->rx_frames - old->rx_frames;
  606. fc_stats->rx_words = data->rx_words - old->rx_words;
  607. fc_stats->lip_count = data->lip - old->lip;
  608. fc_stats->nos_count = data->nos - old->nos;
  609. fc_stats->error_frames = data->error_frames - old->error_frames;
  610. fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
  611. fc_stats->link_failure_count = data->link_failure - old->link_failure;
  612. fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
  613. fc_stats->loss_of_signal_count = data->loss_of_signal -
  614. old->loss_of_signal;
  615. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
  616. old->psp_error_counts;
  617. fc_stats->invalid_tx_word_count = data->invalid_tx_words -
  618. old->invalid_tx_words;
  619. fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
  620. fc_stats->fcp_input_requests = data->input_requests -
  621. old->input_requests;
  622. fc_stats->fcp_output_requests = data->output_requests -
  623. old->output_requests;
  624. fc_stats->fcp_control_requests = data->control_requests -
  625. old->control_requests;
  626. fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
  627. fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
  628. }
  629. static void
  630. zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
  631. struct fsf_qtcb_bottom_port *data)
  632. {
  633. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
  634. fc_stats->tx_frames = data->tx_frames;
  635. fc_stats->tx_words = data->tx_words;
  636. fc_stats->rx_frames = data->rx_frames;
  637. fc_stats->rx_words = data->rx_words;
  638. fc_stats->lip_count = data->lip;
  639. fc_stats->nos_count = data->nos;
  640. fc_stats->error_frames = data->error_frames;
  641. fc_stats->dumped_frames = data->dumped_frames;
  642. fc_stats->link_failure_count = data->link_failure;
  643. fc_stats->loss_of_sync_count = data->loss_of_sync;
  644. fc_stats->loss_of_signal_count = data->loss_of_signal;
  645. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
  646. fc_stats->invalid_tx_word_count = data->invalid_tx_words;
  647. fc_stats->invalid_crc_count = data->invalid_crcs;
  648. fc_stats->fcp_input_requests = data->input_requests;
  649. fc_stats->fcp_output_requests = data->output_requests;
  650. fc_stats->fcp_control_requests = data->control_requests;
  651. fc_stats->fcp_input_megabytes = data->input_mb;
  652. fc_stats->fcp_output_megabytes = data->output_mb;
  653. }
  654. /**
  655. * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
  656. *
  657. * assumption: scsi_transport_fc synchronizes calls of
  658. * get_fc_host_stats and reset_fc_host_stats
  659. * (XXX to be checked otherwise introduce locking)
  660. */
  661. static struct fc_host_statistics *
  662. zfcp_get_fc_host_stats(struct Scsi_Host *shost)
  663. {
  664. struct zfcp_adapter *adapter;
  665. struct fc_host_statistics *fc_stats;
  666. struct fsf_qtcb_bottom_port *data;
  667. int ret;
  668. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  669. fc_stats = zfcp_init_fc_host_stats(adapter);
  670. if (!fc_stats)
  671. return NULL;
  672. data = kzalloc(sizeof(*data), GFP_KERNEL);
  673. if (!data)
  674. return NULL;
  675. ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
  676. if (ret) {
  677. kfree(data);
  678. return NULL; /* XXX return zeroed fc_stats? */
  679. }
  680. if (adapter->stats_reset &&
  681. ((jiffies/HZ - adapter->stats_reset) <
  682. data->seconds_since_last_reset)) {
  683. zfcp_adjust_fc_host_stats(fc_stats, data,
  684. adapter->stats_reset_data);
  685. } else
  686. zfcp_set_fc_host_stats(fc_stats, data);
  687. kfree(data);
  688. return fc_stats;
  689. }
  690. static void
  691. zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
  692. {
  693. struct zfcp_adapter *adapter;
  694. struct fsf_qtcb_bottom_port *data, *old_data;
  695. int ret;
  696. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  697. data = kzalloc(sizeof(*data), GFP_KERNEL);
  698. if (!data)
  699. return;
  700. ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
  701. if (ret == 0) {
  702. adapter->stats_reset = jiffies/HZ;
  703. old_data = adapter->stats_reset_data;
  704. adapter->stats_reset_data = data; /* finally freed in
  705. adater_dequeue */
  706. kfree(old_data);
  707. }
  708. }
  709. static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  710. {
  711. rport->dev_loss_tmo = timeout;
  712. }
  713. struct fc_function_template zfcp_transport_functions = {
  714. .show_starget_port_id = 1,
  715. .show_starget_port_name = 1,
  716. .show_starget_node_name = 1,
  717. .show_rport_supported_classes = 1,
  718. .show_rport_maxframe_size = 1,
  719. .show_rport_dev_loss_tmo = 1,
  720. .show_host_node_name = 1,
  721. .show_host_port_name = 1,
  722. .show_host_permanent_port_name = 1,
  723. .show_host_supported_classes = 1,
  724. .show_host_supported_speeds = 1,
  725. .show_host_maxframe_size = 1,
  726. .show_host_serial_number = 1,
  727. .get_fc_host_stats = zfcp_get_fc_host_stats,
  728. .reset_fc_host_stats = zfcp_reset_fc_host_stats,
  729. .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
  730. /* no functions registered for following dynamic attributes but
  731. directly set by LLDD */
  732. .show_host_port_type = 1,
  733. .show_host_speed = 1,
  734. .show_host_port_id = 1,
  735. };
  736. /**
  737. * ZFCP_DEFINE_SCSI_ATTR
  738. * @_name: name of show attribute
  739. * @_format: format string
  740. * @_value: value to print
  741. *
  742. * Generates attribute for a unit.
  743. */
  744. #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
  745. static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
  746. char *buf) \
  747. { \
  748. struct scsi_device *sdev; \
  749. struct zfcp_unit *unit; \
  750. \
  751. sdev = to_scsi_device(dev); \
  752. unit = sdev->hostdata; \
  753. return sprintf(buf, _format, _value); \
  754. } \
  755. \
  756. static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
  757. ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
  758. ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
  759. ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
  760. static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
  761. &dev_attr_fcp_lun,
  762. &dev_attr_wwpn,
  763. &dev_attr_hba_id,
  764. NULL
  765. };
  766. #undef ZFCP_LOG_AREA