zfcp_scsi.c 24 KB

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